forked from kexun/jianzhioffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day01.java
55 lines (41 loc) · 881 Bytes
/
Day01.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.offer;
/**
* 在给定的二维数组中,从左往右,从上往下值依次变大。然后给定一个数值,判断是否保函在此数组中。
* @author kexun
*
*/
public class Day01 {
private static int[][] array = {
{1,2,8,9},
{2,4,9,12},
{4,7,10,13},
{6,8,11,15}
};
private static boolean contains(int key, int[][] array) {
int x = 0;
int y = array[0].length - 1;
int value = array[x][y];
while (value != key) {
if (x>=array.length || y<=0) {
return false;
}
if (value > key) {
y--;
}
if (value < key) {
x++;
}
System.out.println(x + "-----" + y);
value = array[x][y];
}
if (value == key) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
boolean result = contains(3, array);
System.out.println(result);
}
}