Skip to content

Commit 2fdec23

Browse files
committed
Add solution and test-cases for problem 1295
1 parent a01e89c commit 2fdec23

File tree

3 files changed

+61
-16
lines changed

3 files changed

+61
-16
lines changed

leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [1295.Find Numbers with Even Number of Digits][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an array `nums` of integers, return how many of them contain an **even number** of digits.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: nums = [12,345,2,6,7896]
10+
Output: 2
11+
Explanation:
12+
12 contains 2 digits (even number of digits).
13+
345 contains 3 digits (odd number of digits).
14+
2 contains 1 digit (odd number of digits).
15+
6 contains 1 digit (odd number of digits).
16+
7896 contains 4 digits (even number of digits).
17+
Therefore only 12 and 7896 contain an even number of digits.
1318
```
1419

15-
## 题意
16-
> ...
17-
18-
## 题解
20+
**Example 2:**
1921

20-
### 思路1
21-
> ...
22-
Find Numbers with Even Number of Digits
23-
```go
2422
```
25-
23+
Input: nums = [555,901,482,1771]
24+
Output: 1
25+
Explanation:
26+
Only 1771 contains an even number of digits.
27+
```
2628

2729
## 结语
2830

leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/Solution.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,22 @@ func Solution(nums []int) int {
1212
}
1313
return count
1414
}
15+
16+
func Solution1(nums []int) int {
17+
var isEven func(int) bool
18+
isEven = func(n int) bool {
19+
c := 0
20+
for n > 0 {
21+
n /= 10
22+
c++
23+
}
24+
return c&1 == 0
25+
}
26+
ans := 0
27+
for _, n := range nums {
28+
if isEven(n) {
29+
ans++
30+
}
31+
}
32+
return ans
33+
}

leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/Solution_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,34 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
func TestSolution1(t *testing.T) {
34+
// 测试用例
35+
cases := []struct {
36+
name string
37+
inputs []int
38+
expect int
39+
}{
40+
{"TestCase", []int{124123, 13425, 123, 65, 1, 54362, 134, 6543, 213}, 3},
41+
{"TestCase", []int{}, 0},
42+
{"TestCase", []int{4253626756, 3245876, 2345897, 23490}, 1},
43+
}
44+
45+
// 开始测试
46+
for i, c := range cases {
47+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
48+
got := Solution1(c.inputs)
49+
if !reflect.DeepEqual(got, c.expect) {
50+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
51+
c.expect, got, c.inputs)
52+
}
53+
})
54+
}
55+
}
56+
57+
// 压力测试
3458
func BenchmarkSolution(b *testing.B) {
3559
}
3660

37-
// 使用案列
61+
// 使用案列
3862
func ExampleSolution() {
3963
}

0 commit comments

Comments
 (0)