Skip to content

Commit 8a5fef7

Browse files
committed
feat: update lcof solutions
1 parent ca02e3f commit 8a5fef7

File tree

7 files changed

+84
-47
lines changed

7 files changed

+84
-47
lines changed

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ class Solution:
4747
```java
4848
class Solution {
4949
public int findRepeatNumber(int[] nums) {
50-
for (int i = 0, len = nums.length; i < len; ++i) {
51-
while (i != nums[i]) {
52-
if (nums[i] == nums[nums[i]]) {
53-
return nums[i];
54-
}
50+
for (int i = 0, n = nums.length; i < n; ++i) {
51+
while (nums[i] != i) {
52+
if (nums[i] == nums[nums[i]]) return nums[i];
5553
swap(nums, i, nums[i]);
5654
}
5755
}

lcof/面试题03. 数组中重复的数字/Solution.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
class Solution {
22
public int findRepeatNumber(int[] nums) {
3-
for (int i = 0, len = nums.length; i < len; ++i) {
4-
while (i != nums[i]) {
5-
if (nums[i] == nums[nums[i]]) {
6-
return nums[i];
7-
}
3+
for (int i = 0, n = nums.length; i < n; ++i) {
4+
while (nums[i] != i) {
5+
if (nums[i] == nums[nums[i]]) return nums[i];
86
swap(nums, i, nums[i]);
97
}
108
}

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

+7-14
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,13 @@ class Solution:
5959
```java
6060
class Solution {
6161
public boolean findNumberIn2DArray(int[][] matrix, int target) {
62-
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
63-
return false;
64-
}
65-
int rows = matrix.length, cols = matrix[0].length;
66-
int i = rows - 1, j = 0;
67-
while (i >= 0 && j < cols) {
68-
if (matrix[i][j] == target) {
69-
return true;
70-
}
71-
if (matrix[i][j] > target) {
72-
--i;
73-
} else {
74-
++j;
75-
}
62+
int m, n;
63+
if (matrix == null || (m = matrix.length) == 0 || matrix[0] == null || (n = matrix[0].length) == 0) return false;
64+
int i = 0, j = n - 1;
65+
while (i < m && j >= 0) {
66+
if (matrix[i][j] == target) return true;
67+
if (matrix[i][j] > target) --j;
68+
else ++i;
7669
}
7770
return false;
7871
}

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

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
class Solution {
22
public boolean findNumberIn2DArray(int[][] matrix, int target) {
3-
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
4-
return false;
5-
}
6-
int rows = matrix.length, cols = matrix[0].length;
7-
int i = rows - 1, j = 0;
8-
while (i >= 0 && j < cols) {
9-
if (matrix[i][j] == target) {
10-
return true;
11-
}
12-
if (matrix[i][j] > target) {
13-
--i;
14-
} else {
15-
++j;
16-
}
3+
int m, n;
4+
if (matrix == null || (m = matrix.length) == 0 || matrix[0] == null || (n = matrix[0].length) == 0) return false;
5+
int i = 0, j = n - 1;
6+
while (i < m && j >= 0) {
7+
if (matrix[i][j] == target) return true;
8+
if (matrix[i][j] > target) --j;
9+
else ++i;
1710
}
1811
return false;
1912
}

lcof/面试题05. 替换空格/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class Solution:
3131

3232
### **Java**
3333

34+
- 使用 replace:
35+
3436
```java
3537
class Solution {
3638
public String replaceSpace(String s) {
@@ -39,6 +41,21 @@ class Solution {
3941
}
4042
```
4143

44+
- 使用 StringBuilder:
45+
46+
```java
47+
class Solution {
48+
public String replaceSpace(String s) {
49+
StringBuilder sb = new StringBuilder();
50+
char[] chars = s.toCharArray();
51+
for (char c : chars) {
52+
sb.append(c == ' ' ? "%20" : c);
53+
}
54+
return sb.toString();
55+
}
56+
}
57+
```
58+
4259
### **JavaScript**
4360

4461
```js

lcof/面试题06. 从尾到头打印链表/README.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
## 解法
1919

20-
栈实现。
20+
栈实现。或者其它方式,见题解。
2121

2222
<!-- tabs:start -->
2323

@@ -41,6 +41,8 @@ class Solution:
4141

4242
### **Java**
4343

44+
- 栈实现:
45+
4446
```java
4547
/**
4648
* Definition for singly-linked list.
@@ -67,6 +69,38 @@ class Solution {
6769
}
6870
```
6971

72+
- 先计算链表长度 n,然后创建一个长度为 n 的结果数组。最后遍历链表,依次将节点值存放在数组上(从后往前)。
73+
74+
```java
75+
/**
76+
* Definition for singly-linked list.
77+
* public class ListNode {
78+
* int val;
79+
* ListNode next;
80+
* ListNode(int x) { val = x; }
81+
* }
82+
*/
83+
class Solution {
84+
public int[] reversePrint(ListNode head) {
85+
if (head == null) return new int[]{};
86+
// 计算链表长度n
87+
int n = 0;
88+
ListNode cur = head;
89+
while (cur != null) {
90+
++n;
91+
cur = cur.next;
92+
}
93+
int[] res = new int[n];
94+
cur = head;
95+
while (cur != null) {
96+
res[--n] = cur.val;
97+
cur = cur.next;
98+
}
99+
return res;
100+
}
101+
}
102+
```
103+
70104
### **Go**
71105

72106
```go

lcof/面试题06. 从尾到头打印链表/Solution.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@
88
*/
99
class Solution {
1010
public int[] reversePrint(ListNode head) {
11-
Stack<Integer> s = new Stack<>();
12-
while (head != null) {
13-
s.push(head.val);
14-
head = head.next;
11+
if (head == null) return new int[]{};
12+
// 计算链表长度n
13+
int n = 0;
14+
ListNode cur = head;
15+
while (cur != null) {
16+
++n;
17+
cur = cur.next;
1518
}
16-
int[] res = new int[s.size()];
17-
int i = 0;
18-
while (!s.isEmpty()) {
19-
res[i++] = s.pop();
19+
int[] res = new int[n];
20+
cur = head;
21+
while (cur != null) {
22+
res[--n] = cur.val;
23+
cur = cur.next;
2024
}
2125
return res;
2226
}

0 commit comments

Comments
 (0)