From 2fdec23e4afa7f2db0d02a09a9c395baca033617 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 30 Apr 2025 09:19:10 +0800 Subject: [PATCH] Add solution and test-cases for problem 1295 --- .../README.md | 30 ++++++++++--------- .../Solution.go | 19 ++++++++++++ .../Solution_test.go | 28 +++++++++++++++-- 3 files changed, 61 insertions(+), 16 deletions(-) diff --git a/leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/README.md b/leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/README.md index 5b890ee55..ab876a74b 100644 --- a/leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/README.md +++ b/leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/Solution.go b/leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/Solution.go index 629717485..44ce560ac 100644 --- a/leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/Solution.go +++ b/leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/Solution.go @@ -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 +} diff --git a/leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/Solution_test.go b/leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/Solution_test.go index 3326b3f71..46dcd69da 100644 --- a/leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/Solution_test.go +++ b/leetcode/1201-1300/1295.Find-Numbers-with-Even-Number-of-Digits/Solution_test.go @@ -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() { }