在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
一,二分查找(失败)
// public static boolean Find(int target, int [][] array) {// System.out.println("aaaaaaaaaaaa");// int rowMax = array.length -1;// int rowMin = 0;// int columnMax = array[0].length-1;//// while(rowMin < rowMax){// int rowMiddle = (rowMax + rowMin)/2;//// if (array[rowMiddle][0]== target||array[rowMiddle][columnMax]==target){// return true;// }else if (array[rowMiddle][0]<target&&array[rowMiddle][columnMax]>target){// int columnMin = 0;// while (columnMin <columnMax){// int columnMiddle = (columnMax +columnMin)/2;// if (array[rowMiddle][columnMiddle]==target){// return true;// }else if (array[rowMiddle][columnMiddle]>target){// columnMax =columnMiddle;// }else if (array[rowMiddle][columnMiddle]<target){// columnMin =columnMiddle;// }// } 如果没有,怎么调整 --》宣告失败// }else if (array[rowMiddle][0]>target){// rowMax = rowMiddle;// }else if (array[rowMiddle][columnMax]<target){// rowMin = rowMiddle;// }// }// return false;// }二 . 二分查找(成功) public static boolean Find(int target, int [][] array) { int row = array.length - 1; while (row >= 0) { int columnMax = array[row].length - 1; if(columnMax<0) { return false; } if (array[row][columnMax] >= target || array[row][0] <= target) { int columnMin = 0; while (columnMin <= columnMax&&columnMax>=0) { int columnMiddle = (columnMax + columnMin) / 2; if (array[row][columnMiddle] == target) { return true; } else if (array[row][columnMiddle] < target) { columnMin = columnMiddle + 1; } else if (array[row][columnMiddle] > target) { columnMax = columnMiddle - 1; } } } row --; } return false;}转载于:https://www.cnblogs.com/kaibing/p/8980211.html
相关资源:css3-animation