Skip to content

Add solution and test-cases for problem 2843 #1173

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
Apr 11, 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
31 changes: 31 additions & 0 deletions leetcode/2801-2900/2843.Count-Symmetric-Integers/README.md
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions leetcode/2801-2900/2843.Count-Symmetric-Integers/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions leetcode/2801-2900/2843.Count-Symmetric-Integers/Solution_test.go
Original file line number Diff line number Diff line change
@@ -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() {
}
Loading