Skip to content

Commit 4cc289a

Browse files
committed
feat: add solutions to lc problem: No.2180
No.2180.Count Integers With Even Digit Sum
1 parent fc27d5e commit 4cc289a

File tree

7 files changed

+245
-96
lines changed

7 files changed

+245
-96
lines changed

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

+119-31
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:枚举**
47+
48+
一种最简单直接的方法是枚举 $[1,..num]$ 的所有整数 $x$,判断 $x$ 各位数字之和是否为偶数,是则答案加一。
49+
50+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(1)$。其中 $n$ 为 $num$ 的值。
51+
52+
**方法二:数学**
53+
54+
我们观察发现,在 $[0,..x]$ 的所有数中,每 $10$ 个数中,就有 $5$ 个数的各位数字之和为偶数。例如,$[0,..9]$ 中,每 $10$ 个数中,就有 $5$ 个数的各位数字之和为偶数,分别是 $0,2,4,6,8$。
55+
56+
因此,我们可以先算出 $num$ 中有多少个 $10$ 的倍数,然后乘以 $5$ 再减去 $1$(排除 $0$ 这个偶数),可以得到初始答案 $ans=\left\lfloor \frac{num}{10} \right\rfloor \times 5 - 1$。
57+
58+
接下来,我们还需要考虑剩下的 $num \% 10 + 1$ 个数字中,有多少个数的各位数字之和为偶数。这些数字是否是偶数,跟数字的前面数字之和有关,因此,我们可以算出 $num$ 的前面数字之和 $s$,那么剩余的数字中,还有 $\left\lfloor \frac{num \% 10 + 2 - (s \& 1)}{2} \right\rfloor$ 个数的各位数字之和为偶数。累加到答案 $ans$ 中即可。
59+
60+
我们不妨举个例子,假设 $num$ 为 $123$,那么前面 $[0,..119]$ 中一共有 $12$ 个 $10$ 的倍数,每个 $10$ 的倍数中有 $5$ 个数的各位数字之和为偶数,因此,初始答案为 $ans=12 \times 5 - 1=59$。
61+
62+
剩下的数字分别是 $120,121,122,123$,每个数字的前两位数字之和为 $s = 1+2=3$,是奇数,因此,剩下的数字中,只有 $2$ 个数的各位数字之和为偶数,累加到答案 $ans$ 中,最终答案为 $ans+2=61$。
63+
64+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为 $num$ 的值。
65+
4666
<!-- tabs:start -->
4767

4868
### **Python3**
@@ -53,13 +73,24 @@
5373
class Solution:
5474
def countEven(self, num: int) -> int:
5575
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
76+
for x in range(1, num + 1):
77+
s = 0
78+
while x:
79+
s += x % 10
80+
x //= 10
81+
ans += s % 2 == 0
82+
return ans
83+
```
84+
85+
```python
86+
class Solution:
87+
def countEven(self, num: int) -> int:
88+
ans = num // 10 * 5 - 1
89+
x, s = num // 10, 0
90+
while x:
91+
s += x % 10
92+
x //= 10
93+
ans += (num % 10 + 2 - (s & 1)) >> 1
6394
return ans
6495
```
6596

@@ -72,12 +103,11 @@ class Solution {
72103
public int countEven(int num) {
73104
int ans = 0;
74105
for (int i = 1; i <= num; ++i) {
75-
int j = i, t = 0;
76-
while (j > 0) {
77-
t += j % 10;
78-
j /= 10;
106+
int s = 0;
107+
for (int x = i; x > 0; x /= 10) {
108+
s += x % 10;
79109
}
80-
if (t % 2 == 0) {
110+
if (s % 2 == 0) {
81111
++ans;
82112
}
83113
}
@@ -86,17 +116,17 @@ class Solution {
86116
}
87117
```
88118

