Skip to content

Commit b69c020

Browse files
committed
feat: add solutions to lcof problem: No.04
1 parent a32f227 commit b69c020

File tree

1 file changed

+107
-40
lines changed
  • lcof/面试题04. 二维数组中的查找

1 file changed

+107
-40
lines changed

lcof/面试题04. 二维数组中的查找/README.md

Lines changed: 107 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,40 @@
3838

3939
## 解法
4040

41-
- 换一种观察角度,以右上角位置为基点,往左数值逐渐变小,往下数值逐渐变大。
42-
- 且该角度放在数组任意位置都成立,相当于模拟了一棵**二叉搜索树(Binary Search Tree)**
43-
- 根据二叉搜索树特点,从右上角(或左下角)开始查找即可。
41+
**方法一:二分查找**
42+
43+
由于每一行的所有元素升序排列,因此,对于每一行,我们可以使用二分查找找到第一个大于等于 `target` 的元素,然后判断该元素是否等于 `target`。如果等于 `target`,说明找到了目标值,直接返回 `true`。如果不等于 `target`,说明这一行的所有元素都小于 `target`,应该继续搜索下一行。
44+
45+
如果所有行都搜索完了,都没有找到目标值,说明目标值不存在,返回 `false`
46+
47+
时间复杂度 $O(m \times \log n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
48+
49+
**方法二:从左下角或右上角搜索**
50+
51+
这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 `matrix[i][j]``target` 的大小关系:
52+
53+
-`matrix[i][j] == target`,说明找到了目标值,直接返回 `true`
54+
-`matrix[i][j] > target`,说明这一行从当前位置开始往右的所有元素均大于 `target`,应该让 $i$ 指针往上移动,即 $i \leftarrow i - 1$。
55+
-`matrix[i][j] < target`,说明这一列从当前位置开始往上的所有元素均小于 `target`,应该让 $j$ 指针往右移动,即 $j \leftarrow j + 1$。
56+
57+
若搜索结束依然找不到 `target`,返回 `false`
58+
59+
时间复杂度 $O(m + n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
4460

4561
<!-- tabs:start -->
4662

4763
### **Python3**
4864

65+
```python
66+
class Solution:
67+
def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:
68+
for row in matrix:
69+
j = bisect_left(row, target)
70+
if j < len(matrix[0]) and row[j] == target:
71+
return True
72+
return False
73+
```
74+
4975
```python
5076
class Solution:
5177
def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:
@@ -65,6 +91,20 @@ class Solution:
6591

6692
### **Java**
6793

94+
```java
95+
class Solution {
96+
public boolean findNumberIn2DArray(int[][] matrix, int target) {
97+
for (var row : matrix) {
98+
int j = Arrays.binarySearch(row, target);
99+
if (j >= 0) {
100+
return true;
101+
}
102+
}
103+
return false;
104+
}
105+
}
106+
```
107+
68108
```java
69109
class Solution {
70110
public boolean findNumberIn2DArray(int[][] matrix, int target) {
@@ -87,36 +127,60 @@ class Solution {
87127
}
88128
```
89129

90-
### **JavaScript**
130+
### **C++**
91131

92-
```js
93-
/**
94-
* @param {number[][]} matrix
95-
* @param {number} target
96-
* @return {boolean}
97-
*/
98-
var findNumberIn2DArray = function (matrix, target) {
99-
if (matrix.length == 0 || matrix[0].length == 0) {
132+
```cpp
133+
class Solution {
134+
public:
135+
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
136+
for (auto& row : matrix) {
137+
int j = lower_bound(row.begin(), row.end(), target) - row.begin();
138+
if (j < matrix[0].size() && row[j] == target) {
139+
return true;
140+
}
141+
}
100142
return false;
101143
}
102-
const m = matrix.length;
103-
const n = matrix[0].length;
104-
for (let i = 0, j = n - 1; i < m && j >= 0; ) {
105-
if (matrix[i][j] == target) {
106-
return true;
144+
};
145+
```
146+
147+
```cpp
148+
class Solution {
149+
public:
150+
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
151+
if (matrix.empty()) {
152+
return false;
107153
}
108-
if (matrix[i][j] < target) {
109-
++i;
110-
} else {
111-
--j;
154+
int m = matrix.size(), n = matrix[0].size();
155+
int i = 0, j = n - 1;
156+
while (i < m && j >= 0) {
157+
if (matrix[i][j] == target) {
158+
return true;
159+
} else if (matrix[i][j] < target) {
160+
++i;
161+
} else {
162+
--j;
163+
}
112164
}
165+
return false;
113166
}
114-
return false;
115167
};
116168
```
117169

118170
### **Go**
119171

172+
```go
173+
func findNumberIn2DArray(matrix [][]int, target int) bool {
174+
for _, row := range matrix {
175+
j := sort.SearchInts(row, target)
176+
if j < len(matrix[0]) && row[j] == target {
177+
return true
178+
}
179+
}
180+
return false
181+
}
182+
```
183+
120184
```go
121185
func findNumberIn2DArray(matrix [][]int, target int) bool {
122186
if len(matrix) == 0 {
@@ -137,28 +201,31 @@ func findNumberIn2DArray(matrix [][]int, target int) bool {
137201
}
138202
```
139203

140-
### **C++**
204+
### **JavaScript**
141205

142-
```cpp
143-
class Solution {
144-
public:
145-
bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) {
146-
if (matrix.empty()) {
147-
return false;
206+
```js
207+
/**
208+
* @param {number[][]} matrix
209+
* @param {number} target
210+
* @return {boolean}
211+
*/
212+
var findNumberIn2DArray = function (matrix, target) {
213+
if (matrix.length == 0 || matrix[0].length == 0) {
214+
return false;
215+
}
216+
const m = matrix.length;
217+
const n = matrix[0].length;
218+
for (let i = 0, j = n - 1; i < m && j >= 0; ) {
219+
if (matrix[i][j] == target) {
220+
return true;
148221
}
149-
int m = matrix.size(), n = matrix[0].size();
150-
int i = 0, j = n - 1;
151-
while (i < m && j >= 0) {
152-
if (matrix[i][j] == target) {
153-
return true;
154-
} else if (matrix[i][j] < target) {
155-
++i;
156-
} else {
157-
--j;
158-
}
222+
if (matrix[i][j] < target) {
223+
++i;
224+
} else {
225+
--j;
159226
}
160-
return false;
161227
}
228+
return false;
162229
};
163230
```
164231

0 commit comments

Comments
 (0)