Skip to content

Commit 8637c02

Browse files
Merge branch 'main' into main
2 parents c26dddb + 42054c2 commit 8637c02

File tree

188 files changed

+17083
-18130
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+17083
-18130
lines changed

solution/0100-0199/0195.Tenth Line/README_EN.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,26 @@ tags:
2323
<p>Assume that <code>file.txt</code> has the following content:</p>
2424

2525
<pre>
26-
2726
Line 1
28-
2927
Line 2
30-
3128
Line 3
32-
3329
Line 4
34-
3530
Line 5
36-
3731
Line 6
38-
3932
Line 7
40-
4133
Line 8
42-
4334
Line 9
44-
4535
Line 10
46-
4736
</pre>
4837

4938
<p>Your script should output the tenth line, which is:</p>
5039

5140
<pre>
52-
5341
Line 10
54-
5542
</pre>
5643

5744
<div class="spoilers"><b>Note:</b><br />
58-
5945
1. If the file contains less than 10 lines, what should you output?<br />
60-
6146
2. There&#39;s at least three different solutions. Try to explore all possibilities.</div>
6247

6348
<!-- description:end -->

solution/0200-0299/0206.Reverse Linked List/README.md

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ tags:
6767

6868
### 方法一:头插法
6969

70-
创建虚拟头节点 $dummy$,遍历链表,将每个节点依次插入 $dummy$ 的下一个节点。遍历结束,返回 $dummy.next$。
70+
我们创建一个虚拟头节点 $\textit{dummy}$,然后遍历链表,将每个节点依次插入 $\textit{dummy}$ 的下一个节点。遍历结束,返回 $\textit{dummy.next}$。
7171

72-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度
72+
时间复杂度 $O(n)$,其中 $n$ 为链表的长度。空间复杂度 $O(1)$。
7373

7474
<!-- tabs:start -->
7575

