-
Notifications
You must be signed in to change notification settings - Fork 23
/
Search a 2D Matrix II.java
executable file
·168 lines (138 loc) · 4.5 KB
/
Search a 2D Matrix II.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
M
1528169466
tags: Binary Search, Divide and Conquer
给matrix, 每一行sorted, 每一列从上往下sorted, 找target是否存在
#### Binary Search
- 根据给定的性质, 其实点选的极端一点: x = 最下面的row, y = 当下一行里面最小的left position.
- (x,y)在左下角
- 在此情况下, 只能往一个方向运行: 如果小于target, y++; 如果大于target, 那么只能x--
- 每次操作, 都是删掉一行, 或者一列, 再也不需要回头看
- `while (x >= 0 && y < col) {}` 确保不会跑脱
- 同样的方式: 可以从右上角(0, col - 1) 开始, 代码稍微改一改
#### Divide and Conquer?
- TODO
```
/**
LeetCode: check existance
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Example 1:
Input: matrix, target = 5
Output: true
Example 2:
Input: matrix, target = 20
Output: false
*/
// from bottom-left, (x,y) = (row -1, 0)
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int row = matrix.length;
int col = matrix[0].length;
int x = row - 1;
int y = 0;
while (x >= 0 && y < col) {
if (matrix[x][y] < target) {
y++;
} else if (matrix[x][y] > target) {
x--;
} else {//matrix[x][y] == target
return true;
}
}
return false;
}
}
// from top-right, (x,y) = (0, col - 1)
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int row = matrix.length;
int col = matrix[0].length;
int x = 0;
int y = col - 1;
while (x < row && y >= 0) {
if (matrix[x][y] < target) {
x++;
} else if (matrix[x][y] > target) {
y--;
} else {//matrix[x][y] == target
return true;
}
}
return false;
}
}
/*
LintCode: find # of occurances
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of it.
This matrix has the following properties:
* Integers in each row are sorted from left to right.
* Integers in each column are sorted from up to bottom.
* No duplicate integers in each row or column.
Example
Consider the following matrix:
[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]
Given target = 3, return 2.
Challenge
O(m+n) time and O(1) extra space
*/
/*
Thoughts: 11.29.2015. LintCode updated. Practice again.
Understand that:
1. Each row has exactly 1 instance of that integer, no duplicates.
2. Each row begins with smallest number. So, if matrix[x][y] < target, first thing to do is x++.
3. Each col ends with largest number. So if matrix[x][y] > target,
(no need to care x++ since it's alreay too large for this row), then simply just y--.
4. If match, next step will be x--,y++.
x-- because it has to change a row;
y++ because [x-1, y] can't be the target since no duplicate in column
Beneftis of going from bottown-left: No matter which condition, always have 1 possible way to move.
*/
public class Solution {
/**
* @param matrix: A list of lists of integers
* @param: A number you want to search in the matrix
* @return: An integer indicate the occurrence of target in the given matrix
*/
public int searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
int row = matrix.length;
int col = matrix[0].length;
int x = row - 1;
int y = 0;
int count = 0;
while (x >= 0 && y < col) {
if (matrix[x][y] < target) {
y++;
} else if (matrix[x][y] > target) {
x--;
} else {//matrix[x][y] == target
count++;
x--;
y++;
}
}
return count;
}
}
```