Skip to content

Commit feae0f6

Browse files
authored
feat: update solutions to lc/lcof problems (doocs#2861)
1 parent 14c8085 commit feae0f6

37 files changed

+399
-667
lines changed

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

+65-61
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ class Solution {
100100
class Solution {
101101
public:
102102
vector<int> reversePrint(ListNode* head) {
103-
if (!head) return {};
104-
vector<int> ans = reversePrint(head->next);
105-
ans.push_back(head->val);
103+
vector<int> ans;
104+
for (; head; head = head->next) {
105+
ans.push_back(head->val);
106+
}
107+
reverse(ans.begin(), ans.end());
106108
return ans;
107109
}
108110
};
@@ -145,11 +147,11 @@ func reversePrint(head *ListNode) (ans []int) {
145147
*/
146148

147149
function reversePrint(head: ListNode | null): number[] {
148-
let ans: number[] = [];
149-
for (; !!head; head = head.next) {
150-
ans.unshift(head.val);
150+
const ans: number[] = [];
151+
for (; head; head = head.next) {
152+
ans.push(head.val);
151153
}
152-
return ans;
154+
return ans.reverse();
153155
}
154156
```
155157

@@ -174,14 +176,14 @@ function reversePrint(head: ListNode | null): number[] {
174176
// }
175177
impl Solution {
176178
pub fn reverse_print(head: Option<Box<ListNode>>) -> Vec<i32> {
177-
let mut arr: Vec<i32> = vec![];
179+
let mut ans: Vec<i32> = vec![];
178180
let mut cur = head;
179181
while let Some(node) = cur {
180-
arr.push(node.val);
182+
ans.push(node.val);
181183
cur = node.next;
182184
}
183-
arr.reverse();
184-
arr
185+
ans.reverse();
186+
ans
185187
}
186188
}
187189
```
@@ -191,21 +193,21 @@ impl Solution {
191193
```js
192194
/**
193195
* Definition for singly-linked list.
194-
* function ListNode(val) {
195-
* this.val = val;
196-
* this.next = null;
196+
* function ListNode(val, next) {
197+
* this.val = (val===undefined ? 0 : val)
198+
* this.next = (next===undefined ? null : next)
197199
* }
198200
*/
199201
/**
200202
* @param {ListNode} head
201203
* @return {number[]}
202204
*/
203205
var reversePrint = function (head) {
204-
let ans = [];
205-
for (; !!head; head = head.next) {
206-
ans.unshift(head.val);
206+
const ans = [];
207+
for (; head; head = head.next) {
208+
ans.push(head.val);
207209
}
208-
return ans;
210+
return ans.reverse();
209211
};
210212
```
211213

@@ -217,20 +219,49 @@ var reversePrint = function (head) {
217219
* public class ListNode {
218220
* public int val;
219221
* public ListNode next;
220-
* public ListNode(int x) { val = x; }
222+
* public ListNode(int val=0, ListNode next=null) {
223+
* this.val = val;
224+
* this.next = next;
225+
* }
221226
* }
222227
*/
223-
public class Solution {
224-
public int[] ReversePrint(ListNode head) {
225-
List<int> ans = new List<int>();
226-
while (head != null) {
227-
ans.Add(head.val);
228-
head = head.next;
229-
}
230-
ans.Reverse();
231-
return ans.ToArray();
232-
}
233-
}
228+
public class Solution {
229+
public int[] ReversePrint(ListNode head) {
230+
List<int> ans = new List<int>();
231+
for (; head != null; head = head.next) {
232+
ans.Add(head.val);
233+
}
234+
ans.Reverse();
235+
return ans.ToArray();
236+
}
237+
}
238+
```
239+
240+
#### Swift
241+
242+
```swift
243+
/* public class ListNode {
244+
* public var val: Int
245+
* public var next: ListNode?
246+
* public init(_ val: Int) {
247+
* self.val = val
248+
* self.next = nil
249+
* }
250+
* }
251+
*/
252+
253+
class Solution {
254+
func reversePrint(_ head: ListNode?) -> [Int] {
255+
var stack = [Int]()
256+
var current = head
257+
while let node = current {
258+
stack.append(node.val)
259+
current = node.next
260+
}
261+
262+
return stack.reversed()
263+
}
264+
}
234265
```
235266

236267
<!-- tabs:end -->
@@ -308,11 +339,11 @@ class Solution {
308339
class Solution {
309340
public:
310341
vector<int> reversePrint(ListNode* head) {
311-
vector<int> ans;
312-
for (; head; head = head->next) {
313-
ans.push_back(head->val);
342+
if (!head) {
343+
return {};
314344
}
315-
reverse(ans.begin(), ans.end());
345+
vector<int> ans = reversePrint(head->next);
346+
ans.push_back(head->val);
316347
return ans;
317348
}
318349
};
@@ -402,33 +433,6 @@ var reversePrint = function (head) {
402433
};
403434
```
404435

405-
#### Swift
406-
407-
```swift
408-
/* public class ListNode {
409-
* public var val: Int
410-
* public var next: ListNode?
411-
* public init(_ val: Int) {
412-
* self.val = val
413-
* self.next = nil
414-
* }
415-
* }
416-
*/
417-
418-
class Solution {
419-
func reversePrint(_ head: ListNode?) -> [Int] {
420-
var stack = [Int]()
421-
var current = head
422-
while let node = current {
423-
stack.append(node.val)
424-
current = node.next
425-
}
426-
427-
return stack.reversed()
428-
}
429-
}
430-
```
431-
432436
<!-- tabs:end -->
433437

434438
<!-- solution:end -->

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
class Solution {
1010
public:
1111
vector<int> reversePrint(ListNode* head) {
12-
if (!head) return {};
13-
vector<int> ans = reversePrint(head->next);
14-
ans.push_back(head->val);
12+
vector<int> ans;
13+
for (; head; head = head->next) {
14+
ans.push_back(head->val);
15+
}
16+
reverse(ans.begin(), ans.end());
1517
return ans;
1618
}
1719
};

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

+14-12
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
* public class ListNode {
44
* public int val;
55
* public ListNode next;
6-
* public ListNode(int x) { val = x; }
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
710
* }
811
*/
9-
public class Solution {
10-
public int[] ReversePrint(ListNode head) {
11-
List<int> ans = new List<int>();
12-
while (head != null) {
13-
ans.Add(head.val);
14-
head = head.next;
15-
}
16-
ans.Reverse();
17-
return ans.ToArray();
18-
}
19-
}
12+
public class Solution {
13+
public int[] ReversePrint(ListNode head) {
14+
List<int> ans = new List<int>();
15+
for (; head != null; head = head.next) {
16+
ans.Add(head.val);
17+
}
18+
ans.Reverse();
19+
return ans.ToArray();
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/**
22
* Definition for singly-linked list.
3-
* function ListNode(val) {
4-
* this.val = val;
5-
* this.next = null;
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
66
* }
77
*/
88
/**
99
* @param {ListNode} head
1010
* @return {number[]}
1111
*/
1212
var reversePrint = function (head) {
13-
let ans = [];
14-
for (; !!head; head = head.next) {
15-
ans.unshift(head.val);
13+
const ans = [];
14+
for (; head; head = head.next) {
15+
ans.push(head.val);
1616
}
17-
return ans;
17+
return ans.reverse();
1818
};

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
// }
1717
impl Solution {
1818
pub fn reverse_print(head: Option<Box<ListNode>>) -> Vec<i32> {
19-
let mut arr: Vec<i32> = vec![];
19+
let mut ans: Vec<i32> = vec![];
2020
let mut cur = head;
2121
while let Some(node) = cur {
22-
arr.push(node.val);
22+
ans.push(node.val);
2323
cur = node.next;
2424
}
25-
arr.reverse();
26-
arr
25+
ans.reverse();
26+
ans
2727
}
2828
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
*/
1212

1313
function reversePrint(head: ListNode | null): number[] {
14-
let ans: number[] = [];
15-
for (; !!head; head = head.next) {
16-
ans.unshift(head.val);
14+
const ans: number[] = [];
15+
for (; head; head = head.next) {
16+
ans.push(head.val);
1717
}
18-
return ans;
18+
return ans.reverse();
1919
}

lcof/面试题06. 从尾到头打印链表/Solution2.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
class Solution {
1010
public:
1111
vector<int> reversePrint(ListNode* head) {
12-
vector<int> ans;
13-
for (; head; head = head->next) {
14-
ans.push_back(head->val);
12+
if (!head) {
13+
return {};
1514
}
16-
reverse(ans.begin(), ans.end());
15+
vector<int> ans = reversePrint(head->next);
16+
ans.push_back(head->val);
1717
return ans;
1818
}
1919
};

lcof/面试题11. 旋转数组的最小数字/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9
5252

5353
时间复杂度 $(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。
5454

55-
注意,我们也可以每次取中间元素 `numbers[mid]` 与左端元素 `numbers[l]` 比较,但需要考虑当前 $[l,..r]$ 区间内的元素是否已经有序,即是否满足 `numbers[l] < numbers[r]`,如果满足,直接返回 `numbers[l]` 即可。其它情况与上述方法类似。
56-
5755
<!-- tabs:start -->
5856

5957
#### Python3
@@ -234,7 +232,9 @@ class Solution {
234232

235233
<!-- solution:start-->
236234

237-
### 方法二
235+
### 方法二:二分查找(写法二)
236+
237+
注意,我们也可以每次取中间元素 `numbers[mid]` 与左端元素 `numbers[l]` 比较,但需要考虑当前 $[l,..r]$ 区间内的元素是否已经有序,即是否满足 `numbers[l] < numbers[r]`,如果满足,直接返回 `numbers[l]` 即可。其它情况与方法一类似。
238238

239239
<!-- tabs:start -->
240240

0 commit comments

Comments
 (0)