Skip to content

Commit 9a4f7a1

Browse files
committed
feat: add new lc problems and solutions
1 parent cd8e915 commit 9a4f7a1

File tree

22 files changed

+873
-28
lines changed

22 files changed

+873
-28
lines changed

solution/2100-2199/2180.Count Integers With Even Digit Sum/README.md

+63-2
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,40 @@
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```python
53-
53+
class Solution:
54+
def countEven(self, num: int) -> int:
55+
ans = 0
56+
for i in range(1, num + 1):
57+
t = 0
58+
while i:
59+
t += i % 10
60+
i //= 10
61+
if t % 2 == 0:
62+
ans += 1
63+
return ans
5464
```
5565

5666
### **Java**
5767

5868
<!-- 这里可写当前语言的特殊实现逻辑 -->
5969

6070
```java
61-
71+
class Solution {
72+
public int countEven(int num) {
73+
int ans = 0;
74+
for (int i = 1; i <= num; ++i) {
75+
int j = i, t = 0;
76+
while (j > 0) {
77+
t += j % 10;
78+
j /= 10;
79+
}
80+
if (t % 2 == 0) {
81+
++ans;
82+
}
83+
}
84+
return ans;
85+
}
86+
}
6287
```
6388

6489
### **TypeScript**
@@ -75,6 +100,42 @@ function countEven(num: number): number {
75100
}
76101
```
77102

103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int countEven(int num) {
109+
int ans = 0;
110+
for (int i = 1; i <= num; ++i)
111+
{
112+
int t = 0;
113+
for (int j = i; j; j /= 10) t += j % 10;
114+
if (t % 2 == 0) ++ans;
115+
}
116+
return ans;
117+
}
118+
};
119+
```
120+
121+
### **Go**
122+
123+
```go
124+
func countEven(num int) int {
125+
ans := 0
126+
for i := 1; i <= num; i++ {
127+
t := 0
128+
for j := i; j > 0; j /= 10 {
129+
t += j % 10
130+
}
131+
if t%2 == 0 {
132+
ans++
133+
}
134+
}
135+
return ans
136+
}
137+
```
138+
78139
### **...**
79140

80141
```

solution/2100-2199/2180.Count Integers With Even Digit Sum/README_EN.md

+63-2
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,38 @@ The 14 integers less than or equal to 30 whose digit sums are even are
4242
### **Python3**
4343

4444
```python
45-
45+
class Solution:
46+
def countEven(self, num: int) -> int:
47+
ans = 0
48+
for i in range(1, num + 1):
49+
t = 0
50+
while i:
51+
t += i % 10
52+
i //= 10
53+
if t % 2 == 0:
54+
ans += 1
55+
return ans
4656
```
4757

4858
### **Java**
4959

5060
```java
51-
61+
class Solution {
62+
public int countEven(int num) {
63+
int ans = 0;
64+
for (int i = 1; i <= num; ++i) {
65+
int j = i, t = 0;
66+
while (j > 0) {
67+
t += j % 10;
68+
j /= 10;
69+
}
70+
if (t % 2 == 0) {
71+
++ans;
72+
}
73+
}
74+
return ans;
75+
}
76+
}
5277
```
5378

5479
### **TypeScript**
@@ -65,6 +90,42 @@ function countEven(num: number): number {
6590
}
6691
```
6792

