Skip to content

Add solution and test-cases for problem 1295 #1192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# [1295.Find Numbers with Even Number of Digits][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given an array `nums` of integers, return how many of them contain an **even number** of digits.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [12,345,2,6,7896]
Output: 2
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).
6 contains 1 digit (odd number of digits).
7896 contains 4 digits (even number of digits).
Therefore only 12 and 7896 contain an even number of digits.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Find Numbers with Even Number of Digits
```go
```

Input: nums = [555,901,482,1771]
Output: 1
Explanation:
Only 1771 contains an even number of digits.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,22 @@ func Solution(nums []int) int {
}
return count
}

func Solution1(nums []int) int {
var isEven func(int) bool
isEven = func(n int) bool {
c := 0
for n > 0 {
n /= 10
c++
}
return c&1 == 0
}
ans := 0
for _, n := range nums {
if isEven(n) {
ans++
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,34 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
func TestSolution1(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs []int
expect int
}{
{"TestCase", []int{124123, 13425, 123, 65, 1, 54362, 134, 6543, 213}, 3},
{"TestCase", []int{}, 0},
{"TestCase", []int{4253626756, 3245876, 2345897, 23490}, 1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution1(c.inputs)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
}
})
}
}

// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading