Javascript:如何检查每个矩阵的行是否包含0,返回true [英] Javascript: how to check if every matrix's row contains 0, return true

查看:125
本文介绍了Javascript:如何检查每个矩阵的行是否包含0,返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个矩阵`

[

[5,0,0,4],

[0,5,3, 2],

[1,0,5,0],

[2,0,4,2]];

我需要检查是否有任何行包含0,然后返回true,否则返回false。在这个例子中,它必须返回true,因为每行至少有一个0.



我尝试过:



我试过这样,但是错了。



函数F(矩阵){ 
for(var i = 0; i< matrix.length; i ++){
for(var j = 0; j< matrix.length; j ++){
if(matrix [ i] [j] == 0){
返回true;
}
}
}
返回false;
}
console.log(F([[5,0,0,4],[0,5,3,2],[1,0,5,0],[2,0] ,4)]));

解决方案

如果你想检查是否任何,你的示例代码是正确的b> row包含零项。但是你可能想检查每个行是否包含至少一个零项。



那么它可以通过破解来解决(返回false)当不满足所需条件并最终返回true(没有发生中断)并使用每行变量指示当前行是否有一个或多个零项时:

 for ( var  i =  0  ; i <  matrix.length; i ++){
var hasZero = ;
// for(var j = 0; j< matrix.length; j ++){
for var j = 0 ; j < matrix [i] .length; j ++){
if (matrix [i] [j] == 0 ){
hasZero = true ;
break ;
}
}
// 此行中没有项目为零
// 所以中断并返回false
if (!hasZero)
return false ;
}
// 所有行包含至少一个零项
返回 true ;


< blockquote> matrix.length 矩阵中的行数。

您的代码有效因为矩阵具有相同的列数。

Quote:

我尝试过这样,但这是错误的。



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有找到错误,它只是帮助你通过showin你知道发生了什么。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

JavaScript调试 [ ^ ]

Chrome DevTools &NBSP; |&NBSP;网络  |  Google Developers [ ^ ]

这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


I have this matrix`
[
[5,0,0,4],
[0,5,3,2],
[1,0,5,0],
[2,0,4,2]];
I need to check if any row contains 0, then return true, else return false. In this example it must return true, coz in every row there is minimum one 0.

What I have tried:

I have tried like this, but it's wrong.

function F(matrix){
	for(var i = 0; i < matrix.length; i++){
		for(var j = 0; j < matrix.length; j++){
			if(matrix[i][j] == 0){
				return true;
			}
		}
	}
	return false;
}
console.log(F([[5,0,0,4],[0,5,3,2],[1,0,5,0],[2,0,4,2]]));

解决方案

You example code is correct if you want to check if any row contains a zero item. But you probably want to check if every row contains at least one zero item.

Then it can be solved by breaking (returning false) when the required condition is not met and finally returning true (no break occured) and using a variable per row indicating if the current row has one or more items that are zero:

for(var i = 0; i < matrix.length; i++){
    var hasZero = false;
    //for(var j = 0; j < matrix.length; j++){
    for(var j = 0; j < matrix[i].length; j++){
        if(matrix[i][j] == 0){
            hasZero = true;
            break;
        }
    }
    // No item in this row is zero
    // So break and return false
    if (!hasZero)
        return false;
}
// All rows contain at least one item that is zero
return true;


matrix.length is the number of rows in matrix.
Your code works because matrix have same number of columns.

Quote:

I have tried like this, but it's wrong.


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
JavaScript Debugging[^]
Chrome DevTools  |  Web  |  Google Developers[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于Javascript:如何检查每个矩阵的行是否包含0,返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