diff --git a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/README.md b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/README.md index 614dffc3e801d..ebba6f60b91fa 100644 --- a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/README.md +++ b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/README.md @@ -41,7 +41,7 @@ tags:
输入:nums = [555,901,482,1771] -输出:1 +输出:1 解释: 只有 1771 是位数为偶数的数字。@@ -61,11 +61,13 @@ tags: -### 方法一:枚举 +### 方法一:模拟 -枚举数组 `nums` 中的每个元素,将其转换为字符串,判断字符串长度是否为偶数,是则答案加一。 +我们遍历数组 $\textit{nums}$ 中的每个元素,对于当前遍历到的元素 $x$,我们直接将其转换为字符串,然后判断其长度是否为偶数即可。若是则将答案加一。 -时间复杂度 $O(n \times \log_{10} m)$,空间复杂度 $O(\log_{10} m)$,其中 $n$ 和 $m$ 分别为数组 `nums` 的长度以及数组 `nums` 中的最大元素。 +遍历结束后,我们返回答案即可。 + +时间复杂度 $O(n \times \log M)$,空间复杂度 $O(\log M)$。其中 $n$ 是数组 $\textit{nums}$ 的长度,而 $M$ 是数组 $\textit{nums}$ 中的元素的最大值。 @@ -74,7 +76,7 @@ tags: ```python class Solution: def findNumbers(self, nums: List[int]) -> int: - return sum(len(str(v)) % 2 == 0 for v in nums) + return sum(len(str(x)) % 2 == 0 for x in nums) ``` #### Java @@ -83,8 +85,8 @@ class Solution: class Solution { public int findNumbers(int[] nums) { int ans = 0; - for (int v : nums) { - if (String.valueOf(v).length() % 2 == 0) { + for (int x : nums) { + if (String.valueOf(x).length() % 2 == 0) { ++ans; } } @@ -100,8 +102,8 @@ class Solution { public: int findNumbers(vector
Input: nums = [12,345,2,6,7896] Output: 2 -Explanation: +Explanation: 12 contains 2 digits (even number of digits). 345 contains 3 digits (odd number of digits). 2 contains 1 digit (odd number of digits). @@ -40,7 +40,7 @@ Therefore only 12 and 7896 contain an even number of digits.Input: nums = [555,901,482,1771] -Output: 1 +Output: 1 Explanation: Only 1771 contains an even number of digits.@@ -59,7 +59,13 @@ Only 1771 contains an even number of digits. -### Solution 1 +### Solution 1: Simulation + +We traverse each element $x$ in the array $\textit{nums}$. For the current element $x$, we directly convert it to a string and then check if its length is even. If it is, we increment the answer by one. + +After the traversal is complete, we return the answer. + +The time complexity is $O(n \times \log M)$, and the space complexity is $O(\log M)$. Here, $n$ is the length of the array $\textit{nums}$, and $M$ is the maximum value of the elements in the array $\textit{nums}$. @@ -68,7 +74,7 @@ Only 1771 contains an even number of digits. ```python class Solution: def findNumbers(self, nums: List[int]) -> int: - return sum(len(str(v)) % 2 == 0 for v in nums) + return sum(len(str(x)) % 2 == 0 for x in nums) ``` #### Java @@ -77,8 +83,8 @@ class Solution: class Solution { public int findNumbers(int[] nums) { int ans = 0; - for (int v : nums) { - if (String.valueOf(v).length() % 2 == 0) { + for (int x : nums) { + if (String.valueOf(x).length() % 2 == 0) { ++ans; } } @@ -94,8 +100,8 @@ class Solution { public: int findNumbers(vector& nums) { int ans = 0; - for (int& v : nums) { - ans += to_string(v).size() % 2 == 0; + for (int& x : nums) { + ans += to_string(x).size() % 2 == 0; } return ans; } @@ -106,8 +112,8 @@ public: ```go func findNumbers(nums []int) (ans int) { - for _, v := range nums { - if len(strconv.Itoa(v))%2 == 0 { + for _, x := range nums { + if len(strconv.Itoa(x))%2 == 0 { ans++ } } @@ -115,6 +121,14 @@ func findNumbers(nums []int) (ans int) { } ``` +#### TypeScript + +```ts +function findNumbers(nums: number[]): number { + return nums.filter(x => x.toString().length % 2 === 0).length; +} +``` + #### JavaScript ```js @@ -123,11 +137,7 @@ func findNumbers(nums []int) (ans int) { * @return {number} */ var findNumbers = function (nums) { - let ans = 0; - for (const v of nums) { - ans += String(v).length % 2 == 0; - } - return ans; + return nums.filter(x => x.toString().length % 2 === 0).length; }; ``` diff --git a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.cpp b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.cpp index 01a83f17855b3..8a05d501250c3 100644 --- a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.cpp +++ b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.cpp @@ -2,9 +2,9 @@ class Solution { public: int findNumbers(vector & nums) { int ans = 0; - for (int& v : nums) { - ans += to_string(v).size() % 2 == 0; + for (int& x : nums) { + ans += to_string(x).size() % 2 == 0; } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.go b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.go index df389d4b63993..2b714feabcb8c 100644 --- a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.go +++ b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.go @@ -1,8 +1,8 @@ func findNumbers(nums []int) (ans int) { - for _, v := range nums { - if len(strconv.Itoa(v))%2 == 0 { + for _, x := range nums { + if len(strconv.Itoa(x))%2 == 0 { ans++ } } return -} \ No newline at end of file +} diff --git a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.java b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.java index 5bd0d8d0ef99a..41e9941e31cf6 100644 --- a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.java +++ b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.java @@ -1,11 +1,11 @@ class Solution { public int findNumbers(int[] nums) { int ans = 0; - for (int v : nums) { - if (String.valueOf(v).length() % 2 == 0) { + for (int x : nums) { + if (String.valueOf(x).length() % 2 == 0) { ++ans; } } return ans; } -} \ No newline at end of file +} diff --git a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.js b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.js index ca37b182c22ab..5b77e8f4a2efa 100644 --- a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.js +++ b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.js @@ -3,9 +3,5 @@ * @return {number} */ var findNumbers = function (nums) { - let ans = 0; - for (const v of nums) { - ans += String(v).length % 2 == 0; - } - return ans; + return nums.filter(x => x.toString().length % 2 === 0).length; }; diff --git a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.py b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.py index 6d311f5596c2b..5fd671760e1eb 100644 --- a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.py +++ b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.py @@ -1,3 +1,3 @@ class Solution: def findNumbers(self, nums: List[int]) -> int: - return sum(len(str(v)) % 2 == 0 for v in nums) + return sum(len(str(x)) % 2 == 0 for x in nums) diff --git a/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.ts b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.ts new file mode 100644 index 0000000000000..dee6e231b5acc --- /dev/null +++ b/solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.ts @@ -0,0 +1,3 @@ +function findNumbers(nums: number[]): number { + return nums.filter(x => x.toString().length % 2 === 0).length; +} diff --git a/solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README.md b/solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README.md index 900f63d6ad0c9..c2b1874c84663 100644 --- a/solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README.md +++ b/solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README.md @@ -75,9 +75,9 @@ tags: ### 方法一:哈希表 + 枚举 -根据题目描述,如果一个长串满足条件,那么这个长串的子串(长度至少为 `minSize`)也一定满足条件。因此,我们只需要枚举 $s$ 中所有长度为 `minSize` 的子串,然后利用哈希表记录所有子串的出现次数,找出最大的次数作为答案即可。 +根据题目描述,如果一个长串满足条件,那么这个长串的长度为 $\textit{minSize}$ 的子串也一定满足条件。因此,我们只需要枚举 $s$ 中所有长度为 $\textit{minSize}$ 的子串,然后利用哈希表记录所有子串的出现次数,找出最大的次数作为答案即可。 -时间复杂度 $O(n \times m)$,空间复杂度 $O(n \times m)$。其中 $n$ 和 $m$ 分别为字符串 $s$ 的长度以及 `minSize` 的大小。本题中 $m$ 不超过 $26$。 +时间复杂度 $O(n \times m)$,空间复杂度 $O(n \times m)$。其中 $n$ 和 $m$ 分别为字符串 $s$ 的长度以及 $\textit{minSize}$ 的大小。本题中 $m$ 不超过 $26$。 @@ -162,6 +162,24 @@ func maxFreq(s string, maxLetters int, minSize int, maxSize int) (ans int) { } ``` +#### TypeScript + +```ts +function maxFreq(s: string, maxLetters: number, minSize: number, maxSize: number): number { + const cnt = new Map (); + let ans = 0; + for (let i = 0; i < s.length - minSize + 1; ++i) { + const t = s.slice(i, i + minSize); + const ss = new Set(t.split('')); + if (ss.size <= maxLetters) { + cnt.set(t, (cnt.get(t) || 0) + 1); + ans = Math.max(ans, cnt.get(t)!); + } + } + return ans; +} +``` + diff --git a/solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README_EN.md b/solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README_EN.md index 9a8a00bcdce57..2aa30af79cc2e 100644 --- a/solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README_EN.md +++ b/solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README_EN.md @@ -61,7 +61,11 @@ It satisfies the conditions, 2 unique letters and size 3 (between minSize and ma -### Solution 1 +### Solution 1: Hash Table + Enumeration + +According to the problem description, if a long string meets the condition, then its substring of length $\textit{minSize}$ must also meet the condition. Therefore, we only need to enumerate all substrings of length $\textit{minSize}$ in $s$, then use a hash table to record the occurrence frequency of all substrings, and find the maximum frequency as the answer. + +The time complexity is $O(n \times m)$, and the space complexity is $O(n \times m)$. Here, $n$ and $m$ are the lengths of the string $s$ and $\textit{minSize}$, respectively. In this problem, $m$ does not exceed $26$. @@ -146,6 +150,24 @@ func maxFreq(s string, maxLetters int, minSize int, maxSize int) (ans int) { } ``` +#### TypeScript + +```ts +function maxFreq(s: string, maxLetters: number, minSize: number, maxSize: number): number { + const cnt = new Map (); + let ans = 0; + for (let i = 0; i < s.length - minSize + 1; ++i) { + const t = s.slice(i, i + minSize); + const ss = new Set(t.split('')); + if (ss.size <= maxLetters) { + cnt.set(t, (cnt.get(t) || 0) + 1); + ans = Math.max(ans, cnt.get(t)!); + } + } + return ans; +} +``` + diff --git a/solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/Solution.ts b/solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/Solution.ts new file mode 100644 index 0000000000000..2654afd8e4b3e --- /dev/null +++ b/solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/Solution.ts @@ -0,0 +1,13 @@ +function maxFreq(s: string, maxLetters: number, minSize: number, maxSize: number): number { + const cnt = new Map (); + let ans = 0; + for (let i = 0; i < s.length - minSize + 1; ++i) { + const t = s.slice(i, i + minSize); + const ss = new Set(t.split('')); + if (ss.size <= maxLetters) { + cnt.set(t, (cnt.get(t) || 0) + 1); + ans = Math.max(ans, cnt.get(t)!); + } + } + return ans; +} diff --git a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/README.md b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/README.md index 19c111f488a6b..feb5314f203a3 100644 --- a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/README.md +++ b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/README.md @@ -61,7 +61,15 @@ tags: -### 方法一 +### 方法一:逆序遍历 + +我们用一个变量 $mx$ 记录当前位置右侧的最大值,初始时 $mx = -1$。 + +然后我们从右向左遍历数组,对于每个位置 $i$,我们记当前位置的值为 $x$,将当前位置的值更新为 $mx$,然后更新 $mx = \max(mx, x)$。 + +最后返回原数组即可。 + +时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。 @@ -70,11 +78,11 @@ tags: ```python class Solution: def replaceElements(self, arr: List[int]) -> List[int]: - m = -1 - for i in range(len(arr) - 1, -1, -1): - t = arr[i] - arr[i] = m - m = max(m, t) + mx = -1 + for i in reversed(range(len(arr))): + x = arr[i] + arr[i] = mx + mx = max(mx, x) return arr ``` @@ -83,13 +91,55 @@ class Solution: ```java class Solution { public int[] replaceElements(int[] arr) { - for (int i = arr.length - 1, max = -1; i >= 0; --i) { - int t = arr[i]; - arr[i] = max; - max = Math.max(max, t); + for (int i = arr.length - 1, mx = -1; i >= 0; --i) { + int x = arr[i]; + arr[i] = mx; + mx = Math.max(mx, x); + } + return arr; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + vector replaceElements(vector & arr) { + for (int i = arr.size() - 1, mx = -1; ~i; --i) { + int x = arr[i]; + arr[i] = mx; + mx = max(mx, x); } return arr; } +}; +``` + +#### Go + +```go +func replaceElements(arr []int) []int { + for i, mx := len(arr)-1, -1; i >= 0; i-- { + x := arr[i] + arr[i] = mx + mx = max(mx, x) + } + return arr +} +``` + +#### TypeScript + +```ts +function replaceElements(arr: number[]): number[] { + for (let i = arr.length - 1, mx = -1; ~i; --i) { + const x = arr[i]; + arr[i] = mx; + mx = Math.max(mx, x); + } + return arr; } ``` diff --git a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/README_EN.md b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/README_EN.md index c6101fce18df1..5cd11d3f3f03c 100644 --- a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/README_EN.md +++ b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/README_EN.md @@ -28,7 +28,7 @@ tags: Input: arr = [17,18,5,4,6,1] Output: [18,6,6,6,1,-1] -Explanation: +Explanation: - index 0 --> the greatest element to the right of index 0 is index 1 (18). - index 1 --> the greatest element to the right of index 1 is index 4 (6). - index 2 --> the greatest element to the right of index 2 is index 4 (6). @@ -59,7 +59,15 @@ tags: -### Solution 1 +### Solution 1: Reverse Traversal + +We use a variable $mx$ to record the maximum value to the right of the current position, initially $mx = -1$. + +Then we traverse the array from right to left. For each position $i$, we denote the current value as $x$, update the current position's value to $mx$, and then update $mx = \max(mx, x)$. + +Finally, return the original array. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -68,11 +76,11 @@ tags: ```python class Solution: def replaceElements(self, arr: List[int]) -> List[int]: - m = -1 - for i in range(len(arr) - 1, -1, -1): - t = arr[i] - arr[i] = m - m = max(m, t) + mx = -1 + for i in reversed(range(len(arr))): + x = arr[i] + arr[i] = mx + mx = max(mx, x) return arr ``` @@ -81,13 +89,55 @@ class Solution: ```java class Solution { public int[] replaceElements(int[] arr) { - for (int i = arr.length - 1, max = -1; i >= 0; --i) { - int t = arr[i]; - arr[i] = max; - max = Math.max(max, t); + for (int i = arr.length - 1, mx = -1; i >= 0; --i) { + int x = arr[i]; + arr[i] = mx; + mx = Math.max(mx, x); + } + return arr; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + vectorreplaceElements(vector & arr) { + for (int i = arr.size() - 1, mx = -1; ~i; --i) { + int x = arr[i]; + arr[i] = mx; + mx = max(mx, x); } return arr; } +}; +``` + +#### Go + +```go +func replaceElements(arr []int) []int { + for i, mx := len(arr)-1, -1; i >= 0; i-- { + x := arr[i] + arr[i] = mx + mx = max(mx, x) + } + return arr +} +``` + +#### TypeScript + +```ts +function replaceElements(arr: number[]): number[] { + for (let i = arr.length - 1, mx = -1; ~i; --i) { + const x = arr[i]; + arr[i] = mx; + mx = Math.max(mx, x); + } + return arr; } ``` diff --git a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.cpp b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.cpp new file mode 100644 index 0000000000000..faac001515a5d --- /dev/null +++ b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + vector replaceElements(vector & arr) { + for (int i = arr.size() - 1, mx = -1; ~i; --i) { + int x = arr[i]; + arr[i] = mx; + mx = max(mx, x); + } + return arr; + } +}; diff --git a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.go b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.go new file mode 100644 index 0000000000000..fa1282ac18a52 --- /dev/null +++ b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.go @@ -0,0 +1,8 @@ +func replaceElements(arr []int) []int { + for i, mx := len(arr)-1, -1; i >= 0; i-- { + x := arr[i] + arr[i] = mx + mx = max(mx, x) + } + return arr +} diff --git a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.java b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.java index 8d7b95db66a4b..5260011ea070a 100644 --- a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.java +++ b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.java @@ -1,10 +1,10 @@ class Solution { public int[] replaceElements(int[] arr) { - for (int i = arr.length - 1, max = -1; i >= 0; --i) { - int t = arr[i]; - arr[i] = max; - max = Math.max(max, t); + for (int i = arr.length - 1, mx = -1; i >= 0; --i) { + int x = arr[i]; + arr[i] = mx; + mx = Math.max(mx, x); } return arr; } -} \ No newline at end of file +} diff --git a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.py b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.py index bc3fa6cc61bd4..b67306f30ab59 100644 --- a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.py +++ b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.py @@ -1,8 +1,8 @@ class Solution: def replaceElements(self, arr: List[int]) -> List[int]: - m = -1 - for i in range(len(arr) - 1, -1, -1): - t = arr[i] - arr[i] = m - m = max(m, t) + mx = -1 + for i in reversed(range(len(arr))): + x = arr[i] + arr[i] = mx + mx = max(mx, x) return arr diff --git a/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.ts b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.ts new file mode 100644 index 0000000000000..c5773e8ac6748 --- /dev/null +++ b/solution/1200-1299/1299.Replace Elements with Greatest Element on Right Side/Solution.ts @@ -0,0 +1,8 @@ +function replaceElements(arr: number[]): number[] { + for (let i = arr.length - 1, mx = -1; ~i; --i) { + const x = arr[i]; + arr[i] = mx; + mx = Math.max(mx, x); + } + return arr; +} diff --git a/solution/3100-3199/3162.Find the Number of Good Pairs I/README.md b/solution/3100-3199/3162.Find the Number of Good Pairs I/README.md index bf962a213512e..3f749bf5b7988 100644 --- a/solution/3100-3199/3162.Find the Number of Good Pairs I/README.md +++ b/solution/3100-3199/3162.Find the Number of Good Pairs I/README.md @@ -69,11 +69,11 @@ tags: ### 方法一:暴力枚举 -我们直接枚举所有的数位 $(x, y)$,判断是否满足 $x \mod (y \times k) = 0$,如果满足则答案加一。 +我们直接枚举所有的数位 $(x, y)$,判断是否满足 $x \bmod (y \times k) = 0$,如果满足则答案加一。 枚举结束后,返回答案即可。 -时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是数组 `nums1` 和 `nums2` 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是数组 $\textit{nums1}$ 和 $\textit{nums2}$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/3100-3199/3162.Find the Number of Good Pairs I/README_EN.md b/solution/3100-3199/3162.Find the Number of Good Pairs I/README_EN.md index c7da5c2e19a70..fc90667fd61c3 100644 --- a/solution/3100-3199/3162.Find the Number of Good Pairs I/README_EN.md +++ b/solution/3100-3199/3162.Find the Number of Good Pairs I/README_EN.md @@ -65,11 +65,11 @@ The 5 good pairs are (0, 0)
,(1, 0)
,(1, 1)