93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
int countEven(int num) {
99+
int ans = 0;
100+
for (int i = 1; i <= num; ++i)
101+
{
102+
int t = 0;
103+
for (int j = i; j; j /= 10) t += j % 10;
104+
if (t % 2 == 0) ++ans;
105+
}
106+
return ans;
107+
}
108+
};
109+
```
110+
111+
### **Go**
112+
113+
```go
114+
func countEven(num int) int {
115+
ans := 0
116+
for i := 1; i <= num; i++ {
117+
t := 0
118+
for j := i; j > 0; j /= 10 {
119+
t += j % 10
120+
}
121+
if t%2 == 0 {
122+
ans++
123+
}
124+
}
125+
return ans
126+
}
127+
```
128+
68129
### **...**
69130

70131
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int countEven(int num) {
4+
int ans = 0;
5+
for (int i = 1; i <= num; ++i)
6+
{
7+
int t = 0;
8+
for (int j = i; j; j /= 10) t += j % 10;
9+
if (t % 2 == 0) ++ans;
10+
}
11+
return ans;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func countEven(num int) int {
2+
ans := 0
3+
for i := 1; i <= num; i++ {
4+
t := 0
5+
for j := i; j > 0; j /= 10 {
6+
t += j % 10
7+
}
8+
if t%2 == 0 {
9+
ans++
10+
}
11+
}
12+
return ans
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int countEven(int num) {
3+
int ans = 0;
4+
for (int i = 1; i <= num; ++i) {
5+
int j = i, t = 0;
6+
while (j > 0) {
7+
t += j % 10;
8+
j /= 10;
9+
}
10+
if (t % 2 == 0) {
11+
++ans;
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def countEven(self, num: int) -> int:
3+
ans = 0
4+
for i in range(1, num + 1):
5+
t = 0
6+
while i:
7+
t += i % 10
8+
i //= 10
9+
if t % 2 == 0:
10+
ans += 1
11+
return ans

solution/2100-2199/2182.Construct String With Repeat Limit/README.md

+123-1
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,144 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:贪心 + 双指针**
57+
5658
<!-- tabs:start -->
5759

5860
### **Python3**
5961

6062
<!-- 这里可写当前语言的特殊实现逻辑 -->
6163

6264
```python
63-
65+
class Solution:
66+
def repeatLimitedString(self, s: str, repeatLimit: int) -> str:
67+
cnt = [0] * 26
68+
for c in s:
69+
cnt[ord(c) - ord('a')] += 1
70+
ans = []
71+
for i in range(25, -1, -1):
72+
j = i - 1
73+
while 1:
74+
for _ in range(min(repeatLimit, cnt[i])):
75+
cnt[i] -= 1
76+
ans.append(chr(ord('a') + i))
77+
if cnt[i] == 0:
78+
break
79+
while j >= 0 and cnt[j] == 0:
80+
j -= 1
81+
if j < 0:
82+
break
83+
cnt[j] -= 1
84+
ans.append(chr(ord('a') + j))
85+
return ''.join(ans)
6486
```
6587

6688
### **Java**
6789

6890
<!-- 这里可写当前语言的特殊实现逻辑 -->
6991

7092
```java
93+
class Solution {
94+
public String repeatLimitedString(String s, int repeatLimit) {
95+
int[] cnt = new int[26];
96+
for (char c : s.toCharArray()) {
97+
cnt[c - 'a']++;
98+
}
99+
StringBuilder ans = new StringBuilder();
100+
for (int i = 25; i >= 0; --i) {
101+
int j = i - 1;
102+
while (true) {
103+
for (int k = Math.min(repeatLimit, cnt[i]); k > 0; --k) {
104+
cnt[i]--;
105+
ans.append((char) ('a' + i));
106+
}
107+
if (cnt[i] == 0) {
108+
break;
109+
}
110+
while (j >= 0 && cnt[j] == 0) {
111+
--j;
112+
}
113+
if (j < 0) {
114+
break;
115+
}
116+
cnt[j]--;
117+
ans.append((char) ('a' + j));
118+
}
119+
}
120+
return ans.toString();
121+
}
122+
}
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
string repeatLimitedString(string s, int repeatLimit) {
131+
vector<int> cnt(26);
132+
for (char& c : s) cnt[c - 'a']++;
133+
string ans;
134+
for (int i = 25; ~i; --i)
135+
{
136+
int j = i - 1;
137+
while (true)
138+
{
139+
for (int k = min(cnt[i], repeatLimit); k; --k)
140+
{
141+
cnt[i]--;
142+
ans.push_back('a' + i);
143+
}
144+
if (cnt[i] == 0) break;
145+
while (~j && cnt[j] == 0) --j;
146+
if (j < 0) break;
147+
cnt[j]--;
148+
ans.push_back('a' + j);
149+
}
150+
}
151+
return ans;
152+
}
153+
};
154+
```
71155
156+
### **Go**
157+
158+
```go
159+
func repeatLimitedString(s string, repeatLimit int) string {
160+
cnt := make([]int, 26)
161+
for _, c := range s {
162+
cnt[c-'a']++
163+
}
164+
var ans []byte
165+
for i := 25; i >= 0; i-- {
166+
j := i - 1
167+
for {
168+
for k := min(cnt[i], repeatLimit); k > 0; k-- {
169+
cnt[i]--
170+
ans = append(ans, 'a'+byte(i))
171+
}
172+
if cnt[i] == 0 {
173+
break
174+
}
175+
for j >= 0 && cnt[j] == 0 {
176+
j--
177+
}
178+
if j < 0 {
179+
break
180+
}
181+
cnt[j]--
182+
ans = append(ans, 'a'+byte(j))
183+
}
184+
}
185+
return string(ans)
186+
}
187+
188+
func min(a, b int) int {
189+
if a < b {
190+
return a
191+
}
192+
return b
193+
}
72194
```
73195

74196
### **TypeScript**

0 commit comments

Comments
 (0)