89-
### **TypeScript**
90-
91-
```ts
92-
function countEven(num: number): number {
93-
let ans = 0;
94-
for (let i = 2; i <= num; i++) {
95-
if ([...String(i)].reduce((a, c) => a + Number(c), 0) % 2 == 0) {
96-
ans++;
119+
```java
120+
class Solution {
121+
public int countEven(int num) {
122+
int ans = num / 10 * 5 - 1;
123+
int s = 0;
124+
for (int x = num / 10; x > 0; x /= 10) {
125+
s += x % 10;
97126
}
127+
ans += (num % 10 + 2 - (s & 1)) >> 1;
128+
return ans;
98129
}
99-
return ans;
100130
}
101131
```
102132

@@ -108,30 +138,88 @@ public:
108138
int countEven(int num) {
109139
int ans = 0;
110140
for (int i = 1; i <= num; ++i) {
111-
int t = 0;
112-
for (int j = i; j; j /= 10) t += j % 10;
113-
if (t % 2 == 0) ++ans;
141+
int s = 0;
142+
for (int x = i; x; x /= 10) {
143+
s += x % 10;
144+
}
145+
ans += s % 2 == 0;
114146
}
115147
return ans;
116148
}
117149
};
118150
```
119151
152+
```cpp
153+
class Solution {
154+
public:
155+
int countEven(int num) {
156+
int ans = num / 10 * 5 - 1;
157+
int s = 0;
158+
for (int x = num / 10; x > 0; x /= 10) {
159+
s += x % 10;
160+
}
161+
ans += (num % 10 + 2 - (s & 1)) >> 1;
162+
return ans;
163+
}
164+
};
165+
```
166+
120167
### **Go**
121168

122169
```go
123-
func countEven(num int) int {
124-
ans := 0
170+
func countEven(num int) (ans int) {
125171
for i := 1; i <= num; i++ {
126-
t := 0
127-
for j := i; j > 0; j /= 10 {
128-
t += j % 10
172+
s := 0
173+
for x := i; x > 0; x /= 10 {
174+
s += x % 10
129175
}
130-
if t%2 == 0 {
176+
if s%2 == 0 {
131177
ans++
132178
}
133179
}
134-
return ans
180+
return
181+
}
182+
```
183+
184+
```go
185+
func countEven(num int) (ans int) {
186+
ans = num/10*5 - 1
187+
s := 0
188+
for x := num / 10; x > 0; x /= 10 {
189+
s += x % 10
190+
}
191+
ans += (num%10 + 2 - (s & 1)) >> 1
192+
return
193+
}
194+
```
195+
196+
### **TypeScript**
197+
198+
```ts
199+
function countEven(num: number): number {
200+
let ans = 0;
201+
for (let i = 1; i <= num; ++i) {
202+
let s = 0;
203+
for (let x = i; x; x = Math.floor(x / 10)) {
204+
s += x % 10;
205+
}
206+
if (s % 2 == 0) {
207+
++ans;
208+
}
209+
}
210+
return ans;
211+
}
212+
```
213+
214+
```ts
215+
function countEven(num: number): number {
216+
let ans = Math.floor(num / 10) * 5 - 1;
217+
let s = 0;
218+
for (let x = Math.floor(num / 10); x; x = Math.floor(x / 10)) {
219+
s += x % 10;
220+
}
221+
ans += ((num % 10) + 2 - (s & 1)) >> 1;
222+
return ans;
135223
}
136224
```
137225

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

+99-31
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,24 @@ The 14 integers less than or equal to 30 whose digit sums are even are
4545
class Solution:
4646
def countEven(self, num: int) -> int:
4747
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
48+
for x in range(1, num + 1):
49+
s = 0
50+
while x:
51+
s += x % 10
52+
x //= 10
53+
ans += s % 2 == 0
54+
return ans
55+
```
56+
57+
```python
58+
class Solution:
59+
def countEven(self, num: int) -> int:
60+
ans = num // 10 * 5 - 1
61+
x, s = num // 10, 0
62+
while x:
63+
s += x % 10
64+
x //= 10
65+
ans += (num % 10 + 2 - (s & 1)) >> 1
5566
return ans
5667
```
5768

@@ -62,12 +73,11 @@ class Solution {
6273
public int countEven(int num) {
6374
int ans = 0;
6475
for (int i = 1; i <= num; ++i) {
65-
int j = i, t = 0;
66-
while (j > 0) {
67-
t += j % 10;
68-
j /= 10;
76+
int s = 0;
77+
for (int x = i; x > 0; x /= 10) {
78+
s += x % 10;
6979
}
70-
if (t % 2 == 0) {
80+
if (s % 2 == 0) {
7181
++ans;
7282
}
7383
}
@@ -76,17 +86,17 @@ class Solution {
7686
}
7787
```
7888

