Skip to content

Commit 2c6a6f0

Browse files
authored
Merge branch 'doocs:main' into main
2 parents 58cbd84 + cc4815a commit 2c6a6f0

File tree

492 files changed

+41112
-31216
lines changed

Some content is hidden

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

492 files changed

+41112
-31216
lines changed

images/starcharts.svg

Lines changed: 24081 additions & 23978 deletions
Loading

lcci/04.01.Route Between Nodes/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public:
126126
for (auto& e : graph) {
127127
g[e[0]].push_back(e[1]);
128128
}
129-
function<bool(int)> dfs = [&](int i) {
129+
auto dfs = [&](this auto&& dfs, int i) -> bool {
130130
if (i == target) {
131131
return true;
132132
}

lcci/04.01.Route Between Nodes/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public:
134134
for (auto& e : graph) {
135135
g[e[0]].push_back(e[1]);
136136
}
137-
function<bool(int)> dfs = [&](int i) {
137+
auto dfs = [&](this auto&& dfs, int i) -> bool {
138138
if (i == target) {
139139
return true;
140140
}

lcci/04.01.Route Between Nodes/Solution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Solution {
66
for (auto& e : graph) {
77
g[e[0]].push_back(e[1]);
88
}
9-
function<bool(int)> dfs = [&](int i) {
9+
auto dfs = [&](this auto&& dfs, int i) -> bool {
1010
if (i == target) {
1111
return true;
1212
}
@@ -23,4 +23,4 @@ class Solution {
2323
};
2424
return dfs(start);
2525
}
26-
};
26+
};

lcci/04.02.Minimum Height Tree/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Solution {
103103
class Solution {
104104
public:
105105
TreeNode* sortedArrayToBST(vector<int>& nums) {
106-
function<TreeNode*(int, int)> dfs = [&](int l, int r) -> TreeNode* {
106+
auto dfs = [&](this auto&& dfs, int l, int r) -> TreeNode* {
107107
if (l > r) {
108108
return nullptr;
109109
}

lcci/04.02.Minimum Height Tree/README_EN.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ Given sorted array: [-10,-3,0,5,9],
2424

2525

2626

27-
One possible answer is: [0,-3,9,-10,null,5],which represents the following tree:
27+
One possible answer is: [0,-3,9,-10,null,5],which represents the following tree:
2828

2929

3030

31-
0
31+
0
3232

33-
/ \
33+
/ \
3434

35-
-3 9
35+
-3 9
3636

37-
/ /
37+
/ /
3838

39-
-10 5
39+
-10 5
4040

4141
</pre>
4242

@@ -127,7 +127,7 @@ class Solution {
127127
class Solution {
128128
public:
129129
TreeNode* sortedArrayToBST(vector<int>& nums) {
130-
function<TreeNode*(int, int)> dfs = [&](int l, int r) -> TreeNode* {
130+
auto dfs = [&](this auto&& dfs, int l, int r) -> TreeNode* {
131131
if (l > r) {
132132
return nullptr;
133133
}

lcci/04.02.Minimum Height Tree/Solution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class Solution {
1111
public:
1212
TreeNode* sortedArrayToBST(vector<int>& nums) {
13-
function<TreeNode*(int, int)> dfs = [&](int l, int r) -> TreeNode* {
13+
auto dfs = [&](this auto&& dfs, int l, int r) -> TreeNode* {
1414
if (l > r) {
1515
return nullptr;
1616
}
@@ -19,4 +19,4 @@ class Solution {
1919
};
2020
return dfs(0, nums.size() - 1);
2121
}
22-
};
22+
};

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md

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

2727
<pre>
2828
<strong>输入: </strong>s = "abcabcbb"
29-
<strong>输出: </strong>3
29+
<strong>输出: </strong>3
3030
<strong>解释:</strong> 因为无重复字符的最长子串是 <code>"abc"</code>,所以其长度为 3。
3131
</pre>
3232

solution/0000-0099/0019.Remove Nth Node From End of List/README.md

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,38 @@ var removeNthFromEnd = function (head, n) {
278278
};
279279
```
280280

281+
#### Swift
282+
283+
````swift
284+
/**
285+
* Definition for singly-linked list.
286+
* public class ListNode {
287+
* public var val: Int
288+
* public var next: ListNode?
289+
* public init() { self.val = 0; self.next = nil; }
290+
* public init(_ val: Int) { self.val = val; self.next = nil; }
291+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
292+
* }
293+
*/
294+
class Solution {
295+
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
296+
let dummy = ListNode(0)
297+
dummy.next = head
298+
var fast: ListNode? = dummy
299+
var slow: ListNode? = dummy
300+
for _ in 0..<n {
301+
fast = fast?.next
302+
}
303+
while fast?.next != nil {
304+
fast = fast?.next
305+
slow = slow?.next
306+
}
307+
slow?.next = slow?.next?.next
308+
return dummy.next
309+
}
310+
}
311+
```
312+
281313
#### Ruby
282314

283315
```rb
@@ -311,43 +343,34 @@ end
311343
#### PHP
312344

313345
```php
314-
# Definition for singly-linked list.
315-
# class ListNode {
316-
# public $val;
317-
# public $next;
318-
319-
# public function __construct($val = 0, $next = null)
320-
# {
321-
# $this->val = $val;
322-
# $this->next = $next;
323-
# }
324-
# }
325-
346+
/**
347+
* Definition for a singly-linked list.
348+
* class ListNode {
349+
* public $val = 0;
350+
* public $next = null;
351+
* function __construct($val = 0, $next = null) {
352+
* $this->val = $val;
353+
* $this->next = $next;
354+
* }
355+
* }
356+
*/
326357
class Solution {
327358
/**
328359
* @param ListNode $head
329-
* @param int $n
360+
* @param Integer $n
330361
* @return ListNode
331362
*/
332-
333363
function removeNthFromEnd($head, $n) {
334-
$dummy = new ListNode(0);
335-
$dummy->next = $head;
336-
337-
$first = $dummy;
338-
$second = $dummy;
339-
340-
for ($i = 0; $i <= $n; $i++) {
341-
$second = $second->next;
364+
$dummy = new ListNode(0, $head);
365+
$fast = $slow = $dummy;
366+
for ($i = 0; $i < $n; $i++) {
367+
$fast = $fast->next;
342368
}
343-
344-
while ($second != null) {
345-
$first = $first->next;
346-
$second = $second->next;
369+
while ($fast->next !== null) {
370+
$fast = $fast->next;
371+
$slow = $slow->next;
347372
}
348-
349-
$first->next = $first->next->next;
350-
373+
$slow->next = $slow->next->next;
351374
return $dummy->next;
352375
}
353376
}
@@ -358,3 +381,4 @@ class Solution {
358381
<!-- solution:end -->
359382

360383
<!-- problem:end -->
384+
````

solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,38 @@ var removeNthFromEnd = function (head, n) {
275275
};
276276
```
277277

278+
#### Swift
279+
280+
````swift
281+
/**
282+
* Definition for singly-linked list.
283+
* public class ListNode {
284+
* public var val: Int
285+
* public var next: ListNode?
286+
* public init() { self.val = 0; self.next = nil; }
287+
* public init(_ val: Int) { self.val = val; self.next = nil; }
288+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
289+
* }
290+
*/
291+
class Solution {
292+
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
293+
let dummy = ListNode(0)
294+
dummy.next = head
295+
var fast: ListNode? = dummy
296+
var slow: ListNode? = dummy
297+
for _ in 0..<n {
298+
fast = fast?.next
299+
}
300+
while fast?.next != nil {
301+
fast = fast?.next
302+
slow = slow?.next
303+
}
304+
slow?.next = slow?.next?.next
305+
return dummy.next
306+
}
307+
}
308+
```
309+
278310
#### Ruby
279311

280312
```rb
@@ -308,43 +340,34 @@ end
308340
#### PHP
309341

310342
```php
311-
# Definition for singly-linked list.
312-
# class ListNode {
313-
# public $val;
314-
# public $next;
315-
316-
# public function __construct($val = 0, $next = null)
317-
# {
318-
# $this->val = $val;
319-
# $this->next = $next;
320-
# }
321-
# }
322-
343+
/**
344+
* Definition for a singly-linked list.
345+
* class ListNode {
346+
* public $val = 0;
347+
* public $next = null;
348+
* function __construct($val = 0, $next = null) {
349+
* $this->val = $val;
350+
* $this->next = $next;
351+
* }
352+
* }
353+
*/
323354
class Solution {
324355
/**
325356
* @param ListNode $head
326-
* @param int $n
357+
* @param Integer $n
327358
* @return ListNode
328359
*/
329-
330360
function removeNthFromEnd($head, $n) {
331-
$dummy = new ListNode(0);
332-
$dummy->next = $head;
333-
334-
$first = $dummy;
335-
$second = $dummy;
336-
337-
for ($i = 0; $i <= $n; $i++) {
338-
$second = $second->next;
361+
$dummy = new ListNode(0, $head);
362+
$fast = $slow = $dummy;
363+
for ($i = 0; $i < $n; $i++) {
364+
$fast = $fast->next;
339365
}
340-
341-
while ($second != null) {
342-
$first = $first->next;
343-
$second = $second->next;
366+
while ($fast->next !== null) {
367+
$fast = $fast->next;
368+
$slow = $slow->next;
344369
}
345-
346-
$first->next = $first->next->next;
347-
370+
$slow->next = $slow->next->next;
348371
return $dummy->next;
349372
}
350373
}
@@ -355,3 +378,4 @@ class Solution {
355378
<!-- solution:end -->
356379

357380
<!-- problem:end -->
381+
````

0 commit comments

Comments
 (0)