行从左到右有序,列从上到下有序
/** * Created by itworker365 on 5/12/2017. */ public class Test { public static void main(String[] args) { int[][] data = new int[5][5]; for (int i = 0; i < data.length; i++) { int base = i; for (int k = 0; k < data[0].length; k++) { data[i][k] = base + k; System.out.print(data[i][k] + " "); } System.out.println(); } Test t = new Test(); boolean result = t.Find(2, data); } public boolean Find(int target, int [][] array) { int arrayHangLength = array.length; int arrayLieLength = array[0].length; boolean result = false; //列反着看,行正着看 for (int i = arrayLieLength - 1; i >= 0; i--) { for (int k = 0; k <= arrayHangLength - 1;) { //当数据小于右上角元素,往左移动列 if (i < 0) { return false; } if (target < array[k][i]) { i--; } else if (target > array[k][i]) { //当数据大于右上角元素,下去一行 k++; } else if (target == array[k][i]) { System.out.println("target find: " + target + " at: " + k + " " + i); result = true; break; } } } return result; } }