Skip to content

Commit 8987620

Browse files
authored
feat: update lc problems (doocs#2914)
1 parent 4de0fe7 commit 8987620

File tree

19 files changed

+335
-226
lines changed

19 files changed

+335
-226
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,14 @@ class Solution {
275275
func reverseList(_ head: ListNode?) -> ListNode? {
276276
let dummy = ListNode(0)
277277
var curr = head
278-
278+
279279
while curr != nil {
280280
let next = curr?.next
281281
curr?.next = dummy.next
282282
dummy.next = curr
283283
curr = next
284284
}
285-
285+
286286
return dummy.next
287287
}
288288
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class Solution {
330330
var cur: ListNode? = dummy
331331
var l1 = l1
332332
var l2 = l2
333-
333+
334334
while let l1Node = l1, let l2Node = l2 {
335335
if l1Node.val <= l2Node.val {
336336
cur?.next = l1Node
@@ -341,9 +341,9 @@ class Solution {
341341
}
342342
cur = cur?.next
343343
}
344-
344+
345345
cur?.next = l1 ?? l2
346-
346+
347347
return dummy.next
348348
}
349349
}

lcof/面试题29. 顺时针打印矩阵/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,14 @@ class Solution {
334334
guard !matrix.isEmpty && !matrix[0].isEmpty else {
335335
return []
336336
}
337-
337+
338338
let m = matrix.count
339339
let n = matrix[0].count
340340
var vis = Array(repeating: Array(repeating: false, count: n), count: m)
341341
var ans = [Int]()
342342
var i = 0, j = 0, k = 0
343343
let dirs = [0, 1, 0, -1, 0]
344-
344+
345345
for _ in 0..<m*n {
346346
ans.append(matrix[i][j])
347347
vis[i][j] = true
@@ -354,7 +354,7 @@ class Solution {
354354
i = x
355355
j = y
356356
}
357-
357+
358358
return ans
359359
}
360360
}

lcof/面试题31. 栈的压入、弹出序列/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,15 @@ class Solution {
215215
func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool {
216216
var stack = [Int]()
217217
var j = 0
218-
218+
219219
for v in pushed {
220220
stack.append(v)
221221
while !stack.isEmpty && stack.last == popped[j] {
222222
stack.removeLast()
223223
j += 1
224224
}
225225
}
226-
226+
227227
return j == pushed.count
228228
}
229229
}

lcof/面试题34. 二叉树中和为某一值的路径/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public class Solution {
399399
* }
400400
* }
401401
*/
402-
402+
403403
class Solution {
404404
private var t = [Int]()
405405
private var ans = [[Int]]()

lcof/面试题35. 复杂链表的复制/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,17 @@ public class Solution {
296296
* public var val: Int
297297
* public var next: Node?
298298
* public var random: Node?
299-
299+
300300
* public init(_ val: Int) {
301301
* self.val = val
302302
* self.next = nil
303303
* self.random = nil
304304
* }
305-
305+
306306
* public static func == (lhs: Node, rhs: Node) -> Bool {
307307
* return lhs === rhs
308308
* }
309-
309+
310310
* public func hash(into hasher: inout Hasher) {
311311
* hasher.combine(ObjectIdentifier(self))
312312
* }
@@ -319,23 +319,23 @@ class Solution {
319319
let dummy = Node(0)
320320
var tail: Node? = dummy
321321
var cur = head
322-
322+
323323
while cur != nil {
324324
tail?.next = Node(cur!.val)
325325
tail = tail?.next
326326
d[cur!] = tail
327327
cur = cur?.next
328328
}
329-
329+
330330
tail = dummy.next
331331
cur = head
332-
332+
333333
while cur != nil {
334334
tail?.random = d[cur!.random ?? Node(0)]
335335
tail = tail?.next
336336
cur = cur?.next
337337
}
338-
338+
339339
return dummy.next
340340
}
341341
}

solution/1300-1399/1366.Rank Teams by Votes/README.md

+16-29
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,35 @@ tags:
3737

3838
<p>&nbsp;</p>
3939

40-
<p><strong>示例 1:</strong></p>
40+
<p><strong class="example">示例 1:</strong></p>
4141

42-
<pre><strong>输入:</strong>votes = [&quot;ABC&quot;,&quot;ACB&quot;,&quot;ABC&quot;,&quot;ACB&quot;,&quot;ACB&quot;]
43-
<strong>输出:</strong>&quot;ACB&quot;
44-
<strong>解释:</strong>A 队获得五票「排位第一」,没有其他队获得「排位第一」,所以 A 队排名第一。
42+
<pre>
43+
<strong>输入:</strong>votes = ["ABC","ACB","ABC","ACB","ACB"]
44+
<strong>输出:</strong>"ACB"
45+
<strong>解释:</strong>
46+
A 队获得五票「排位第一」,没有其他队获得「排位第一」,所以 A 队排名第一。
4547
B 队获得两票「排位第二」,三票「排位第三」。
4648
C 队获得三票「排位第二」,两票「排位第三」。
4749
由于 C 队「排位第二」的票数较多,所以 C 队排第二,B 队排第三。
4850
</pre>
4951

50-
<p><strong>示例 2:</strong></p>
52+
<p><strong class="example">示例 2:</strong></p>
5153

52-
<pre><strong>输入:</strong>votes = [&quot;WXYZ&quot;,&quot;XYZW&quot;]
53-
<strong>输出:</strong>&quot;XWYZ&quot;
54-
<strong>解释:</strong>X 队在并列僵局打破后成为排名第一的团队。X 队和 W 队的「排位第一」票数一样,但是 X 队有一票「排位第二」,而 W 没有获得「排位第二」。
54+
<pre>
55+
<strong>输入:</strong>votes = ["WXYZ","XYZW"]
56+
<strong>输出:</strong>"XWYZ"
57+
<strong>解释:</strong>
58+
X 队在并列僵局打破后成为排名第一的团队。X 队和 W 队的「排位第一」票数一样,但是 X 队有一票「排位第二」,而 W 没有获得「排位第二」。
5559
</pre>
5660

57-
<p><strong>示例 3:</strong></p>
61+
<p><strong class="example">示例 3:</strong></p>
5862

59-
<pre><strong>输入:</strong>votes = [&quot;ZMNAGUEDSJYLBOPHRQICWFXTVK&quot;]
60-
<strong>输出:</strong>&quot;ZMNAGUEDSJYLBOPHRQICWFXTVK&quot;
63+
<pre>
64+
<strong>输入:</strong>votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
65+
<strong>输出:</strong>"ZMNAGUEDSJYLBOPHRQICWFXTVK"
6166
<strong>解释:</strong>只有一个投票者,所以排名完全按照他的意愿。
6267
</pre>
6368

64-
<p><strong>示例 4:</strong></p>
65-
66-
<pre><strong>输入:</strong>votes = [&quot;BCA&quot;,&quot;CAB&quot;,&quot;CBA&quot;,&quot;ABC&quot;,&quot;ACB&quot;,&quot;BAC&quot;]
67-
<strong>输出:</strong>&quot;ABC&quot;
68-
<strong>解释:</strong>
69-
A 队获得两票「排位第一」,两票「排位第二」,两票「排位第三」。
70-
B 队获得两票「排位第一」,两票「排位第二」,两票「排位第三」。
71-
C 队获得两票「排位第一」,两票「排位第二」,两票「排位第三」。
72-
完全并列,所以我们需要按照字母升序排名。
73-
</pre>
74-
75-
<p><strong>示例 5:</strong></p>
76-
77-
<pre><strong>输入:</strong>votes = [&quot;M&quot;,&quot;M&quot;,&quot;M&quot;,&quot;M&quot;]
78-
<strong>输出:</strong>&quot;M&quot;
79-
<strong>解释:</strong>只有 M 队参赛,所以它排名第一。
80-
</pre>
81-
8269
<p>&nbsp;</p>
8370

8471
<p><strong>提示:</strong></p>

solution/2500-2599/2567.Minimum Score by Changing Two Elements/README_EN.md

+31-22
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,48 @@ tags:
2020

2121
<!-- description:start -->
2222

23-
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p>
23+
<p>You are given an integer array <code>nums</code>.</p>
2424

2525
<ul>
26-
<li>The <strong>low</strong> score of <code><font face="monospace">nums</font></code> is the minimum value of <code>|nums[i]&nbsp;- nums[j]|</code> over all <code>0 &lt;= i &lt; j &lt; nums.length</code>.</li>
27-
<li>The <strong>high</strong> score of&nbsp;<code><font face="monospace">nums</font></code> is the maximum value of <code>|nums[i]&nbsp;- nums[j]|</code> over all <code>0 &lt;= i &lt; j &lt; nums.length</code>.</li>
28-
<li>The <strong>score</strong> of <code>nums</code> is the sum of the <strong>high</strong> and <strong>low</strong> scores of nums.</li>
26+
<li>The <strong>low</strong> score of <code>nums</code> is the <strong>minimum</strong> absolute difference between any two integers.</li>
27+
<li>The <strong>high</strong> score of <code>nums</code> is the <strong>maximum</strong> absolute difference between any two integers.</li>
28+
<li>The <strong>score</strong> of <code>nums</code> is the sum of the <strong>high</strong> and <strong>low</strong> scores.</li>
2929
</ul>
3030

31-
<p>To minimize the score of <code>nums</code>, we can change the value of <strong>at most two</strong> elements of <code>nums</code>.</p>
32-
33-
<p>Return <em>the <strong>minimum</strong> possible <strong>score</strong> after changing&nbsp;the value of <strong>at most two</strong> elements o</em>f <code>nums</code>.</p>
34-
35-
<p>Note that <code>|x|</code> denotes the absolute value of <code>x</code>.</p>
31+
<p>Return the <strong>minimum score</strong> after <strong>changing two elements</strong> of <code>nums</code>.</p>
3632

3733
<p>&nbsp;</p>
3834
<p><strong class="example">Example 1:</strong></p>
3935

40-
<pre>
41-
<strong>Input:</strong> nums = [1,4,3]
42-
<strong>Output:</strong> 0
43-
<strong>Explanation:</strong> Change value of nums[1] and nums[2] to 1 so that nums becomes [1,1,1]. Now, the value of <code>|nums[i] - nums[j]|</code> is always equal to 0, so we return 0 + 0 = 0.
44-
</pre>
36+
<div class="example-block">
37+
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,7,8,5]</span></p>
38+
39+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
40+
41+
<p><strong>Explanation:</strong></p>
42+
43+
<ul>
44+
<li>Change <code>nums[0]</code> and <code>nums[1]</code> to be 6 so that <code>nums</code> becomes [6,6,7,8,5].</li>
45+
<li>The low score is the minimum absolute difference: |6 - 6| = 0.</li>
46+
<li>The high score is the maximum absolute difference: |8 - 5| = 3.</li>
47+
<li>The sum of high and low score is 3.</li>
48+
</ul>
49+
</div>
4550

4651
<p><strong class="example">Example 2:</strong></p>
4752

48-
<pre>
49-
<strong>Input:</strong> nums = [1,4,7,8,5]
50-
<strong>Output:</strong> 3
51-
<strong>Explanation:</strong> Change nums[0] and nums[1] to be 6. Now nums becomes [6,6,7,8,5].
52-
Our low score is achieved when i = 0 and j = 1, in which case |<code>nums[i] - nums[j]</code>| = |6 - 6| = 0.
53-
Our high score is achieved when i = 3 and j = 4, in which case |<code>nums[i] - nums[j]</code>| = |8 - 5| = 3.
54-
The sum of our high and low score is 3, which we can prove to be minimal.
55-
</pre>
53+
<div class="example-block">
54+
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,3]</span></p>
55+
56+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
57+
58+
<p><strong>Explanation:</strong></p>
59+
60+
<ul>
61+
<li>Change <code>nums[1]</code> and <code>nums[2]</code> to 1 so that <code>nums</code> becomes [1,1,1].</li>
62+
<li>The sum of maximum absolute difference and minimum absolute difference is 0.</li>
63+
</ul>
64+
</div>
5665

