diff --git a/leetcode/2001-2100/2081.Sum-of-k-Mirror-Numbers/README.md b/leetcode/2001-2100/2081.Sum-of-k-Mirror-Numbers/README.md index c3079388b..353dcaeee 100755 --- a/leetcode/2001-2100/2081.Sum-of-k-Mirror-Numbers/README.md +++ b/leetcode/2001-2100/2081.Sum-of-k-Mirror-Numbers/README.md @@ -1,28 +1,56 @@ # [2081.Sum of k-Mirror Numbers][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 +A **k-mirror number** is a **positive** integer **without leading zeros** that reads the same both forward and backward in base-10 **as well as** in base-k. + +- For example, `9` is a 2-mirror number. The representation of `9` in base-10 and base-2 are 9 and `1001` respectively, which read the same both forward and backward. +- On the contrary, `4` is not a 2-mirror number. The representation of `4` in base-2 is `100`, which does not read the same both forward and backward. + +Given the base `k` and the number `n`, return the **sum** of the `n` **smallest** k-mirror numbers. + **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: k = 2, n = 5 +Output: 25 +Explanation: +The 5 smallest 2-mirror numbers and their representations in base-2 are listed as follows: + base-10 base-2 + 1 1 + 3 11 + 5 101 + 7 111 + 9 1001 +Their sum = 1 + 3 + 5 + 7 + 9 = 25. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Sum of k-Mirror Numbers -```go ``` +Input: k = 3, n = 7 +Output: 499 +Explanation: +The 7 smallest 3-mirror numbers are and their representations in base-3 are listed as follows: + base-10 base-3 + 1 1 + 2 2 + 4 11 + 8 22 + 121 11111 + 151 12121 + 212 21212 +Their sum = 1 + 2 + 4 + 8 + 121 + 151 + 212 = 499. +``` + +**Example 3:** +``` +Input: k = 7, n = 17 +Output: 20379000 +Explanation: The 17 smallest 7-mirror numbers are: +1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596 +``` ## 结语 diff --git a/leetcode/2001-2100/2081.Sum-of-k-Mirror-Numbers/Solution.go b/leetcode/2001-2100/2081.Sum-of-k-Mirror-Numbers/Solution.go index d115ccf5e..5ce6f0ea3 100644 --- a/leetcode/2001-2100/2081.Sum-of-k-Mirror-Numbers/Solution.go +++ b/leetcode/2001-2100/2081.Sum-of-k-Mirror-Numbers/Solution.go @@ -1,5 +1,44 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(k int, n int) int64 { + digit := make([]int, 100) + left, count, ans := 1, 0, int64(0) + for count < n { + right := left * 10 + for op := 0; op < 2; op++ { + // enumerate i' + for i := left; i < right && count < n; i++ { + combined := int64(i) + x := i + if op == 0 { + x = i / 10 + } + for x > 0 { + combined = combined*10 + int64(x%10) + x /= 10 + } + if isPalindrome(combined, k, digit) { + count++ + ans += combined + } + } + } + left = right + } + return ans +} + +func isPalindrome(x int64, k int, digit []int) bool { + length := -1 + for x > 0 { + length++ + digit[length] = int(x % int64(k)) + x /= int64(k) + } + for i, j := 0, length; i < j; i, j = i+1, j-1 { + if digit[i] != digit[j] { + return false + } + } + return true } diff --git a/leetcode/2001-2100/2081.Sum-of-k-Mirror-Numbers/Solution_test.go b/leetcode/2001-2100/2081.Sum-of-k-Mirror-Numbers/Solution_test.go index 14ff50eb4..fa6a7875b 100644 --- a/leetcode/2001-2100/2081.Sum-of-k-Mirror-Numbers/Solution_test.go +++ b/leetcode/2001-2100/2081.Sum-of-k-Mirror-Numbers/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + k, n int + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 2, 5, 25}, + {"TestCase2", 3, 7, 499}, + {"TestCase3", 7, 17, 20379000}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.k, c.n) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.k, c.n) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }