diff --git a/solution/0200-0299/0209.Minimum Size Subarray Sum/README.md b/solution/0200-0299/0209.Minimum Size Subarray Sum/README.md index 9b23bf83a977e..80f8c5d012195 100644 --- a/solution/0200-0299/0209.Minimum Size Subarray Sum/README.md +++ b/solution/0200-0299/0209.Minimum Size Subarray Sum/README.md @@ -266,13 +266,15 @@ public class Solution { ### 方法二:双指针 -我们可以使用双指针 $j$ 和 $i$ 维护一个窗口,其中窗口中的所有元素之和小于 $target$。初始时 $j = 0$,答案 $ans = n + 1$,其中 $n$ 为数组 $nums$ 的长度。 +我们注意到,数组 $\textit{nums}$ 中的元素均为正整数,我们可以考虑使用双指针来维护一个滑动窗口。 -接下来,指针 $i$ 从 $0$ 开始向右移动,每次移动一步,我们将指针 $i$ 对应的元素加入窗口,同时更新窗口中元素之和。如果窗口中元素之和大于等于 $target$,说明当前子数组满足条件,我们可以更新答案,即 $ans = \min(ans, i - j + 1)$。然后我们不断地从窗口中移除元素 $nums[j]$,直到窗口中元素之和小于 $target$,然后重复上述过程。 +具体地,我们定义两个指针 $\textit{l}$ 和 $\textit{r}$ 分别表示滑动窗口的左边界和右边界,用一个变量 $\textit{s}$ 代表滑动窗口中的元素和。 -最后,如果 $ans \leq n$,则说明存在满足条件的子数组,返回 $ans$,否则返回 $0$。 +在每一步操作中,我们移动右指针 $\textit{r}$,使得滑动窗口中加入一个元素,如果此时 $\textit{s} \ge \textit{target}$,我们就更新最小长度 $\textit{ans} = \min(\textit{ans}, \textit{r} - \textit{l} + 1$,并将左指针 $\textit{l}$ 循环向右移动,直至有 $\textit{s} < \textit{target}$。 + +最后,如果最小长度 $\textit{ans}$ 仍为初始值,我们就返回 $0$,否则返回 $\textit{ans}$。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $nums$ 的长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 @@ -281,16 +283,15 @@ public class Solution { ```python class Solution: def minSubArrayLen(self, target: int, nums: List[int]) -> int: - n = len(nums) - ans = n + 1 - s = j = 0 - for i, x in enumerate(nums): + l = s = 0 + ans = inf + for r, x in enumerate(nums): s += x - while j < n and s >= target: - ans = min(ans, i - j + 1) - s -= nums[j] - j += 1 - return ans if ans <= n else 0 + while s >= target: + ans = min(ans, r - l + 1) + s -= nums[l] + l += 1 + return 0 if ans == inf else ans ``` #### Java @@ -298,17 +299,17 @@ class Solution: ```java class Solution { public int minSubArrayLen(int target, int[] nums) { - int n = nums.length; + int l = 0, n = nums.length; long s = 0; int ans = n + 1; - for (int i = 0, j = 0; i < n; ++i) { - s += nums[i]; - while (j < n && s >= target) { - ans = Math.min(ans, i - j + 1); - s -= nums[j++]; + for (int r = 0; r < n; ++r) { + s += nums[r]; + while (s >= target) { + ans = Math.min(ans, r - l + 1); + s -= nums[l++]; } } - return ans <= n ? ans : 0; + return ans > n ? 0 : ans; } } ``` @@ -319,17 +320,17 @@ class Solution { class Solution { public: int minSubArrayLen(int target, vector& nums) { - int n = nums.size(); + int l = 0, n = nums.size(); long long s = 0; int ans = n + 1; - for (int i = 0, j = 0; i < n; ++i) { - s += nums[i]; - while (j < n && s >= target) { - ans = min(ans, i - j + 1); - s -= nums[j++]; + for (int r = 0; r < n; ++r) { + s += nums[r]; + while (s >= target) { + ans = min(ans, r - l + 1); + s -= nums[l++]; } } - return ans == n + 1 ? 0 : ans; + return ans > n ? 0 : ans; } }; ``` @@ -338,18 +339,17 @@ public: ```go func minSubArrayLen(target int, nums []int) int { - n := len(nums) - s := 0 - ans := n + 1 - for i, j := 0, 0; i < n; i++ { - s += nums[i] + l, n := 0, len(nums) + s, ans := 0, n+1 + for r, x := range nums { + s += x for s >= target { - ans = min(ans, i-j+1) - s -= nums[j] - j++ + ans = min(ans, r-l+1) + s -= nums[l] + l++ } } - if ans == n+1 { + if ans > n { return 0 } return ans @@ -361,16 +361,15 @@ func minSubArrayLen(target int, nums []int) int { ```ts function minSubArrayLen(target: number, nums: number[]): number { const n = nums.length; - let s = 0; - let ans = n + 1; - for (let i = 0, j = 0; i < n; ++i) { - s += nums[i]; + let [s, ans] = [0, n + 1]; + for (let l = 0, r = 0; r < n; ++r) { + s += nums[r]; while (s >= target) { - ans = Math.min(ans, i - j + 1); - s -= nums[j++]; + ans = Math.min(ans, r - l + 1); + s -= nums[l++]; } } - return ans === n + 1 ? 0 : ans; + return ans > n ? 0 : ans; } ``` diff --git a/solution/0200-0299/0209.Minimum Size Subarray Sum/README_EN.md b/solution/0200-0299/0209.Minimum Size Subarray Sum/README_EN.md index f540fa51066b5..404a6cdd43a9c 100644 --- a/solution/0200-0299/0209.Minimum Size Subarray Sum/README_EN.md +++ b/solution/0200-0299/0209.Minimum Size Subarray Sum/README_EN.md @@ -272,16 +272,15 @@ The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is ```python class Solution: def minSubArrayLen(self, target: int, nums: List[int]) -> int: - n = len(nums) - ans = n + 1 - s = j = 0 - for i, x in enumerate(nums): + l = s = 0 + ans = inf + for r, x in enumerate(nums): s += x - while j < n and s >= target: - ans = min(ans, i - j + 1) - s -= nums[j] - j += 1 - return ans if ans <= n else 0 + while s >= target: + ans = min(ans, r - l + 1) + s -= nums[l] + l += 1 + return 0 if ans == inf else ans ``` #### Java @@ -289,17 +288,17 @@ class Solution: ```java class Solution { public int minSubArrayLen(int target, int[] nums) { - int n = nums.length; + int l = 0, n = nums.length; long s = 0; int ans = n + 1; - for (int i = 0, j = 0; i < n; ++i) { - s += nums[i]; - while (j < n && s >= target) { - ans = Math.min(ans, i - j + 1); - s -= nums[j++]; + for (int r = 0; r < n; ++r) { + s += nums[r]; + while (s >= target) { + ans = Math.min(ans, r - l + 1); + s -= nums[l++]; } } - return ans <= n ? ans : 0; + return ans > n ? 0 : ans; } } ``` @@ -310,17 +309,17 @@ class Solution { class Solution { public: int minSubArrayLen(int target, vector& nums) { - int n = nums.size(); + int l = 0, n = nums.size(); long long s = 0; int ans = n + 1; - for (int i = 0, j = 0; i < n; ++i) { - s += nums[i]; - while (j < n && s >= target) { - ans = min(ans, i - j + 1); - s -= nums[j++]; + for (int r = 0; r < n; ++r) { + s += nums[r]; + while (s >= target) { + ans = min(ans, r - l + 1); + s -= nums[l++]; } } - return ans == n + 1 ? 0 : ans; + return ans > n ? 0 : ans; } }; ``` @@ -329,18 +328,17 @@ public: ```go func minSubArrayLen(target int, nums []int) int { - n := len(nums) - s := 0 - ans := n + 1 - for i, j := 0, 0; i < n; i++ { - s += nums[i] + l, n := 0, len(nums) + s, ans := 0, n+1 + for r, x := range nums { + s += x for s >= target { - ans = min(ans, i-j+1) - s -= nums[j] - j++ + ans = min(ans, r-l+1) + s -= nums[l] + l++ } } - if ans == n+1 { + if ans > n { return 0 } return ans @@ -352,16 +350,15 @@ func minSubArrayLen(target int, nums []int) int { ```ts function minSubArrayLen(target: number, nums: number[]): number { const n = nums.length; - let s = 0; - let ans = n + 1; - for (let i = 0, j = 0; i < n; ++i) { - s += nums[i]; + let [s, ans] = [0, n + 1]; + for (let l = 0, r = 0; r < n; ++r) { + s += nums[r]; while (s >= target) { - ans = Math.min(ans, i - j + 1); - s -= nums[j++]; + ans = Math.min(ans, r - l + 1); + s -= nums[l++]; } } - return ans === n + 1 ? 0 : ans; + return ans > n ? 0 : ans; } ``` diff --git a/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.cpp b/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.cpp index b6d57f2aaabd5..cc31cd24044b5 100644 --- a/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.cpp +++ b/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.cpp @@ -1,16 +1,16 @@ class Solution { public: int minSubArrayLen(int target, vector& nums) { - int n = nums.size(); + int l = 0, n = nums.size(); long long s = 0; int ans = n + 1; - for (int i = 0, j = 0; i < n; ++i) { - s += nums[i]; - while (j < n && s >= target) { - ans = min(ans, i - j + 1); - s -= nums[j++]; + for (int r = 0; r < n; ++r) { + s += nums[r]; + while (s >= target) { + ans = min(ans, r - l + 1); + s -= nums[l++]; } } - return ans == n + 1 ? 0 : ans; + return ans > n ? 0 : ans; } -}; \ No newline at end of file +}; diff --git a/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.go b/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.go index 37a23211e9740..e3cecbba9dff3 100644 --- a/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.go +++ b/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.go @@ -1,17 +1,16 @@ func minSubArrayLen(target int, nums []int) int { - n := len(nums) - s := 0 - ans := n + 1 - for i, j := 0, 0; i < n; i++ { - s += nums[i] + l, n := 0, len(nums) + s, ans := 0, n+1 + for r, x := range nums { + s += x for s >= target { - ans = min(ans, i-j+1) - s -= nums[j] - j++ + ans = min(ans, r-l+1) + s -= nums[l] + l++ } } - if ans == n+1 { + if ans > n { return 0 } return ans -} \ No newline at end of file +} diff --git a/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.java b/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.java index 11b6a8155908b..d922a1fb4b6c4 100644 --- a/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.java +++ b/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.java @@ -1,15 +1,15 @@ class Solution { public int minSubArrayLen(int target, int[] nums) { - int n = nums.length; + int l = 0, n = nums.length; long s = 0; int ans = n + 1; - for (int i = 0, j = 0; i < n; ++i) { - s += nums[i]; - while (j < n && s >= target) { - ans = Math.min(ans, i - j + 1); - s -= nums[j++]; + for (int r = 0; r < n; ++r) { + s += nums[r]; + while (s >= target) { + ans = Math.min(ans, r - l + 1); + s -= nums[l++]; } } - return ans <= n ? ans : 0; + return ans > n ? 0 : ans; } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.py b/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.py index 6dcbc2480b59f..38e4717138007 100644 --- a/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.py +++ b/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.py @@ -1,12 +1,11 @@ class Solution: def minSubArrayLen(self, target: int, nums: List[int]) -> int: - n = len(nums) - ans = n + 1 - s = j = 0 - for i, x in enumerate(nums): + l = s = 0 + ans = inf + for r, x in enumerate(nums): s += x - while j < n and s >= target: - ans = min(ans, i - j + 1) - s -= nums[j] - j += 1 - return ans if ans <= n else 0 + while s >= target: + ans = min(ans, r - l + 1) + s -= nums[l] + l += 1 + return 0 if ans == inf else ans diff --git a/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.ts b/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.ts index ef54870682bf6..66fd4ae9b601d 100644 --- a/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.ts +++ b/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.ts @@ -1,13 +1,12 @@ function minSubArrayLen(target: number, nums: number[]): number { const n = nums.length; - let s = 0; - let ans = n + 1; - for (let i = 0, j = 0; i < n; ++i) { - s += nums[i]; + let [s, ans] = [0, n + 1]; + for (let l = 0, r = 0; r < n; ++r) { + s += nums[r]; while (s >= target) { - ans = Math.min(ans, i - j + 1); - s -= nums[j++]; + ans = Math.min(ans, r - l + 1); + s -= nums[l++]; } } - return ans === n + 1 ? 0 : ans; + return ans > n ? 0 : ans; } diff --git a/solution/0200-0299/0266.Palindrome Permutation/README.md b/solution/0200-0299/0266.Palindrome Permutation/README.md index b1181c7a2fe31..222068a8d3801 100644 --- a/solution/0200-0299/0266.Palindrome Permutation/README.md +++ b/solution/0200-0299/0266.Palindrome Permutation/README.md @@ -58,9 +58,9 @@ tags: -### 方法一:数组 +### 方法一:计数 -创建一个长度为 $26$ 的数组,统计每个字母出现的频率,至多有一个字符出现奇数次数即可。 +如果一个字符串是回文串,那么至多只有一个字符出现奇数次数,其余字符都出现偶数次数。因此我们只需要统计每个字符出现的次数,然后判断是否满足这个条件即可。 时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串的长度,而 $|\Sigma|$ 是字符集的大小,本题中字符集为小写字母,因此 $|\Sigma|=26$。 @@ -131,7 +131,7 @@ func canPermutePalindrome(s string) bool { ```ts function canPermutePalindrome(s: string): boolean { - const cnt: number[] = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of s) { ++cnt[c.charCodeAt(0) - 97]; } @@ -147,11 +147,11 @@ function canPermutePalindrome(s: string): boolean { * @return {boolean} */ var canPermutePalindrome = function (s) { - const cnt = new Array(26).fill(0); + const cnt = new Map(); for (const c of s) { - ++cnt[c.charCodeAt() - 'a'.charCodeAt()]; + cnt.set(c, (cnt.get(c) || 0) + 1); } - return cnt.filter(c => c % 2 === 1).length < 2; + return [...cnt.values()].filter(v => v % 2 === 1).length < 2; }; ``` diff --git a/solution/0200-0299/0266.Palindrome Permutation/README_EN.md b/solution/0200-0299/0266.Palindrome Permutation/README_EN.md index 927d690bcf32e..593d55562d798 100644 --- a/solution/0200-0299/0266.Palindrome Permutation/README_EN.md +++ b/solution/0200-0299/0266.Palindrome Permutation/README_EN.md @@ -56,7 +56,11 @@ tags: -### Solution 1 +### Solution 1: Counting + +If a string is a palindrome, at most one character can appear an odd number of times, while all other characters must appear an even number of times. Therefore, we only need to count the occurrences of each character and then check if this condition is satisfied. + +Time complexity is $O(n)$, and space complexity is $O(|\Sigma|)$. Here, $n$ is the length of the string, and $|\Sigma|$ is the size of the character set. In this problem, the character set consists of lowercase letters, so $|\Sigma|=26$. @@ -125,7 +129,7 @@ func canPermutePalindrome(s string) bool { ```ts function canPermutePalindrome(s: string): boolean { - const cnt: number[] = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of s) { ++cnt[c.charCodeAt(0) - 97]; } @@ -141,11 +145,11 @@ function canPermutePalindrome(s: string): boolean { * @return {boolean} */ var canPermutePalindrome = function (s) { - const cnt = new Array(26).fill(0); + const cnt = new Map(); for (const c of s) { - ++cnt[c.charCodeAt() - 'a'.charCodeAt()]; + cnt.set(c, (cnt.get(c) || 0) + 1); } - return cnt.filter(c => c % 2 === 1).length < 2; + return [...cnt.values()].filter(v => v % 2 === 1).length < 2; }; ``` diff --git a/solution/0200-0299/0266.Palindrome Permutation/Solution.js b/solution/0200-0299/0266.Palindrome Permutation/Solution.js index 346a9ee9421a0..9d3ff25b7e41a 100644 --- a/solution/0200-0299/0266.Palindrome Permutation/Solution.js +++ b/solution/0200-0299/0266.Palindrome Permutation/Solution.js @@ -3,9 +3,9 @@ * @return {boolean} */ var canPermutePalindrome = function (s) { - const cnt = new Array(26).fill(0); + const cnt = new Map(); for (const c of s) { - ++cnt[c.charCodeAt() - 'a'.charCodeAt()]; + cnt.set(c, (cnt.get(c) || 0) + 1); } - return cnt.filter(c => c % 2 === 1).length < 2; + return [...cnt.values()].filter(v => v % 2 === 1).length < 2; }; diff --git a/solution/0200-0299/0266.Palindrome Permutation/Solution.ts b/solution/0200-0299/0266.Palindrome Permutation/Solution.ts index 5edeb264d7743..5004ae6504acb 100644 --- a/solution/0200-0299/0266.Palindrome Permutation/Solution.ts +++ b/solution/0200-0299/0266.Palindrome Permutation/Solution.ts @@ -1,5 +1,5 @@ function canPermutePalindrome(s: string): boolean { - const cnt: number[] = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of s) { ++cnt[c.charCodeAt(0) - 97]; }