Skip to content

Commit 961d5d6

Browse files
committed
feat: update solutions to lcof problems
1 parent 4604d21 commit 961d5d6

File tree

12 files changed

+126
-123
lines changed

12 files changed

+126
-123
lines changed

lcof/面试题21. 调整数组顺序使奇数位于偶数前面/README.md

+27-17
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,51 @@
1919

2020
## 解法
2121

22+
双指针。
23+
2224
<!-- tabs:start -->
2325

2426
### **Python3**
2527

2628
```python
2729
class Solution:
2830
def exchange(self, nums: List[int]) -> List[int]:
29-
res = [0 for _ in range(len(nums))]
3031
p, q = 0, len(nums) - 1
31-
for e in nums:
32-
if (e & 1) == 0:
33-
res[q] = e
34-
q -= 1
35-
else:
36-
res[p] = e
32+
while p < q:
33+
if nums[p] & 1 == 1:
3734
p += 1
38-
return res
35+
continue
36+
if nums[q] & 1 == 0:
37+
q -= 1
38+
continue
39+
nums[p], nums[q] = nums[q], nums[p]
40+
return nums
3941
```
4042

4143
### **Java**
4244

4345
```java
4446
class Solution {
4547
public int[] exchange(int[] nums) {
46-
int len = nums.length;
47-
int[] res = new int[len];
48-
int p = 0, q = len - 1;
49-
for (int e : nums) {
50-
if ((e & 1) == 0) {
51-
res[q--] = e;
52-
} else {
53-
res[p++] = e;
48+
int p = 0, q = nums.length - 1;
49+
while (p < q) {
50+
if ((nums[p] & 1) == 1) {
51+
++p;
52+
continue;
5453
}
54+
if ((nums[q] & 1) == 0) {
55+
--q;
56+
continue;
57+
}
58+
swap(nums, p, q);
5559
}
56-
return res;
60+
return nums;
61+
}
62+
63+
private void swap(int[] nums, int p, int q) {
64+
int t = nums[p];
65+
nums[p] = nums[q];
66+
nums[q] = t;
5767
}
5868
}
5969
```
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
class Solution {
22
public int[] exchange(int[] nums) {
3-
int len = nums.length;
4-
int[] res = new int[len];
5-
int p = 0, q = len - 1;
6-
for (int e : nums) {
7-
if ((e & 1) == 0) {
8-
res[q--] = e;
9-
} else {
10-
res[p++] = e;
3+
int p = 0, q = nums.length - 1;
4+
while (p < q) {
5+
if ((nums[p] & 1) == 1) {
6+
++p;
7+
continue;
118
}
9+
if ((nums[q] & 1) == 0) {
10+
--q;
11+
continue;
12+
}
13+
swap(nums, p, q);
1214
}
13-
return res;
15+
return nums;
16+
}
17+
18+
private void swap(int[] nums, int p, int q) {
19+
int t = nums[p];
20+
nums[p] = nums[q];
21+
nums[q] = t;
1422
}
1523
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution:
22
def exchange(self, nums: List[int]) -> List[int]:
3-
res = [0 for _ in range(len(nums))]
43
p, q = 0, len(nums) - 1
5-
for e in nums:
6-
if (e & 1) == 0:
7-
res[q] = e
8-
q -= 1
9-
else:
10-
res[p] = e
4+
while p < q:
5+
if nums[p] & 1 == 1:
116
p += 1
12-
return res
7+
continue
8+
if nums[q] & 1 == 0:
9+
q -= 1
10+
continue
11+
nums[p], nums[q] = nums[q], nums[p]
12+
return nums

lcof/面试题22. 链表中倒数第k个节点/README.md

+14-21
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
## 解法
1414

15-
定义 `p``q` 指针指向 `head`
15+
定义快慢指针 `slow``fast`,初始指向 `head`
1616

17-
`p` 先向前走 `k` 步,接着 `p``q` 同时向前走,当 `p` 指向 `null` 时,`q` 指向的节点即为链表的倒数第 `k` 个节点。
17+
`fast` 先向前走 `k` 步,接着 `slow``fast` 同时向前走,当 `fast` 指向 `null` 时,`slow` 指向的节点即为链表的倒数第 `k` 个节点。
1818

1919
<!-- tabs:start -->
2020

@@ -29,17 +29,13 @@
2929

3030
class Solution:
3131
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
32-
if not (head or head.next):
33-
return head
34-
35-
p = q = head
32+
slow = fast = head
3633
for _ in range(k):
37-
p = p.next
38-
while p:
39-
p = p.next
40-
q = q.next
41-
return q
42-
34+
fast = fast.next
35+
while fast:
36+
slow = slow.next
37+
fast = fast.next
38+
return slow
4339
```
4440

4541
### **Java**
@@ -55,18 +51,15 @@ class Solution:
5551
*/
5652
class Solution {
5753
public ListNode getKthFromEnd(ListNode head, int k) {
58-
if (head == null || head.next == null) {
59-
return head;
60-
}
61-
ListNode p = head, q = head;
54+
ListNode slow = head, fast = head;
6255
while (k-- > 0) {
63-
p = p.next;
56+
fast = fast.next;
6457
}
65-
while (p != null) {
66-
p = p.next;
67-
q = q.next;
58+
while (fast != null) {
59+
slow = slow.next;
60+
fast = fast.next;
6861
}
69-
return q;
62+
return slow;
7063
}
7164
}
7265
```

lcof/面试题22. 链表中倒数第k个节点/Solution.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@
88
*/
99
class Solution {
1010
public ListNode getKthFromEnd(ListNode head, int k) {
11-
if (head == null || head.next == null) {
12-
return head;
13-
}
14-
ListNode p = head, q = head;
11+
ListNode slow = head, fast = head;
1512
while (k-- > 0) {
16-
p = p.next;
13+
fast = fast.next;
1714
}
18-
while (p != null) {
19-
p = p.next;
20-
q = q.next;
15+
while (fast != null) {
16+
slow = slow.next;
17+
fast = fast.next;
2118
}
22-
return q;
19+
return slow;
2320
}
2421
}

lcof/面试题22. 链表中倒数第k个节点/Solution.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66

77
class Solution:
88
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
9-
if not (head or head.next):
10-
return head
11-
12-
p = q = head
9+
slow = fast = head
1310
for _ in range(k):
14-
p = p.next
15-
while p:
16-
p = p.next
17-
q = q.next
18-
return q
11+
fast = fast.next
12+
while fast:
13+
slow = slow.next
14+
fast = fast.next
15+
return slow

lcof/面试题24. 反转链表/README.md

+14-15
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
## 解法
1919

20-
定义指针 `p``q` 分别指向头节点和下一个节点,`pre` 指向头节点的前一个节点
20+
定义指针 `pre``cur` 分别指向 null 和头节点
2121

22-
遍历链表,改变指针 `p` 指向的节点的指向,将其指向 `pre` 指针指向的节点,即 `p.next = pre`。然后 `pre` 指针指向 `p``p``q` 指针往前走。
22+
遍历链表,`cur.next` 临时保存到 `t` 中,然后改变指针 `cur` 指向的节点的指向,将其指向 `pre` 指针指向的节点,即 `cur.next = pre`。然后 `pre` 指针指向 `cur``cur` 指针往前走。
2323

2424
当遍历结束后,返回 `pre` 指针即可。
2525

@@ -36,12 +36,12 @@
3636

3737
class Solution:
3838
def reverseList(self, head: ListNode) -> ListNode:
39-
pre, p = None, head
40-
while p:
41-
q = p.next
42-
p.next = pre
43-
pre = p
44-
p = q
39+
pre, cur = None, head
40+
while cur:
41+
t = cur.next
42+
cur.next = pre
43+
pre = cur
44+
cur = t
4545
return pre
4646
```
4747

@@ -58,13 +58,12 @@ class Solution:
5858
*/
5959
class Solution {
6060
public ListNode reverseList(ListNode head) {
61-
ListNode pre = null;
62-
ListNode p = head;
63-
while (p != null) {
64-
ListNode q = p.next;
65-
p.next = pre;
66-
pre = p;
67-
p = q;
61+
ListNode pre = null, cur = head;
62+
while (cur != null) {
63+
ListNode t = cur.next;
64+
cur.next = pre;
65+
pre = cur;
66+
cur = t;
6867
}
6968
return pre;
7069
}

lcof/面试题24. 反转链表/Solution.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
*/
99
class Solution {
1010
public ListNode reverseList(ListNode head) {
11-
ListNode pre = null;
12-
ListNode p = head;
13-
while (p != null) {
14-
ListNode q = p.next;
15-
p.next = pre;
16-
pre = p;
17-
p = q;
11+
ListNode pre = null, cur = head;
12+
while (cur != null) {
13+
ListNode t = cur.next;
14+
cur.next = pre;
15+
pre = cur;
16+
cur = t;
1817
}
1918
return pre;
2019
}

lcof/面试题24. 反转链表/Solution.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
class Solution:
88
def reverseList(self, head: ListNode) -> ListNode:
9-
pre, p = None, head
10-
while p:
11-
q = p.next
12-
p.next = pre
13-
pre = p
14-
p = q
9+
pre, cur = None, head
10+
while cur:
11+
t = cur.next
12+
cur.next = pre
13+
pre = cur
14+
cur = t
1515
return pre

lcof/面试题25. 合并两个排序的链表/README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@
3232

3333
class Solution:
3434
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
35-
dummy = ListNode()
36-
cur = dummy
35+
dummy = ListNode(0)
36+
p = dummy
3737
while l1 and l2:
3838
if l1.val <= l2.val:
39-
cur.next = l1
39+
p.next = l1
4040
l1 = l1.next
4141
else:
42-
cur.next = l2
42+
p.next = l2
4343
l2 = l2.next
44-
cur = cur.next
45-
cur.next = l1 or l2
44+
p = p.next
45+
p.next = l1 or l2
4646
return dummy.next
4747
```
4848

@@ -60,18 +60,18 @@ class Solution:
6060
class Solution {
6161
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
6262
ListNode dummy = new ListNode(0);
63-
ListNode cur = dummy;
63+
ListNode p = dummy;
6464
while (l1 != null && l2 != null) {
6565
if (l1.val <= l2.val) {
66-
cur.next = l1;
66+
p.next = l1;
6767
l1 = l1.next;
6868
} else {
69-
cur.next = l2;
69+
p.next = l2;
7070
l2 = l2.next;
7171
}
72-
cur = cur.next;
72+
p = p.next;
7373
}
74-
cur.next = l1 == null ? l2 : l1;
74+
p.next = l1 == null ? l2 : l1;
7575
return dummy.next;
7676
}
7777
}

lcof/面试题25. 合并两个排序的链表/Solution.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
class Solution {
1010
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
1111
ListNode dummy = new ListNode(0);
12-
ListNode cur = dummy;
12+
ListNode p = dummy;
1313
while (l1 != null && l2 != null) {
1414
if (l1.val <= l2.val) {
15-
cur.next = l1;
15+
p.next = l1;
1616
l1 = l1.next;
1717
} else {
18-
cur.next = l2;
18+
p.next = l2;
1919
l2 = l2.next;
2020
}
21-
cur = cur.next;
21+
p = p.next;
2222
}
23-
cur.next = l1 == null ? l2 : l1;
23+
p.next = l1 == null ? l2 : l1;
2424
return dummy.next;
2525
}
2626
}

0 commit comments

Comments
 (0)