Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Leetcode Qs-2081#789

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
SrishtiSinha2003 wants to merge6 commits intocodemistic:main
base:main
Choose a base branch
Loading
fromSrishtiSinha2003:main
Open
Show file tree
Hide file tree
Changes fromall 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
62 changes: 62 additions & 0 deletions2081. Sum of k-Mirror Numbers/README.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
# 2081. Sum of k-Mirror Numbers
Hard
---
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. Its representation in base-10 is 9, and in base-2 is 1001, both of which are palindromes.

- 4 is not a 2-mirror number because its base-2 form is 100, which is not a palindrome.

Given the base k and the number n, return the sum of the n smallest k-mirror numbers.

### Example 1
```
Input: k = 2, n = 5
Output: 25
Explanation:
The 5 smallest 2-mirror numbers and their base-2 representations are:

base-10 base-2
1 1
3 11
5 101
7 111
9 1001

Sum = 1 + 3 + 5 + 7 + 9 = 25.
```

### Example 2
```
Input: k = 3, n = 7
Output: 499
Explanation:
The 7 smallest 3-mirror numbers and their base-3 representations are:

base-10 base-3
1 1
2 2
4 11
8 22
121 11111
151 12121
212 21212

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
```

# Constraints
```
2 <= k <= 9
1 <= n <= 30
```
58 changes: 58 additions & 0 deletions2081. Sum of k-Mirror Numbers/Solution.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
public class Solution {
public long kMirror(int k, int n) {
long ans = 0;
int cnt = 0;
long[] pow10 = new long[12];
pow10[0] = 1;
for (int i = 1; i < 12; i++) pow10[i] = pow10[i - 1] * 10;

while (cnt < n) {
int len = 1;
for (; ; len++) {
int half = (len + 1) / 2;
long start = pow10[half - 1], end = pow10[half];
for (long h = (half == 1 ? 1 : start); h < end && cnt < n; h++) {
long t = h, rev = 0, x = h;
if ((len & 1) == 1) t /= 10;
while (t > 0) {
rev = rev * 10 + t % 10;
t /= 10;
}
long p = x * pow10[len - half] + rev;
if (isPal(p, k)) {
ans += p;
cnt++;
if (cnt == n) return ans;
}
}
}
}
return ans;
}

private boolean isPal(long x, int k) {
long t = x, rev = 0;
while (t > 0) {
rev = rev * k + t % k;
t /= k;
}
return rev == x;
}

// Main function to test the solution
public static void main(String[] args) {
Solution sol = new Solution();

int k1 = 2, n1 = 5;
System.out.println("Input: k = " + k1 + ", n = " + n1);
System.out.println("Output: " + sol.kMirror(k1, n1)); // Expected: 25

int k2 = 3, n2 = 7;
System.out.println("\nInput: k = " + k2 + ", n = " + n2);
System.out.println("Output: " + sol.kMirror(k2, n2)); // Expected: 499

int k3 = 7, n3 = 17;
System.out.println("\nInput: k = " + k3 + ", n = " + n3);
System.out.println("Output: " + sol.kMirror(k3, n3)); // Expected: 20379000
}
}

[8]ページ先頭

©2009-2025 Movatter.jp