5766
<p>&nbsp;</p>
5867
<p><strong>Constraints:</strong></p>

solution/2600-2699/2612.Minimum Reverse Operations/README_EN.md

+40-25
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,59 @@ tags:
2020

2121
<!-- description:start -->
2222

23-
<p>You are given an integer <code>n</code> and an integer <code>p</code> in the range <code>[<font face="monospace">0</font>, n - 1]</code>. Representing a <strong>0-indexed</strong> array <code>arr</code>&nbsp;of length <code>n</code> where all positions are set to <code>0</code>&#39;s, except position <code>p</code> which is set to <code>1</code>.</p>
24-
25-
<p>You are also given an integer array <code>banned</code> containing some positions from the array. For the <strong>i</strong><sup><strong>th</strong></sup> position in <code>banned</code>, <code>arr[banned[i]] = 0</code>, and <code>banned[i] != p</code>.</p>
26-
27-
<p>You can perform <strong>multiple</strong> operations on <code>arr</code>. In an operation, you can choose a <strong>subarray</strong> with size <code>k</code> and <strong>reverse</strong> the subarray. However, the <code>1</code> in <code>arr</code> should never go to any of the positions in <code>banned</code>. In other words, after each operation <code>arr[banned[i]]</code> <strong>remains</strong> <code>0</code>.</p>
28-
29-
<p><em>Return an array</em> <code>ans</code> <em>where</em><em> for each </em><code>i</code><em> from </em><code>[0, n - 1]</code>, <code>ans[i]</code> <em>is the <strong>minimum</strong> number of reverse operations needed to bring the</em> <code>1</code> <em>to position</em> <code>i</code><em> in arr</em>, <em>or</em> <code>-1</code> <em>if it is impossible</em>.</p>
23+
<p>You are given an integer <code>n</code> and an integer <code>p</code> representing an array <code>arr</code> of length <code>n</code> where all elements are set to 0&#39;s, except position <code>p</code> which is set to 1. You are also given an integer array <code>banned</code> containing restricted positions. Perform the following operation on <code>arr</code>:</p>
3024

