题解 | #二维数组中的查找#
二维数组中的查找
http://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e
从左到右,从上到下数字会越来越大; 则判断范围应逐渐向左,向当前行收缩; 即每一下次循环应当比上一次循环更少次数的一维数组,每一次循环应当尽可能使循环在本次结束
function Find(target, array)
{
let n = array[0].length;
for(let i = 0; i < array.length; i++){
let tmp = array[i];
for( let j =0; j < n; j++){
if(tmp[j] === target){
return true;
} else if(tmp[j] < target){
continue;
} else {
if (j === 0){
return false;
} else {
n = j;
}
}
}
}
return false;
}
module.exports = {
Find : Find
};