從數組的右上角(左下角)開始查找java
左上角同理,就再也不贅述了。數組
public boolean Find(int target, int [][] array) { int rows = array.length - 1,cols = array[0].length - 1,firstrows = 0; boolean flag = false; if(rows<=0 || cols<=0) System.out.println("數組爲空"); else { while(firstrows <= rows && cols >= 0) { if(array[firstrows][cols]>target) --cols; else if(array[firstrows][cols]<target) ++firstrows; else { flag = true; break; } } } if(flag==false) return false; else
return true; }