3125
<ul>
32-
<li>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</li>
33-
<li>The values of <code>ans[i]</code> are independent for all <code>i</code>&#39;s.</li>
34-
<li>The <strong>reverse </strong>of an array is an array containing the values in <strong>reverse order</strong>.</li>
26+
<li>Reverse a <span data-keyword="subarray-nonempty"><strong>subarray</strong></span> with size <code>k</code> if the single 1 is not set to a position in <code>banned</code>.</li>
3527
</ul>
3628

29+
<p>Return an integer array <code>answer</code> with <code>n</code> results where the <code>i<sup>th</sup></code> result is<em> </em>the <strong>minimum</strong> number of operations needed to bring the single 1 to position <code>i</code> in <code>arr</code>, or -1 if it is impossible.</p>
30+
3731
<p>&nbsp;</p>
3832
<p><strong class="example">Example 1:</strong></p>
3933

40-
<pre>
41-
<strong>Input:</strong> n = 4, p = 0, banned = [1,2], k = 4
42-
<strong>Output:</strong> [0,-1,-1,1]
43-
<strong>Explanation:</strong> In this case <code>k = 4</code> so there is only one possible reverse operation we can perform, which is reversing the whole array. Initially, 1<strong> </strong>is placed at position 0 so the amount of operations we need for position 0 is <code>0</code>. We can never place a 1 on the banned positions, so the answer for positions 1 and 2 is <code>-1</code>. Finally, with one reverse operation we can bring the 1 to index 3, so the answer for position 3 is <code>1</code>.
44-
</pre>
34+
<div class="example-block">
35+
<p><strong>Input:</strong> <span class="example-io">n = 4, p = 0, banned = [1,2], k = 4</span></p>
36+
37+
<p><strong>Output:</strong> <span class="example-io">[0,-1,-1,1]</span></p>
38+
39+
<p><strong>Explanation:</strong></p>
40+
41+
<ul>
42+
<li>Initially 1 is placed at position 0 so the number of operations we need for position 0 is 0.</li>
43+
<li>We can never place 1 on the banned positions, so the answer for positions 1 and 2 is -1.</li>
44+
<li>Perform the operation of size 4 to reverse the whole array.</li>
45+
<li>After a single operation 1 is at position 3 so the answer for position 3 is 1.</li>
46+
</ul>
47+
</div>
4548

