题解 | #二维数组中的查找#
二维数组中的查找
https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e
export function Find(target: number, array: number[][]): boolean { // write code here let rowLen = array[0].length ; let columnLen = array.length; for(let i = 0 ; i < columnLen ; i ++){ if(target < array[i][0] || target > array[i][rowLen-1])continue; let low:number = 0; let high:number = rowLen-1; let div:number = 0; while(low <= high){ div = Math.floor((high + low)/2); if(target == array[i][div])return true; else if(target < array[i][div]){ high = div-1; }else low = div+1; } } return false }
本题所用的方法主要为二分查找法的思想,同时判断根据某个一维数组的最值判断target是否在数组范围内实现优化