题目:
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上往下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该函数。
例如下面的二维数组就是每行、每列都是递增排序。如果在这个数组中查找数字7,则返回true,如果查找数组5,由于数组中不含有该数字,则返回false。
1 2 8 9
2 4 9 12
4 7 10 13
6 8 11 15
下面是我实现的代码,修改下类名(Solution)和方法名(Find)通过了牛客网的测试用例
1 public class No2Array{
2 public static void main(String[] args){
3 int[][] array={{1,2,8,9},
4 {2,4,9,12},
5 {4,7,10,13},
6 {6,8,11,15}};
7 System.out.println(findByTheUpperRightCorner(5,array)+"\n");
8 System.out.println(findByTheLowerLeftCorner(7,array));
9 }
10
11 /*
12 * 选取数组查找范围内的右上角的数字,如果该数字等于要查找的数字,则查找结束。
13 * 如果该数字小于要查找的数字,则剔除该数字所在的行;
14 * 如果该数字大于要查找的数字,则剔除该数字所在的列;
15 */
16 public static boolean findByTheUpperRightCorner(int target, int [][] array) {
17 if(array.length>0){
18 int rows=array.length;
19 int columns=array[0].length;
20 int row=0;
21 int column=columns-1;
22 while(row<rows&&column>=0){
23 if(array[row][column]==target)
24 return true;
25 if(array[row][column]>target)
26 column--;
27 else
28 row++;
29 }
31 }
32 return false;
33 }
34
35 /*
36 * 选取数组查找范围内的左下角的数字,如果该数字等于要查找的数字,则查找结束;
37 * 如果该数字小于要查找的数字,则剔除该数字所在的列;
38 * 如果该数字大于要查找的数字,则剔除该数字所在的行;
39 */
40 public static boolean findByTheLowerLeftCorner(int target,int[][] array){
41 if(array.length>0){
42 int rows=array.length;
43 int columns=array[0].length;
44 int row=rows-1;
45 int column=0;
46 while(row>=0&&column<columns){
47 if(array[row][column]==target)
48 return true;
49 if(array[row][column]<target)
50 column++;
51 else
52 row--;
53 }
54 return false;
55 }
56 return false;
57 }
58 }
通过选取右上角和左上角的两种实现思路都给出了实现代码,明白其中一个原理另外一个也就很容易了。关键是不能选取左上角或者右下角的数组作为对比,比如选取左上角的数字小于查找的值,那么该数字的下面数字和右面数字都有可能是要查找的范围,这样无法缩小查找范围。所以只要知道从右上角或者左下角进行查找就很容易解决了。
两种思路:
一种是:把每一行看成有序递增的数组,利用二分查找,通过遍历每一行得到答案,时间复杂度是nlogn
public class Solution {
public boolean Find(int [][]array,int target) {
for(int i=0;i<array.length;i++){
intlow=0;
inthigh=array[i].length-1;
while(low<=high){
intmid=(low+high)/2;
if(target>array[i][mid])
low=mid+1;
elseif(target<array[i][mid])
high=mid-1;
else
returntrue;
}
}
return false;
}
}
另外一种思路是:
利用二维数组由上到下,由左到右递增的规律,
那么选取右上角或者左下角的元素a[row][col]与target进行比较,
当target小于元素a[row][col]时,那么target必定在元素a所在行的左边,
即col--;
当target大于元素a[row][col]时,那么target必定在元素a所在列的下边,
即row++;
public class Solution {
public boolean Find(int [][]array,int target) {
int row=0;
int col=array[0].length-1;
while(row<=array.length-1&&col>=0){
if(target==array[row][col])
returntrue;
elseif(target>array[row][col])
row++;
else
col--;
}
当我们解决一个问题的时候,一个很有效的方法就是从一个具体的问题入手,通过分析简单具体的例子,试图寻找普遍的规律。
[java] view plain copy
- package com.ynu.www.offer;
- public class FindFromMatrix {
- private static int[][] sample = new int[][] { { 1, 2, 8, 9 },
- { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
- public static void printSample() {
- for (int i = 0; i < sample.length; i++) {
- for (int j = 0; j < sample[i].length; j++) {
- System.out.print(sample[i][j] + " ");
- }
- System.out.println();
- }
- }
- public static boolean getValuefromMatrix(int[][] sample, int rows,
- int columns, int num) {
- boolean found = false;
- if (sample != null && rows > 0 && columns > 0) {
- int row = 0;
- int column = columns - 1;
- while (row < rows && column >= 0) {
- int tempValue = sample[row][column];
- if (num > tempValue) {
- ++row;
- } else if (num < tempValue) {
- --column;
- } else {
- found = true;
- break;
- }
- }
- }
- return found;
- }
- public static void main(String[] args) {
- printSample();
- System.out.println(getValuefromMatrix(sample, 4, 4, 7));
- }
- }
文章转自https://blog.csdn.net/qq_35124535/article/details/59481971