79-
### **TypeScript**
80-
81-
```ts
82-
function countEven(num: number): number {
83-
let ans = 0;
84-
for (let i = 2; i <= num; i++) {
85-
if ([...String(i)].reduce((a, c) => a + Number(c), 0) % 2 == 0) {
86-
ans++;
89+
```java
90+
class Solution {
91+
public int countEven(int num) {
92+
int ans = num / 10 * 5 - 1;
93+
int s = 0;
94+
for (int x = num / 10; x > 0; x /= 10) {
95+
s += x % 10;
8796
}
97+
ans += (num % 10 + 2 - (s & 1)) >> 1;
98+
return ans;
8899
}
89-
return ans;
90100
}
91101
```
92102

@@ -98,30 +108,88 @@ public:
98108
int countEven(int num) {
99109
int ans = 0;
100110
for (int i = 1; i <= num; ++i) {
101-
int t = 0;
102-
for (int j = i; j; j /= 10) t += j % 10;
103-
if (t % 2 == 0) ++ans;
111+
int s = 0;
112+
for (int x = i; x; x /= 10) {
113+
s += x % 10;
114+
}
115+
ans += s % 2 == 0;
104116
}
105117
return ans;
106118
}
107119
};
108120
```
109121
122+
```cpp
123+
class Solution {
124+
public:
125+
int countEven(int num) {
126+
int ans = num / 10 * 5 - 1;
127+
int s = 0;
128+
for (int x = num / 10; x > 0; x /= 10) {
129+
s += x % 10;
130+
}
131+
ans += (num % 10 + 2 - (s & 1)) >> 1;
132+
return ans;
133+
}
134+
};
135+
```
136+
110137
### **Go**
111138

112139
```go
113-
func countEven(num int) int {
114-
ans := 0
140+
func countEven(num int) (ans int) {
115141
for i := 1; i <= num; i++ {
116-
t := 0
117-
for j := i; j > 0; j /= 10 {
118-
t += j % 10
142+
s := 0
143+
for x := i; x > 0; x /= 10 {
144+
s += x % 10
119145
}
120-
if t%2 == 0 {
146+
if s%2 == 0 {
121147
ans++
122148
}
123149
}
124-
return ans
150+
return
151+
}
152+
```
153+
154+
```go
155+
func countEven(num int) (ans int) {
156+
ans = num/10*5 - 1
157+
s := 0
158+
for x := num / 10; x > 0; x /= 10 {
159+
s += x % 10
160+
}
161+
ans += (num%10 + 2 - (s & 1)) >> 1
162+
return
163+
}
164+
```
165+
166+
### **TypeScript**
167+
168+
```ts
169+
function countEven(num: number): number {
170+
let ans = 0;
171+
for (let i = 1; i <= num; ++i) {
172+
let s = 0;
173+
for (let x = i; x; x = Math.floor(x / 10)) {
174+
s += x % 10;
175+
}
176+
if (s % 2 == 0) {
177+
++ans;
178+
}
179+
}
180+
return ans;
181+
}
182+
```
183+
184+
```ts
185+
function countEven(num: number): number {
186+
let ans = Math.floor(num / 10) * 5 - 1;
187+
let s = 0;
188+
for (let x = Math.floor(num / 10); x; x = Math.floor(x / 10)) {
189+
s += x % 10;
190+
}
191+
ans += ((num % 10) + 2 - (s & 1)) >> 1;
192+
return ans;
125193
}
126194
```
127195

Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public:
33
int countEven(int num) {
4-
int ans = 0;
5-
for (int i = 1; i <= num; ++i) {
6-
int t = 0;
7-
for (int j = i; j; j /= 10) t += j % 10;
8-
if (t % 2 == 0) ++ans;
4+
int ans = num / 10 * 5 - 1;
5+
int s = 0;
6+
for (int x = num / 10; x > 0; x /= 10) {
7+
s += x % 10;
98
}
9+
ans += (num % 10 + 2 - (s & 1)) >> 1;
1010
return ans;
1111
}
1212
};

0 commit comments

Comments
 (0)