@@ -279,15 +279,15 @@ var reverseList = function (head) {
279279
*/
280280
public class Solution {
281281
public ListNode ReverseList(ListNode head) {
282-
ListNode pre = null;
283-
for (ListNode p = head; p != null;)
284-
{
285-
ListNode t = p.next;
286-
p.next = pre;
287-
pre = p;
288-
p = t;
282+
ListNode dummy = new ListNode();
283+
ListNode curr = head;
284+
while (curr != null) {
285+
ListNode next = curr.next;
286+
curr.next = dummy.next;
287+
dummy.next = curr;
288+
curr = next;
289289
}
290-
return pre;
290+
return dummy.next;
291291
}
292292
}
293293
```
@@ -466,6 +466,33 @@ impl Solution {
466466
}
467467
```
468468

469+
#### C#
470+
471+
```cs
472+
/**
473+
* Definition for singly-linked list.
474+
* public class ListNode {
475+
* public int val;
476+
* public ListNode next;
477+
* public ListNode(int val=0, ListNode next=null) {
478+
* this.val = val;
479+
* this.next = next;
480+
* }
481+
* }
482+
*/
483+
public class Solution {
484+
public ListNode ReverseList(ListNode head) {
485+
if (head == null || head.next == null) {
486+
return head;
487+
}
488+
ListNode ans = ReverseList(head.next);
489+
head.next.next = head;
490+
head.next = null;
491+
return ans;
492+
}
493+
}
494+
```
495+
469496
<!-- tabs:end -->
470497

471498
<!-- solution:end -->

solution/0200-0299/0206.Reverse Linked List/README_EN.md

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ tags:
5858

5959
<!-- solution:start -->
6060

61-
### Solution 1
61+
### Solution 1: Head Insertion Method
62+
63+
We create a dummy node $\textit{dummy}$, then traverse the linked list and insert each node after the $\textit{dummy}$ node. After traversal, return $\textit{dummy.next}$.
64+
65+
The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.
6266

6367
<!-- tabs:start -->
6468

@@ -268,15 +272,15 @@ var reverseList = function (head) {
268272
*/
269273
public class Solution {
270274
public ListNode ReverseList(ListNode head) {
271-
ListNode pre = null;
272-
for (ListNode p = head; p != null;)
273-
{
274-
ListNode t = p.next;
275-
p.next = pre;
276-
pre = p;
277-
p = t;
275+
ListNode dummy = new ListNode();
276+
ListNode curr = head;
277+
while (curr != null) {
278+
ListNode next = curr.next;
279+
curr.next = dummy.next;
280+
dummy.next = curr;
281+
curr = next;
278282
}
279-
return pre;
283+
return dummy.next;
280284
}
281285
}
282286
```
@@ -287,7 +291,11 @@ public class Solution {
287291

288292
<!-- solution:start -->
289293

290-
### Solution 2
294+
### Solution 2: Recursion
295+
296+
We recursively reverse all nodes from the second node to the end of the list, then attach the $head$ to the end of the reversed list.
297+
298+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the linked list.
291299

292300
<!-- tabs:start -->
293301

@@ -451,6 +459,33 @@ impl Solution {
451459
}
452460
```
453461

462+
#### C#
463+
464+
```cs
465+
/**
466+
* Definition for singly-linked list.
467+
* public class ListNode {
468+
* public int val;
469+
* public ListNode next;
470+
* public ListNode(int val=0, ListNode next=null) {
471+
* this.val = val;
472+
* this.next = next;
473+
* }
474+
* }
475+
*/
476+
public class Solution {
477+
public ListNode ReverseList(ListNode head) {
478+
if (head == null || head.next == null) {
479+
return head;
480+
}
481+
ListNode ans = ReverseList(head.next);
482+
head.next.next = head;
483+
head.next = null;
484+
return ans;
485+
}
486+
}
487+
```
488+
454489
<!-- tabs:end -->
455490

456491
<!-- solution:end -->

solution/0200-0299/0206.Reverse Linked List/Solution.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
*/
1212
public class Solution {
1313
public ListNode ReverseList(ListNode head) {
14-
ListNode pre = null;
15-
for (ListNode p = head; p != null;)
16-
{
17-
ListNode t = p.next;
18-
p.next = pre;
19-
pre = p;
20-
p = t;
14+
ListNode dummy = new ListNode();
15+
ListNode curr = head;
16+
while (curr != null) {
17+
ListNode next = curr.next;
18+
curr.next = dummy.next;
19+
dummy.next = curr;
20+
curr = next;
2121
}
22-
return pre;
22+
return dummy.next;
2323
}
24-
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode ReverseList(ListNode head) {
14+
if (head == null || head.next == null) {
15+
return head;
16+
}
17+
ListNode ans = ReverseList(head.next);
18+
head.next.next = head;
19+
head.next = null;
20+
return ans;
21+
}
22+
}

solution/0200-0299/0234.Palindrome Linked List/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ tags:
6060

6161
我们可以先用快慢指针找到链表的中点,接着反转右半部分的链表。然后同时遍历前后两段链表,若前后两段链表节点对应的值不等,说明不是回文链表,否则说明是回文链表。
6262

63-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度
63+
时间复杂度 $O(n)$,其中 $n$ 为链表的长度。空间复杂度 $O(1)$。
6464

6565
<!-- tabs:start -->
6666

solution/0200-0299/0234.Palindrome Linked List/README_EN.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ tags:
5353

5454
<!-- solution:start -->
5555

56-
### Solution 1
56+
### Solution 1: Fast and Slow Pointers
57+
58+
We can use fast and slow pointers to find the middle of the linked list, then reverse the right half of the list. After that, we traverse both halves simultaneously, checking if the corresponding node values are equal. If any pair of values is unequal, it's not a palindrome linked list; otherwise, it is a palindrome linked list.
59+
60+
The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.
5761

5862
<!-- tabs:start -->
5963

solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/README.md

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,11 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### 方法一:迭代或递归
60+
### 方法一:迭代
6161

62-
从上到下搜索,找到第一个值位于 $[p.val, q.val]$ 之间的结点即可
62+
我们从根节点开始遍历,如果当前节点的值小于 $\textit{p}$ 和 $\textit{q}$ 的值,说明 $\textit{p}$ 和 $\textit{q}$ 应该在当前节点的右子树,因此将当前节点移动到右子节点;如果当前节点的值大于 $\textit{p}$ 和 $\textit{q}$ 的值,说明 $\textit{p}$ 和 $\textit{q}$ 应该在当前节点的左子树,因此将当前节点移动到左子节点;否则说明当前节点就是 $\textit{p}$ 和 $\textit{q}$ 的最近公共祖先,返回当前节点即可
6363

64-
既可以用迭代实现,也可以用递归实现。
65-
66-
迭代的时间复杂度为 $O(n)$,空间复杂度为 $O(1)$。
67-
68-
递归的时间复杂度为 $O(n)$,空间复杂度为 $O(n)$。
64+
时间复杂度 $O(n)$,其中 $n$ 是二叉搜索树的节点个数。空间复杂度 $O(1)$。
6965

7066
<!-- tabs:start -->
7167

@@ -164,9 +160,9 @@ public:
164160
165161
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
166162
for {
167-
if root.Val < p.Val && root.Val < q.Val {
163+
if root.Val < min(p.Val, q.Val) {
168164
root = root.Right
169-
} else if root.Val > p.Val && root.Val > q.Val {
165+
} else if root.Val > max(p.Val, q.Val) {
170166
root = root.Left
171167
} else {
172168
return root
@@ -209,13 +205,47 @@ function lowestCommonAncestor(
209205
}
210206
```
211207

208+
#### C#
209+
210+
```cs
211+
/**
212+
* Definition for a binary tree node.
213+
* public class TreeNode {
214+
* public int val;
215+
* public TreeNode left;
216+
* public TreeNode right;
217+
* public TreeNode(int x) { val = x; }
218+
* }
219+
*/
220+
221+
public class Solution {
222+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
223+
while (true) {
224+
if (root.val < Math.Min(p.val, q.val)) {
225+
root = root.right;
226+
} else if (root.val > Math.Max(p.val, q.val)) {
227+
root = root.left;
228+
} else {
229+
return root;
230+
}
231+
}
232+
}
233+
}
234+
```
235+
212236
<!-- tabs:end -->
213237

214238
<!-- solution:end -->
215239

216240
<!-- solution:start -->
217241

218-
### 方法二
242+
### 方法二:递归
243+
244+
我们也可以使用递归的方法来解决这个问题。
245+
246+
我们首先判断当前节点的值是否小于 $\textit{p}$ 和 $\textit{q}$ 的值,如果是,则递归遍历右子树;如果当前节点的值大于 $\textit{p}$ 和 $\textit{q}$ 的值,如果是,则递归遍历左子树;否则说明当前节点就是 $\textit{p}$ 和 $\textit{q}$ 的最近公共祖先,返回当前节点即可。
247+
248+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉搜索树的节点个数。
219249

220250
<!-- tabs:start -->
221251

@@ -339,12 +369,42 @@ function lowestCommonAncestor(
339369
p: TreeNode | null,
340370
q: TreeNode | null,
341371
): TreeNode | null {
342-
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
343-
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
372+
if (root.val > p.val && root.val > q.val) {
373+
return lowestCommonAncestor(root.left, p, q);
374+
}
375+
if (root.val < p.val && root.val < q.val) {
376+
return lowestCommonAncestor(root.right, p, q);
377+
}
344378
return root;
345379
}
346380
```
347381

382+
#### C#
383+
384+
```cs
385+
/**
386+
* Definition for a binary tree node.
387+
* public class TreeNode {
388+
* public int val;
389+
* public TreeNode left;
390+
* public TreeNode right;
391+
* public TreeNode(int x) { val = x; }
392+
* }
393+
*/
394+
395+
public class Solution {
396+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
397+
if (root.val < Math.Min(p.val, q.val)) {
398+
return LowestCommonAncestor(root.right, p, q);
399+
}
400+
if (root.val > Math.Max(p.val, q.val)) {
401+
return LowestCommonAncestor(root.left, p, q);
402+
}
403+
return root;
404+
}
405+
}
406+
```
407+
348408
<!-- tabs:end -->
349409

350410
<!-- solution:end -->

0 commit comments

Comments
 (0)