Skip to content

Commit 31995c5

Browse files
committed
feat: update solutions to lcof problems
* 面试题04.二维数组中的查找 * 面试题05.替换空格 * 面试题06.从尾到头打印链表
1 parent bef0886 commit 31995c5

File tree

16 files changed

+189
-225
lines changed

16 files changed

+189
-225
lines changed

lcof/面试题03. 数组中重复的数字/README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@
22

33
## 题目描述
44

5-
找出数组中重复的数字。
5+
<p>找出数组中重复的数字。</p>
66

7-
在一个长度为 n 的数组 nums 里的所有数字都在 0 ~ n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
7+
<p><br>
8+
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。</p>
89

9-
**示例 1:**
10+
<p><strong>示例 1:</strong></p>
1011

11-
```
12-
输入:
12+
<pre><strong>输入:</strong>
1313
[2, 3, 1, 0, 2, 5, 3]
14-
输出:2 或 3
15-
```
14+
<strong>输出:</strong>2 或 3
15+
</pre>
1616

17-
**限制:**
17+
<p>&nbsp;</p>
1818

19-
```
20-
2 <= n <= 100000
21-
```
19+
<p><strong>限制:</strong></p>
20+
21+
<p><code>2 &lt;= n &lt;= 100000</code></p>
2222

2323
## 解法
2424

2525
三种方式
2626

27-
- 排序
28-
- 先排序,将相同的数字聚集到一起。
29-
- 再遍历,当位于 `i``i + 1` 的数字相等时,返回该数字。
30-
- 哈希表
31-
- 记录数字在数组中的数量,当数量为 2 时,返回即可。
32-
- 原地交换
33-
- 0 ~ n-1 范围内的数,分别还原到对应的位置上,如:数字 2 交换到下标为 2 的位置。
34-
- 若交换过程中发现重复,则直接返回。
27+
- 排序
28+
- 先排序,将相同的数字聚集到一起。
29+
- 再遍历,当位于 `i``i + 1` 的数字相等时,返回该数字。
30+
- 哈希表
31+
- 记录数字在数组中的数量,当数量为 2 时,返回即可。
32+
- 原地交换
33+
- 0 ~ n-1 范围内的数,分别还原到对应的位置上,如:数字 2 交换到下标为 2 的位置。
34+
- 若交换过程中发现重复,则直接返回。
3535

3636
<!-- tabs:start -->
3737

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

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,45 @@
22

33
## 题目描述
44

5-
在一个 n \* m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
5+
<p>在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。</p>
66

7-
**示例:**
7+
<p> </p>
88

9-
现有矩阵 matrix 如下:
9+
<p><strong>示例:</strong></p>
1010

11-
```
11+
<p>现有矩阵 matrix 如下:</p>
12+
13+
<pre>
1214
[
1315
[1, 4, 7, 11, 15],
1416
[2, 5, 8, 12, 19],
1517
[3, 6, 9, 16, 22],
1618
[10, 13, 14, 17, 24],
1719
[18, 21, 23, 26, 30]
1820
]
19-
```
21+
</pre>
22+
23+
<p>给定 target = <code>5</code>,返回 <code>true</code>。</p>
24+
25+
<p>给定 target = <code>20</code>,返回 <code>false</code>。</p>
2026

21-
给定 target = 5,返回  `true`
27+
<p> </p>
2228

23-
给定  target = 20,返回  `false`
29+
<p><strong>限制:</strong></p>
2430

25-
**限制:**
31+
<p><code>0 <= n <= 1000</code></p>
2632

27-
- `0 <= n <= 1000`
33+
<p><code>0 <= m <= 1000</code></p>
2834

29-
- `0 <= m <= 1000`
35+
<p> </p>
36+
37+
<p><strong>注意:</strong>本题与主站 240 题相同:<a href="https://leetcode-cn.com/problems/search-a-2d-matrix-ii/">https://leetcode-cn.com/problems/search-a-2d-matrix-ii/</a></p>
3038

3139
## 解法
3240

33-
- 换一种观察角度,以右上角位置为基点,往左数值逐渐变小,往下数值逐渐变大。
34-
- 且该角度放在数组任意位置都成立,相当于模拟了一棵**二叉搜索树(Binary Search Tree)**
35-
- 根据二叉搜索树特点,从右上角(或左下角)开始查找即可。
41+
- 换一种观察角度,以右上角位置为基点,往左数值逐渐变小,往下数值逐渐变大。
42+
- 且该角度放在数组任意位置都成立,相当于模拟了一棵**二叉搜索树(Binary Search Tree)**
43+
- 根据二叉搜索树特点,从右上角(或左下角)开始查找即可。
3644

3745
<!-- tabs:start -->
3846

