Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b335300

Browse files
feat: add solutions to lc problems: No.1295,1299 (doocs#3629)
* No.1295.Find Numbers with Even Number of Digits * No.1297.Maximum Number of Occurrences of a Substring * No.1299.Replace Elements with Greatest Element on Right Side
1 parent c40929f commit b335300

File tree

20 files changed

+280
-85
lines changed

20 files changed

+280
-85
lines changed

‎solution/1200-1299/1295.Find Numbers with Even Number of Digits/README.md‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tags:
4141

4242
<pre>
4343
<strong>输入:</strong>nums = [555,901,482,1771]
44-
<strong>输出:</strong>1
44+
<strong>输出:</strong>1
4545
<strong>解释: </strong>
4646
只有 1771 是位数为偶数的数字。
4747
</pre>
@@ -61,11 +61,13 @@ tags:
6161

6262
<!-- solution:start -->
6363

64-
### 方法一:枚举
64+
### 方法一:模拟
6565

66-
枚举数组 `nums` 中的每个元素,将其转换为字符串,判断字符串长度是否为偶数,是则答案加一
66+
我们遍历数组 $\textit{nums}$ 中的每个元素,对于当前遍历到的元素 $x,ドル我们直接将其转换为字符串,然后判断其长度是否为偶数即可。若是则将答案加一
6767

68-
时间复杂度 $O(n \times \log_{10} m),ドル空间复杂度 $O(\log_{10} m),ドル其中 $n$ 和 $m$ 分别为数组 `nums` 的长度以及数组 `nums` 中的最大元素。
68+
遍历结束后,我们返回答案即可。
69+
70+
时间复杂度 $O(n \times \log M),ドル空间复杂度 $O(\log M)$。其中 $n$ 是数组 $\textit{nums}$ 的长度,而 $M$ 是数组 $\textit{nums}$ 中的元素的最大值。
6971

7072
<!-- tabs:start -->
7173

@@ -74,7 +76,7 @@ tags:
7476
```python
7577
class Solution:
7678
def findNumbers(self, nums: List[int]) -> int:
77-
return sum(len(str(v)) % 2 == 0 for v in nums)
79+
return sum(len(str(x)) % 2 == 0 for x in nums)
7880
```
7981

8082
#### Java
@@ -83,8 +85,8 @@ class Solution:
8385
class Solution {
8486
public int findNumbers(int[] nums) {
8587
int ans = 0;
86-
for (int v : nums) {
87-
if (String.valueOf(v).length() % 2 == 0) {
88+
for (int x : nums) {
89+
if (String.valueOf(x).length() % 2 == 0) {
8890
++ans;
8991
}
9092
}
@@ -100,8 +102,8 @@ class Solution {
100102
public:
101103
int findNumbers(vector<int>& nums) {
102104
int ans = 0;
103-
for (int& v : nums) {
104-
ans += to_string(v).size() % 2 == 0;
105+
for (int& x : nums) {
106+
ans += to_string(x).size() % 2 == 0;
105107
}
106108
return ans;
107109
}
@@ -112,15 +114,23 @@ public:
112114
113115
```go
114116
func findNumbers(nums []int) (ans int) {
115-
for _, v := range nums {
116-
if len(strconv.Itoa(v))%2 == 0 {
117+
for _, x := range nums {
118+
if len(strconv.Itoa(x))%2 == 0 {
117119
ans++
118120
}
119121
}
120122
return
121123
}
122124
```
123125

126+
#### TypeScript
127+
128+
```ts
129+
function findNumbers(nums: number[]): number {
130+
return nums.filter(x => x.toString().length % 2 === 0).length;
131+
}
132+
```
133+
124134
#### JavaScript
125135

126136
```js
@@ -129,11 +139,7 @@ func findNumbers(nums []int) (ans int) {
129139
* @return {number}
130140
*/
131141
var findNumbers = function (nums) {
132-
let ans = 0;
133-
for (const v of nums) {
134-
ans += String(v).length % 2 == 0;
135-
}
136-
return ans;
142+
return nums.filter(x => x.toString().length % 2 === 0).length;
137143
};
138144
```
139145

‎solution/1200-1299/1295.Find Numbers with Even Number of Digits/README_EN.md‎

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ tags:
2727
<pre>
2828
<strong>Input:</strong> nums = [12,345,2,6,7896]
2929
<strong>Output:</strong> 2
30-
<strong>Explanation:
30+
<strong>Explanation:
3131
</strong>12 contains 2 digits (even number of digits).&nbsp;
3232
345 contains 3 digits (odd number of digits).&nbsp;
3333
2 contains 1 digit (odd number of digits).&nbsp;
@@ -40,7 +40,7 @@ Therefore only 12 and 7896 contain an even number of digits.
4040

4141
<pre>
4242
<strong>Input:</strong> nums = [555,901,482,1771]
43-
<strong>Output:</strong> 1
43+
<strong>Output:</strong> 1
4444
<strong>Explanation: </strong>
4545
Only 1771 contains an even number of digits.
4646
</pre>
@@ -59,7 +59,13 @@ Only 1771 contains an even number of digits.
5959

6060
<!-- solution:start -->
6161

62-
### Solution 1
62+
### Solution 1: Simulation
63+
64+
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.
65+
66+
After the traversal is complete, we return the answer.
67+
68+
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}$.
6369

6470
<!-- tabs:start -->
6571

@@ -68,7 +74,7 @@ Only 1771 contains an even number of digits.
6874
```python
6975
class Solution:
7076
def findNumbers(self, nums: List[int]) -> int:
71-
return sum(len(str(v)) % 2 == 0 for v in nums)
77+
return sum(len(str(x)) % 2 == 0 for x in nums)
7278
```
7379

7480
#### Java
@@ -77,8 +83,8 @@ class Solution:
7783
class Solution {
7884
public int findNumbers(int[] nums) {
7985
int ans = 0;
80-
for (int v : nums) {
81-
if (String.valueOf(v).length() % 2 == 0) {
86+
for (int x : nums) {
87+
if (String.valueOf(x).length() % 2 == 0) {
8288
++ans;
8389
}
8490
}
@@ -94,8 +100,8 @@ class Solution {
94100
public:
95101
int findNumbers(vector<int>& nums) {
96102
int ans = 0;
97-
for (int& v : nums) {
98-
ans += to_string(v).size() % 2 == 0;
103+
for (int& x : nums) {
104+
ans += to_string(x).size() % 2 == 0;
99105
}
100106
return ans;
101107
}
@@ -106,15 +112,23 @@ public:
106112
107113
```go
108114
func findNumbers(nums []int) (ans int) {
109-
for _, v := range nums {
110-
if len(strconv.Itoa(v))%2 == 0 {
115+
for _, x := range nums {
116+
if len(strconv.Itoa(x))%2 == 0 {
111117
ans++
112118
}
113119
}
114120
return
115121
}
116122
```
117123

124+
#### TypeScript
125+
126+
```ts
127+
function findNumbers(nums: number[]): number {
128+
return nums.filter(x => x.toString().length % 2 === 0).length;
129+
}
130+
```
131+
118132
#### JavaScript
119133

120134
```js
@@ -123,11 +137,7 @@ func findNumbers(nums []int) (ans int) {
123137
* @return {number}
124138
*/
125139
var findNumbers = function (nums) {
126-
let ans = 0;
127-
for (const v of nums) {
128-
ans += String(v).length % 2 == 0;
129-
}
130-
return ans;
140+
return nums.filter(x => x.toString().length % 2 === 0).length;
131141
};
132142
```
133143

‎solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ class Solution {
22
public:
33
int findNumbers(vector<int>& nums) {
44
int ans = 0;
5-
for (int& v : nums) {
6-
ans += to_string(v).size() % 2 == 0;
5+
for (int& x : nums) {
6+
ans += to_string(x).size() % 2 == 0;
77
}
88
return ans;
99
}
10-
};
10+
};
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
func findNumbers(nums []int) (ans int) {
2-
for _, v := range nums {
3-
if len(strconv.Itoa(v))%2 == 0 {
2+
for _, x := range nums {
3+
if len(strconv.Itoa(x))%2 == 0 {
44
ans++
55
}
66
}
77
return
8-
}
8+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public int findNumbers(int[] nums) {
33
int ans = 0;
4-
for (int v : nums) {
5-
if (String.valueOf(v).length() % 2 == 0) {
4+
for (int x : nums) {
5+
if (String.valueOf(x).length() % 2 == 0) {
66
++ans;
77
}
88
}
99
return ans;
1010
}
11-
}
11+
}

‎solution/1200-1299/1295.Find Numbers with Even Number of Digits/Solution.js‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,5 @@
33
* @return {number}
44
*/
55
var findNumbers = function (nums) {
6-
let ans = 0;
7-
for (const v of nums) {
8-
ans += String(v).length % 2 == 0;
9-
}
10-
return ans;
6+
return nums.filter(x => x.toString().length % 2 === 0).length;
117
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Solution:
22
def findNumbers(self, nums: List[int]) -> int:
3-
return sum(len(str(v)) % 2 == 0 for v in nums)
3+
return sum(len(str(x)) % 2 == 0 for x in nums)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function findNumbers(nums: number[]): number {
2+
return nums.filter(x => x.toString().length % 2 === 0).length;
3+
}

‎solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README.md‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ tags:
7575

7676
### 方法一:哈希表 + 枚举
7777

78-
根据题目描述,如果一个长串满足条件,那么这个长串的子串(长度至少为 `minSize`)也一定满足条件。因此,我们只需要枚举 $s$ 中所有长度为 `minSize` 的子串,然后利用哈希表记录所有子串的出现次数,找出最大的次数作为答案即可。
78+
根据题目描述,如果一个长串满足条件,那么这个长串的长度为 $\textit{minSize}$ 的子串也一定满足条件。因此,我们只需要枚举 $s$ 中所有长度为 $\textit{minSize}$ 的子串,然后利用哈希表记录所有子串的出现次数,找出最大的次数作为答案即可。
7979

80-
时间复杂度 $O(n \times m),ドル空间复杂度 $O(n \times m)$。其中 $n$ 和 $m$ 分别为字符串 $s$ 的长度以及 `minSize` 的大小。本题中 $m$ 不超过 26ドル$。
80+
时间复杂度 $O(n \times m),ドル空间复杂度 $O(n \times m)$。其中 $n$ 和 $m$ 分别为字符串 $s$ 的长度以及 $\textit{minSize}$ 的大小。本题中 $m$ 不超过 26ドル$。
8181

8282
<!-- tabs:start -->
8383

@@ -162,6 +162,24 @@ func maxFreq(s string, maxLetters int, minSize int, maxSize int) (ans int) {
162162
}
163163
```
164164

165+
#### TypeScript
166+
167+
```ts
168+
function maxFreq(s: string, maxLetters: number, minSize: number, maxSize: number): number {
169+
const cnt = new Map<string, number>();
170+
let ans = 0;
171+
for (let i = 0; i < s.length - minSize + 1; ++i) {
172+
const t = s.slice(i, i + minSize);
173+
const ss = new Set(t.split(''));
174+
if (ss.size <= maxLetters) {
175+
cnt.set(t, (cnt.get(t) || 0) + 1);
176+
ans = Math.max(ans, cnt.get(t)!);
177+
}
178+
}
179+
return ans;
180+
}
181+
```
182+
165183
<!-- tabs:end -->
166184

167185
<!-- solution:end -->

‎solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README_EN.md‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ It satisfies the conditions, 2 unique letters and size 3 (between minSize and ma
6161

6262
<!-- solution:start -->
6363

64-
### Solution 1
64+
### Solution 1: Hash Table + Enumeration
65+
66+
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.
67+
68+
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ドル$.
6569

6670
<!-- tabs:start -->
6771

@@ -146,6 +150,24 @@ func maxFreq(s string, maxLetters int, minSize int, maxSize int) (ans int) {
146150
}
147151
```
148152

153+
#### TypeScript
154+
155+
```ts
156+
function maxFreq(s: string, maxLetters: number, minSize: number, maxSize: number): number {
157+
const cnt = new Map<string, number>();
158+
let ans = 0;
159+
for (let i = 0; i < s.length - minSize + 1; ++i) {
160+
const t = s.slice(i, i + minSize);
161+
const ss = new Set(t.split(''));
162+
if (ss.size <= maxLetters) {
163+
cnt.set(t, (cnt.get(t) || 0) + 1);
164+
ans = Math.max(ans, cnt.get(t)!);
165+
}
166+
}
167+
return ans;
168+
}
169+
```
170+
149171
<!-- tabs:end -->
150172

151173
<!-- solution:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /