45 lines
1.0 KiB
Java
Executable File
45 lines
1.0 KiB
Java
Executable File
package com.thinker.bishi.offer;
|
|
|
|
/**
|
|
* 剑指offer面试题三。
|
|
* 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成
|
|
*/
|
|
public class Offer_3 {
|
|
|
|
private static int[][] arr= new int[][]{
|
|
{1,2,8,9},
|
|
{2,4,9,12},
|
|
{4,7,10,13},
|
|
{6,8,11,15}
|
|
};
|
|
private static int rows = 0;
|
|
private static int lines = 0;
|
|
|
|
public static void init(){
|
|
lines = arr.length;
|
|
rows = arr[0].length;
|
|
}
|
|
|
|
|
|
public static boolean go(int find){
|
|
boolean flag = false;
|
|
int i = 0,j = rows-1;
|
|
while (i<lines && j>=0){
|
|
if(arr[i][j]==find){
|
|
flag = true;break;
|
|
}else if(arr[i][j] > find){
|
|
j--;
|
|
}else if(arr[i][j] < find){
|
|
i++;
|
|
}
|
|
}
|
|
return flag;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
init();
|
|
boolean exit = go(7);
|
|
System.out.println(exit);
|
|
}
|
|
}
|