@@ -43,36 +51,35 @@ class Solution:
4351
def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:
4452
if not matrix or not matrix[0]:
4553
return False
46-
rows, cols = len(matrix), len(matrix[0])
47-
i, j = rows - 1, 0
48-
while i >= 0 and j < cols:
54+
m, n = len(matrix), len(matrix[0])
55+
i, j = m - 1, 0
56+
while i >= 0 and j < n:
4957
if matrix[i][j] == target:
5058
return True
5159
if matrix[i][j] > target:
5260
i -= 1
5361
else:
5462
j += 1
5563
return False
56-
5764
```
5865

5966
### **Java**
6067

6168
```java
6269
class Solution {
6370
public boolean findNumberIn2DArray(int[][] matrix, int target) {
64-
int m, n;
65-
if (matrix == null || (m = matrix.length) == 0 || matrix[0] == null || (n = matrix[0].length) == 0) {
71+
if (matrix.length == 0 || matrix[0].length == 0) {
6672
return false;
6773
}
68-
for (int i = 0, j = n - 1; i < m && j >= 0;) {
74+
int m = matrix.length, n = matrix[0].length;
75+
for (int i = m - 1, j = 0; i >= 0 && j < n;) {
6976
if (matrix[i][j] == target) {
7077
return true;
7178
}
72-
if (matrix[i][j] < target) {
73-
++i;
79+
if (matrix[i][j] > target) {
80+
--i;
7481
} else {
75-
--j;
82+
++j;
7683
}
7784
}
7885
return false;
@@ -89,15 +96,11 @@ class Solution {
8996
* @return {boolean}
9097
*/
9198
var findNumberIn2DArray = function (matrix, target) {
92-
let m, n;
93-
if (
94-
matrix == null ||
95-
(m = matrix.length) == 0 ||
96-
matrix[0] == null ||
97-
(n = matrix[0].length) == 0
98-
) {
99+
if (matrix.length == 0 || matrix[0].length == 0) {
99100
return false;
100101
}
102+
const m = matrix.length;
103+
const n = matrix[0].length;
101104
for (let i = 0, j = n - 1; i < m && j >= 0; ) {
102105
if (matrix[i][j] == target) {
103106
return true;
@@ -163,22 +166,23 @@ public:
163166
164167
```ts
165168
function findNumberIn2DArray(matrix: number[][], target: number): boolean {
166-
let m: number = matrix.length,
167-
n: number;
168-
if (!matrix || !m || !matrix[0] || !(n = matrix[0].length)) return false;
169-
let i: number = 0,
170-
j: number = n - 1;
171-
while (i < m && j >= 0) {
172-
let cur: number = matrix[i][j];
173-
if (cur == target) return true;
174-
if (cur > target) {
175-
j--;
169+
if (matrix.length == 0 || matrix[0].length == 0) {
170+
return false;
171+
}
172+
const m = matrix.length;
173+
const n = matrix[0].length;
174+
for (let i = 0, j = n - 1; i < m && j >= 0; ) {
175+
if (matrix[i][j] == target) {
176+
return true;
177+
}
178+
if (matrix[i][j] < target) {
179+
++i;
176180
} else {
177-
i++;
181+
--j;
178182
}
179183
}
180184
return false;
181-
}
185+
};
182186
```
183187

184188
### **Rust**

lcof/面试题04. 二维数组中的查找/Solution.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
class Solution {
22
public boolean findNumberIn2DArray(int[][] matrix, int target) {
3-
int m, n;
4-
if (matrix == null || (m = matrix.length) == 0 || matrix[0] == null || (n = matrix[0].length) == 0) {
3+
if (matrix.length == 0 || matrix[0].length == 0) {
54
return false;
65
}
7-
for (int i = 0, j = n - 1; i < m && j >= 0;) {
6+
int m = matrix.length, n = matrix[0].length;
7+
for (int i = m - 1, j = 0; i >= 0 && j < n;) {
88
if (matrix[i][j] == target) {
99
return true;
1010
}
11-
if (matrix[i][j] < target) {
12-
++i;
11+
if (matrix[i][j] > target) {
12+
--i;
1313
} else {
14-
--j;
14+
++j;
1515
}
1616
}
1717
return false;

lcof/面试题04. 二维数组中的查找/Solution.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44
* @return {boolean}
55
*/
66
var findNumberIn2DArray = function (matrix, target) {
7-
let m, n;
8-
if (
9-
matrix == null ||
10-
(m = matrix.length) == 0 ||
11-
matrix[0] == null ||
12-
(n = matrix[0].length) == 0
13-
) {
7+
if (matrix.length == 0 || matrix[0].length == 0) {
148
return false;
159
}
10+
const m = matrix.length;
11+
const n = matrix[0].length;
1612
for (let i = 0, j = n - 1; i < m && j >= 0; ) {
1713
if (matrix[i][j] == target) {
1814
return true;

lcof/面试题04. 二维数组中的查找/Solution.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ class Solution:
22
def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:
33
if not matrix or not matrix[0]:
44
return False
5-
rows, cols = len(matrix), len(matrix[0])
6-
i, j = rows - 1, 0
7-
while i >= 0 and j < cols:
5+
m, n = len(matrix), len(matrix[0])
6+
i, j = m - 1, 0
7+
while i >= 0 and j < n:
88
if matrix[i][j] == target:
99
return True
1010
if matrix[i][j] > target:
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
function findNumberIn2DArray(matrix: number[][], target: number): boolean {
2-
let m: number = matrix.length,
3-
n: number;
4-
if (!matrix || !m || !matrix[0] || !(n = matrix[0].length)) return false;
5-
let i: number = 0,
6-
j: number = n - 1;
7-
while (i < m && j >= 0) {
8-
let cur: number = matrix[i][j];
9-
if (cur == target) return true;
10-
if (cur > target) {
11-
j--;
2+
if (matrix.length == 0 || matrix[0].length == 0) {
3+
return false;
4+
}
5+
const m = matrix.length;
6+
const n = matrix[0].length;
7+
for (let i = 0, j = n - 1; i < m && j >= 0; ) {
8+
if (matrix[i][j] == target) {
9+
return true;
10+
}
11+
if (matrix[i][j] < target) {
12+
++i;
1213
} else {
13-
i++;
14+
--j;
1415
}
1516
}
1617
return false;
17-
}
18+
};

0 commit comments

Comments
 (0)