From 74f98b8b80421ba9eb88c27616e1082534763cbb Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 11 Apr 2025 09:09:59 +0800 Subject: [PATCH] Add solution and test-cases for problem 2843 --- .../2843.Count-Symmetric-Integers/README.md | 31 +++++++++++++++ .../2843.Count-Symmetric-Integers/Solution.go | 28 ++++++++++++++ .../Solution_test.go | 38 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 leetcode/2801-2900/2843.Count-Symmetric-Integers/README.md create mode 100755 leetcode/2801-2900/2843.Count-Symmetric-Integers/Solution.go create mode 100755 leetcode/2801-2900/2843.Count-Symmetric-Integers/Solution_test.go diff --git a/leetcode/2801-2900/2843.Count-Symmetric-Integers/README.md b/leetcode/2801-2900/2843.Count-Symmetric-Integers/README.md new file mode 100644 index 000000000..9e78277fb --- /dev/null +++ b/leetcode/2801-2900/2843.Count-Symmetric-Integers/README.md @@ -0,0 +1,31 @@ +# [2843.Count Symmetric Integers][title] + +## Description +You are given two positive integers `low` and `high`. + +An integer `x` consisting of `2 * n` digits is **symmetric** if the sum of the first `n` digits of `x` is equal to the sum of the last `n` digits of `x`. Numbers with an odd number of digits are never symmetric. + +Return the **number of symmetric** integers in the range `[low, high]`. + +**Example 1:** + +``` +Input: low = 1, high = 100 +Output: 9 +Explanation: There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99. +``` + +**Example 2:** + +``` +Input: low = 1200, high = 1230 +Output: 4 +Explanation: There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/count-symmetric-integers +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2801-2900/2843.Count-Symmetric-Integers/Solution.go b/leetcode/2801-2900/2843.Count-Symmetric-Integers/Solution.go new file mode 100755 index 000000000..90e77f01c --- /dev/null +++ b/leetcode/2801-2900/2843.Count-Symmetric-Integers/Solution.go @@ -0,0 +1,28 @@ +package Solution + +import "fmt" + +func Solution(low int, high int) int { + var ok func(int) bool + ok = func(n int) bool { + s := fmt.Sprintf("%d", n) + l := len(s) + if l&1 == 1 { + return false + } + // 1 2, 3, 4 + left, right := 0, 0 + for start, end := 0, l-1; start < end; start, end = start+1, end-1 { + left += int(s[start] - '0') + right += int(s[end] - '0') + } + return left == right + } + cnt := 0 + for i := low; i <= high; i++ { + if ok(i) { + cnt++ + } + } + return cnt +} diff --git a/leetcode/2801-2900/2843.Count-Symmetric-Integers/Solution_test.go b/leetcode/2801-2900/2843.Count-Symmetric-Integers/Solution_test.go new file mode 100755 index 000000000..d5b13173c --- /dev/null +++ b/leetcode/2801-2900/2843.Count-Symmetric-Integers/Solution_test.go @@ -0,0 +1,38 @@ +package Solution + +import ( + "reflect" + "strconv" + "testing" +) + +func TestSolution(t *testing.T) { + // 测试用例 + cases := []struct { + name string + low, high int + expect int + }{ + {"TestCase1", 1, 100, 9}, + {"TestCase2", 1200, 1230, 4}, + } + + // 开始测试 + for i, c := range cases { + t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { + got := Solution(c.low, c.high) + if !reflect.DeepEqual(got, c.expect) { + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.low, c.high) + } + }) + } +} + +// 压力测试 +func BenchmarkSolution(b *testing.B) { +} + +// 使用案列 +func ExampleSolution() { +}