4649
<p><strong class="example">Example 2:</strong></p>
4750

48-
<pre>
49-
<strong>Input:</strong> n = 5, p = 0, banned = [2,4], k = 3
50-
<strong>Output:</strong> [0,-1,-1,-1,-1]
51-
<strong>Explanation:</strong> In this case the 1 is initially at position 0, so the answer for that position is <code>0</code>. We can perform reverse operations of size 3. The 1 is currently located at position 0, so we need to reverse the subarray <code>[0, 2]</code> for it to leave that position, but reversing that subarray makes position 2 have a 1, which shouldn&#39;t happen. So, we can&#39;t move the 1 from position 0, making the result for all the other positions <code>-1</code>.
52-
</pre>
51+
<div class="example-block">
52+
<p><strong>Input:</strong> <span class="example-io">n = 5, p = 0, banned = [2,4], k = 3</span></p>
53+
54+
<p><strong>Output:</strong> <span class="example-io">[0,-1,-1,-1,-1]</span></p>
55+
56+
<p><strong>Explanation:</strong></p>
57+
58+
<ul>
59+
<li>Initially 1 is placed at position 0 so the number of operations we need for position 0 is 0.</li>
60+
<li>We cannot perform the operation on the subarray positions <code>[0, 2]</code> because position 2 is in banned.</li>
61+
<li>Because 1 cannot be set at position 2, it is impossible to set 1 at other positions in more operations.</li>
62+
</ul>
63+
</div>
5364

5465
<p><strong class="example">Example 3:</strong></p>
5566

56-
<pre>
57-
<strong>Input:</strong> n = 4, p = 2, banned = [0,1,3], k = 1
58-
<strong>Output:</strong> [-1,-1,0,-1]
59-
<strong>Explanation:</strong> In this case we can only perform reverse operations of size 1.<strong>&nbsp;</strong>So the 1 never changes its position.
60-
</pre>
67+
<div class="example-block">
68+
<p><strong>Input:</strong> <span class="example-io">n = 4, p = 2, banned = [0,1,3], k = 1</span></p>
69+
70+
<p><strong>Output:</strong> <span class="example-io">[-1,-1,0,-1]</span></p>
71+
72+
<p><strong>Explanation:</strong></p>
73+
74+
<p>Perform operations of size 1 and 1 never changes its position.</p>
75+
</div>
6176

6277
<p>&nbsp;</p>
6378
<p><strong>Constraints:</strong></p>

0 commit comments

Comments
 (0)