題目描述
在一個二維數組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。
測試用例:
7,[[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]
對應輸出應該爲:
true
最終源碼:
public class Solution {
public static boolean Find(int target, int [][] array) {
int rCount = array.length;//獲取矩陣行數
int cCount = array[rCount].length;//獲取矩陣列數
//從左下角進行比較,如果小就往上走,如果大就往右走,直到找到target,或是返回false
for(int i = rCount-1, j = 0;i>=0&&j<cCount;){
if (target == array[i][j]) {
return true;
}else if(target < array[i][j]){
i--;
continue;
}else if (target > array[i][j]) {
j++;
continue;
}
}
return false;
}
}