diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md index 30a0694ac6393..4c0f519bc0dc4 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md @@ -15,7 +15,7 @@
 输入: s = "abcabcbb"
 输出: 3 
-解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
+解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
 

示例 2:

@@ -23,7 +23,7 @@
 输入: s = "bbbbb"
 输出: 1
-解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
+解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
 

示例 3:

@@ -31,8 +31,8 @@
 输入: s = "pwwkew"
 输出: 3
-解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
-     请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
+解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
+请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
 

 

diff --git a/solution/0000-0099/0006.Zigzag Conversion/README.md b/solution/0000-0099/0006.Zigzag Conversion/README.md index a5668dd8a258c..86415cacdbde7 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README.md @@ -211,7 +211,7 @@ func convert(s string, numRows int) string { */ var convert = function (s, numRows) { if (numRows == 1) return s; - let arr = new Array(numRows); + const arr = new Array(numRows); for (let i = 0; i < numRows; i++) arr[i] = []; let mi = 0, isDown = true; @@ -225,7 +225,7 @@ var convert = function (s, numRows) { else mi--; } let ans = []; - for (let item of arr) { + for (const item of arr) { ans = ans.concat(item); } return ans.join(''); diff --git a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md index 471c1be5b271f..303851237fa12 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md @@ -205,7 +205,7 @@ func convert(s string, numRows int) string { */ var convert = function (s, numRows) { if (numRows == 1) return s; - let arr = new Array(numRows); + const arr = new Array(numRows); for (let i = 0; i < numRows; i++) arr[i] = []; let mi = 0, isDown = true; @@ -219,7 +219,7 @@ var convert = function (s, numRows) { else mi--; } let ans = []; - for (let item of arr) { + for (const item of arr) { ans = ans.concat(item); } return ans.join(''); diff --git a/solution/0000-0099/0006.Zigzag Conversion/Solution.js b/solution/0000-0099/0006.Zigzag Conversion/Solution.js index c6d4dcd721ae1..e291e38cb6b18 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/Solution.js +++ b/solution/0000-0099/0006.Zigzag Conversion/Solution.js @@ -5,7 +5,7 @@ */ var convert = function (s, numRows) { if (numRows == 1) return s; - let arr = new Array(numRows); + const arr = new Array(numRows); for (let i = 0; i < numRows; i++) arr[i] = []; let mi = 0, isDown = true; @@ -19,7 +19,7 @@ var convert = function (s, numRows) { else mi--; } let ans = []; - for (let item of arr) { + for (const item of arr) { ans = ans.concat(item); } return ans.join(''); diff --git a/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md b/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md index 5763f2f1203bb..177b710b7ed04 100644 --- a/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md +++ b/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md @@ -19,14 +19,14 @@

示例 1:

-输入:nums = [4,5,6,7,0,1,2], target = 0
+输入:nums = [4,5,6,7,0,1,2], target = 0
 输出:4
 

示例 2:

-输入:nums = [4,5,6,7,0,1,2], target = 3
+输入:nums = [4,5,6,7,0,1,2], target = 3
 输出:-1

示例 3:

diff --git a/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md b/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md index a1550b28c8c39..f8d23d79a642a 100644 --- a/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md +++ b/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md @@ -17,13 +17,13 @@

示例 1:

-输入:nums = [5,7,7,8,8,10], target = 8
+输入:nums = [5,7,7,8,8,10], target = 8
 输出:[3,4]

示例 2:

-输入:nums = [5,7,7,8,8,10], target = 6
+输入:nums = [5,7,7,8,8,10], target = 6
 输出:[-1,-1]

示例 3:

diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/README.md b/solution/1000-1099/1040.Moving Stones Until Consecutive II/README.md index 30f3ef4cb32db..7081c8369522d 100644 --- a/solution/1000-1099/1040.Moving Stones Until Consecutive II/README.md +++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/README.md @@ -61,6 +61,10 @@ +**方法一:排序 + 分类讨论 + 双指针** + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `stones` 的长度。 + ### **Python3** @@ -68,7 +72,20 @@ ```python - +class Solution: + def numMovesStonesII(self, stones: List[int]) -> List[int]: + stones.sort() + mi = n = len(stones) + mx = max(stones[-1] - stones[1] + 1, stones[-2] - stones[0] + 1) - (n - 1) + i = 0 + for j, x in enumerate(stones): + while x - stones[i] + 1 > n: + i += 1 + if j - i + 1 == n - 1 and x - stones[i] == n - 2: + mi = min(mi, 2) + else: + mi = min(mi, n - (j - i + 1)) + return [mi, mx] ``` ### **Java** @@ -76,7 +93,111 @@ ```java +class Solution { + public int[] numMovesStonesII(int[] stones) { + Arrays.sort(stones); + int n = stones.length; + int mi = n; + int mx = Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1); + for (int i = 0, j = 0; j < n; ++j) { + while (stones[j] - stones[i] + 1 > n) { + ++i; + } + if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) { + mi = Math.min(mi, 2); + } else { + mi = Math.min(mi, n - (j - i + 1)); + } + } + return new int[] {mi, mx}; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + vector numMovesStonesII(vector& stones) { + sort(stones.begin(), stones.end()); + int n = stones.size(); + int mi = n; + int mx = max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1); + for (int i = 0, j = 0; j < n; ++j) { + while (stones[j] - stones[i] + 1 > n) { + ++i; + } + if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) { + mi = min(mi, 2); + } else { + mi = min(mi, n - (j - i + 1)); + } + } + return {mi, mx}; + } +}; +``` + +### **Go** + +```go +func numMovesStonesII(stones []int) []int { + sort.Ints(stones) + n := len(stones) + mi := n + mx := max(stones[n-1]-stones[1]+1, stones[n-2]-stones[0]+1) - (n - 1) + i := 0 + for j, x := range stones { + for x-stones[i]+1 > n { + i++ + } + if j-i+1 == n-1 && stones[j]-stones[i] == n-2 { + mi = min(mi, 2) + } else { + mi = min(mi, n-(j-i+1)) + } + } + return []int{mi, mx} +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} +``` +### **TypeScript** + +```ts +function numMovesStonesII(stones: number[]): number[] { + stones.sort((a, b) => a - b); + const n = stones.length; + let mi = n; + const mx = + Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - + (n - 1); + for (let i = 0, j = 0; j < n; ++j) { + while (stones[j] - stones[i] + 1 > n) { + ++i; + } + if (j - i + 1 === n - 1 && stones[j] - stones[i] === n - 2) { + mi = Math.min(mi, 2); + } else { + mi = Math.min(mi, n - (j - i + 1)); + } + } + return [mi, mx]; +} ``` ### **...** diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/README_EN.md b/solution/1000-1099/1040.Moving Stones Until Consecutive II/README_EN.md index 7ad960864fe1b..ebfd2b254cf93 100644 --- a/solution/1000-1099/1040.Moving Stones Until Consecutive II/README_EN.md +++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/README_EN.md @@ -57,13 +57,130 @@ Notice we cannot move 10 -> 2 to finish the game, because that would be an il ### **Python3** ```python - +class Solution: + def numMovesStonesII(self, stones: List[int]) -> List[int]: + stones.sort() + mi = n = len(stones) + mx = max(stones[-1] - stones[1] + 1, stones[-2] - stones[0] + 1) - (n - 1) + i = 0 + for j, x in enumerate(stones): + while x - stones[i] + 1 > n: + i += 1 + if j - i + 1 == n - 1 and x - stones[i] == n - 2: + mi = min(mi, 2) + else: + mi = min(mi, n - (j - i + 1)) + return [mi, mx] ``` ### **Java** ```java +class Solution { + public int[] numMovesStonesII(int[] stones) { + Arrays.sort(stones); + int n = stones.length; + int mi = n; + int mx = Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1); + for (int i = 0, j = 0; j < n; ++j) { + while (stones[j] - stones[i] + 1 > n) { + ++i; + } + if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) { + mi = Math.min(mi, 2); + } else { + mi = Math.min(mi, n - (j - i + 1)); + } + } + return new int[] {mi, mx}; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + vector numMovesStonesII(vector& stones) { + sort(stones.begin(), stones.end()); + int n = stones.size(); + int mi = n; + int mx = max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1); + for (int i = 0, j = 0; j < n; ++j) { + while (stones[j] - stones[i] + 1 > n) { + ++i; + } + if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) { + mi = min(mi, 2); + } else { + mi = min(mi, n - (j - i + 1)); + } + } + return {mi, mx}; + } +}; +``` + +### **Go** + +```go +func numMovesStonesII(stones []int) []int { + sort.Ints(stones) + n := len(stones) + mi := n + mx := max(stones[n-1]-stones[1]+1, stones[n-2]-stones[0]+1) - (n - 1) + i := 0 + for j, x := range stones { + for x-stones[i]+1 > n { + i++ + } + if j-i+1 == n-1 && stones[j]-stones[i] == n-2 { + mi = min(mi, 2) + } else { + mi = min(mi, n-(j-i+1)) + } + } + return []int{mi, mx} +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} +``` +### **TypeScript** + +```ts +function numMovesStonesII(stones: number[]): number[] { + stones.sort((a, b) => a - b); + const n = stones.length; + let mi = n; + const mx = + Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - + (n - 1); + for (let i = 0, j = 0; j < n; ++j) { + while (stones[j] - stones[i] + 1 > n) { + ++i; + } + if (j - i + 1 === n - 1 && stones[j] - stones[i] === n - 2) { + mi = Math.min(mi, 2); + } else { + mi = Math.min(mi, n - (j - i + 1)); + } + } + return [mi, mx]; +} ``` ### **...** diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.cpp b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.cpp new file mode 100644 index 0000000000000..314885e94bfc7 --- /dev/null +++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector numMovesStonesII(vector& stones) { + sort(stones.begin(), stones.end()); + int n = stones.size(); + int mi = n; + int mx = max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1); + for (int i = 0, j = 0; j < n; ++j) { + while (stones[j] - stones[i] + 1 > n) { + ++i; + } + if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) { + mi = min(mi, 2); + } else { + mi = min(mi, n - (j - i + 1)); + } + } + return {mi, mx}; + } +}; \ No newline at end of file diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.go b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.go new file mode 100644 index 0000000000000..b3f712ddbc7c0 --- /dev/null +++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.go @@ -0,0 +1,32 @@ +func numMovesStonesII(stones []int) []int { + sort.Ints(stones) + n := len(stones) + mi := n + mx := max(stones[n-1]-stones[1]+1, stones[n-2]-stones[0]+1) - (n - 1) + i := 0 + for j, x := range stones { + for x-stones[i]+1 > n { + i++ + } + if j-i+1 == n-1 && stones[j]-stones[i] == n-2 { + mi = min(mi, 2) + } else { + mi = min(mi, n-(j-i+1)) + } + } + return []int{mi, mx} +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} \ No newline at end of file diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.java b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.java new file mode 100644 index 0000000000000..d5d8e6a54835f --- /dev/null +++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.java @@ -0,0 +1,19 @@ +class Solution { + public int[] numMovesStonesII(int[] stones) { + Arrays.sort(stones); + int n = stones.length; + int mi = n; + int mx = Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1); + for (int i = 0, j = 0; j < n; ++j) { + while (stones[j] - stones[i] + 1 > n) { + ++i; + } + if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) { + mi = Math.min(mi, 2); + } else { + mi = Math.min(mi, n - (j - i + 1)); + } + } + return new int[] {mi, mx}; + } +} \ No newline at end of file diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.py b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.py new file mode 100644 index 0000000000000..9aa212f569ce4 --- /dev/null +++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def numMovesStonesII(self, stones: List[int]) -> List[int]: + stones.sort() + mi = n = len(stones) + mx = max(stones[-1] - stones[1] + 1, stones[-2] - stones[0] + 1) - (n - 1) + i = 0 + for j, x in enumerate(stones): + while x - stones[i] + 1 > n: + i += 1 + if j - i + 1 == n - 1 and x - stones[i] == n - 2: + mi = min(mi, 2) + else: + mi = min(mi, n - (j - i + 1)) + return [mi, mx] diff --git a/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.ts b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.ts new file mode 100644 index 0000000000000..ecd444c136472 --- /dev/null +++ b/solution/1000-1099/1040.Moving Stones Until Consecutive II/Solution.ts @@ -0,0 +1,19 @@ +function numMovesStonesII(stones: number[]): number[] { + stones.sort((a, b) => a - b); + const n = stones.length; + let mi = n; + const mx = + Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - + (n - 1); + for (let i = 0, j = 0; j < n; ++j) { + while (stones[j] - stones[i] + 1 > n) { + ++i; + } + if (j - i + 1 === n - 1 && stones[j] - stones[i] === n - 2) { + mi = Math.min(mi, 2); + } else { + mi = Math.min(mi, n - (j - i + 1)); + } + } + return [mi, mx]; +} diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/README.md b/solution/1000-1099/1041.Robot Bounded In Circle/README.md index 8000f8ff91e6a..5e9dc8f969881 100644 --- a/solution/1000-1099/1041.Robot Bounded In Circle/README.md +++ b/solution/1000-1099/1041.Robot Bounded In Circle/README.md @@ -178,6 +178,28 @@ func isRobotBounded(instructions string) bool { } ``` +### **TypeScript** + +```ts +function isRobotBounded(instructions: string): boolean { + const direction = new Array(4).fill(0); + let cur = 0; + for (const c of instructions.split('')) { + if (c === 'L') { + cur = (cur + 1) % 4; + } else if (c === 'R') { + cur = (cur + 3) % 4; + } else { + ++direction[cur]; + } + } + return ( + cur !== 0 || + (direction[0] === direction[2] && direction[1] === direction[3]) + ); +} +``` + ### **...** ``` diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md b/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md index 906194839e41a..caee4aff6baf2 100644 --- a/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md +++ b/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md @@ -163,6 +163,28 @@ func isRobotBounded(instructions string) bool { } ``` +### **TypeScript** + +```ts +function isRobotBounded(instructions: string): boolean { + const direction = new Array(4).fill(0); + let cur = 0; + for (const c of instructions.split('')) { + if (c === 'L') { + cur = (cur + 1) % 4; + } else if (c === 'R') { + cur = (cur + 3) % 4; + } else { + ++direction[cur]; + } + } + return ( + cur !== 0 || + (direction[0] === direction[2] && direction[1] === direction[3]) + ); +} +``` + ### **...** ``` diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/Solution.ts b/solution/1000-1099/1041.Robot Bounded In Circle/Solution.ts new file mode 100644 index 0000000000000..fd8d424f77da0 --- /dev/null +++ b/solution/1000-1099/1041.Robot Bounded In Circle/Solution.ts @@ -0,0 +1,17 @@ +function isRobotBounded(instructions: string): boolean { + const direction = new Array(4).fill(0); + let cur = 0; + for (const c of instructions.split('')) { + if (c === 'L') { + cur = (cur + 1) % 4; + } else if (c === 'R') { + cur = (cur + 3) % 4; + } else { + ++direction[cur]; + } + } + return ( + cur !== 0 || + (direction[0] === direction[2] && direction[1] === direction[3]) + ); +} diff --git a/solution/main.py b/solution/main.py index 73b1ec3cd7e34..eb1e311112c61 100644 --- a/solution/main.py +++ b/solution/main.py @@ -134,7 +134,7 @@ def generate_summary(result): def refresh(result): """update problems""" pattern = re.compile("src=\"(.*?)\"") - skip_question_ids = {1599} + skip_question_ids = {3, 33, 34, 1599} for question in result: front_question_id = question['frontend_question_id']