Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中设置矩阵零的性能_Javascript_Performance - Fatal编程技术网

在JavaScript中设置矩阵零的性能

在JavaScript中设置矩阵零的性能,javascript,performance,Javascript,Performance,我用JavaScript构建了集合矩阵零点算法。我的代码中有很多循环,所以我想知道是否有更好的方法来实现这个算法 给定一个m x n矩阵,如果元素为0,则将其整行和整列设置为0。在适当的地方做 我循环矩阵,当我找到一个0时,我将它的整行和整列设置为一个标志('X')。如果位置为0,我不会设置“X”,因为我将来可能需要检查此位置。最后,我将所有的“X”替换为0 var setZeroes = function(matrix) { if(matrix === null || matrix.l

我用JavaScript构建了集合矩阵零点算法。我的代码中有很多循环,所以我想知道是否有更好的方法来实现这个算法

给定一个m x n矩阵,如果元素为0,则将其整行和整列设置为0。在适当的地方做

我循环矩阵,当我找到一个0时,我将它的整行和整列设置为一个标志('X')。如果位置为0,我不会设置“X”,因为我将来可能需要检查此位置。最后,我将所有的“X”替换为0

var setZeroes = function(matrix) {
    if(matrix === null || matrix.length === 0)
        return [];
    
    for(let i=0; i<matrix.length; i++){
        for(let j=0; j<matrix[i].length; j++){
            if(matrix[i][j] === 0){
                setFlag(matrix, i, j);
            }
        }
    }
    
    for(i=0; i<matrix.length; i++){
        for(j=0; j<matrix[i].length; j++){
            if(matrix[i][j] === 'X')
                matrix[i][j] = 0;
        }
    }
};

const setFlag = function(matrix, i, j) {
    matrix[i][j] = 'X';
    let tempI = i+1;
    while(tempI < matrix.length){
        if(matrix[tempI][j] !== 0)
            matrix[tempI][j] = 'X';
        tempI++;
    }
    tempI = i-1;
    while(tempI >= 0){
        if(matrix[tempI][j] !== 0)
            matrix[tempI][j] = 'X';
        tempI--;
    }
    let tempJ = j+1;
    while(tempJ < matrix[i].length){
        if(matrix[i][tempJ] !== 0)
            matrix[i][tempJ] = 'X';
        tempJ++;
    }
    tempJ = j-1;
    while(tempJ >= 0){
        if(matrix[i][tempJ] !== 0)
            matrix[i][tempJ] = 'X';
        tempJ--;
    }
}
var setZeroes=函数(矩阵){
if(matrix==null | | matrix.length==0)
返回[];

对于(让i=0;i简单方法,如果您想在js中使用更多功能

cols = []
matrix = matrix.map(row => {
    cols.push(...row.reduce((a, v, i) => (v == 0 && a.push(i), a), []) )
    return row.includes(0) ? Array(row.length).fill(0) : row;
})
然后在“cols”数组中有一个需要替换的所有列的列表。只需循环该列表并设置为零,如下所示:

cols.forEach((i) => {
    matrix = matrix.map(row => row.map((v, idx) => i == idx ? 0 : v))
})
工作示例:

let矩阵=[
[ 1, 2, 3, 4, 5 ],
[ 1, 0, 3, 4, 5 ],
[ 1, 2, 3, 4, 0 ],
[ 1, 2, 3, 4, 5 ],
[ 1, 2, 3, 4, 5 ]
];
cols=[]
矩阵=矩阵.映射(行=>{
cols.push(…row.reduce((a,v,i)=>(v==0&&a.push(i,a),[]))
返回行。包括(0)?数组(行。长度)。填充(0):行;
})
cols.forEach((i)=>{
matrix=matrix.map(row=>row.map((v,idx)=>i==idx?0:v))
})

对于(let row of matrix)console.log(row.join(“”));
这里有一个更具声明性的实现

let replaceRowCol=(矩阵,trg=0,val=0)=>{
//存储矩阵的宽度和高度
设w=矩阵长度;
设h=矩阵[0]。长度;
//用于保存需要进行的所有替换的数组
让replaceArr=[];
//任何坐标值为'trg'的坐标都会导致该坐标的替换
for(设x=0;x对于(让矩阵的行)console.log(row.join(“”));
您可能想在这里发布此信息,我相信这里有两个错误:您的减速机总是需要返回
a
,并且减速机的初始值应该是
[]
。行应该是:
cols.push(…row.reduce((a,v,I)=>(v==0&&a.push(I,a),[]))
。否则,您的代码会被破坏,说您有一个不可调用的迭代器(因为
reduce
的结果不是数组),我在手机上未经测试就编写了它。您想得对,很好