Skip to content

Commit 1237cde

Browse files
committed
+ problem 2081
1 parent 59b59ff commit 1237cde

File tree

5 files changed

+204
-0
lines changed

5 files changed

+204
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# 2081. Sum of k-Mirror Numbers
2+
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.
3+
4+
* 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.
5+
* 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.
6+
7+
Given the base `k` and the number `n`, return *the **sum** of the* `n` ***smallest** k-mirror numbers*.
8+
9+
#### Example 1:
10+
<pre>
11+
<strong>Input:</strong> k = 2, n = 5
12+
<strong>Output:</strong> 25
13+
<strong>Explanation:</strong>
14+
The 5 smallest 2-mirror numbers and their representations in base-2 are listed as follows:
15+
base-10 base-2
16+
1 1
17+
3 11
18+
5 101
19+
7 111
20+
9 1001
21+
Their sum = 1 + 3 + 5 + 7 + 9 = 25.
22+
</pre>
23+
24+
#### Example 2:
25+
<pre>
26+
<strong>Input:</strong> k = 3, n = 7
27+
<strong>Output:</strong> 499
28+
<strong>Explanation:</strong>
29+
The 7 smallest 3-mirror numbers are and their representations in base-3 are listed as follows:
30+
base-10 base-3
31+
1 1
32+
2 2
33+
4 11
34+
8 22
35+
121 11111
36+
151 12121
37+
212 21212
38+
Their sum = 1 + 2 + 4 + 8 + 121 + 151 + 212 = 499.
39+
</pre>
40+
41+
#### Example 3:
42+
<pre>
43+
<strong>Input:</strong> k = 7, n = 17
44+
<strong>Output:</strong> 20379000
45+
<strong>Explanation:</strong> The 17 smallest 7-mirror numbers are:
46+
1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596
47+
</pre>
48+
49+
#### Constraints:
50+
* `2 <= k <= 9`
51+
* `1 <= n <= 30`
52+
53+
## Solutions (Python)
54+
55+
### 1. Solution
56+
```Python
57+
class Solution:
58+
def kMirror(self, k: int, n: int) -> int:
59+
x = 1
60+
nums = []
61+
62+
while True:
63+
for a in range(x, 10 * x):
64+
b = c = int(str(a) + str(a)[-2::-1])
65+
d = ''
66+
while c > 0:
67+
d = str(c % k) + d
68+
c //= k
69+
if d == d[::-1]:
70+
nums.append(b)
71+
if len(nums) == n:
72+
return sum(nums)
73+
for a in range(x, 10 * x):
74+
b = c = int(str(a) + str(a)[::-1])
75+
d = ''
76+
while c > 0:
77+
d = str(c % k) + d
78+
c //= k
79+
if str(d) == str(d)[::-1]:
80+
nums.append(b)
81+
if len(nums) == n:
82+
return sum(nums)
83+
84+
x *= 10
85+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# 2081. k 镜像数字的和
2+
一个 **k 镜像数字** 指的是一个在十进制和 k 进制下从前往后读和从后往前读都一样的 **没有前导 0****** 整数。
3+
4+
* 比方说,`9` 是一个 2 镜像数字。`9` 在十进制下为 `9` ,二进制下为 `1001` ,两者从前往后读和从后往前读都一样。
5+
* 相反地,`4` 不是一个 2 镜像数字。`4` 在二进制下为 `100` ,从前往后和从后往前读不相同。
6+
7+
给你进制 `k` 和一个数字 `n` ,请你返回 k 镜像数字中 **最小**`n` 个数 **之和**
8+
9+
#### 示例 1:
10+
<pre>
11+
<strong>输入:</strong> k = 2, n = 5
12+
<strong>输出:</strong> 25
13+
<strong>解释:</strong>
14+
最小的 5 个 2 镜像数字和它们的二进制表示如下:
15+
十进制 二进制
16+
1 1
17+
3 11
18+
5 101
19+
7 111
20+
9 1001
21+
它们的和为 1 + 3 + 5 + 7 + 9 = 25 。
22+
</pre>
23+
24+
#### 示例 2:
25+
<pre>
26+
<strong>输入:</strong> k = 3, n = 7
27+
<strong>输出:</strong> 499
28+
<strong>解释:</strong>
29+
7 个最小的 3 镜像数字和它们的三进制表示如下:
30+
十进制 三进制
31+
1 1
32+
2 2
33+
4 11
34+
8 22
35+
121 11111
36+
151 12121
37+
212 21212
38+
它们的和为 1 + 2 + 4 + 8 + 121 + 151 + 212 = 499 。
39+
</pre>
40+
41+
#### 示例 3:
42+
<pre>
43+
<strong>输入:</strong> k = 7, n = 17
44+
<strong>输出:</strong> 20379000
45+
<strong>解释:</strong> 17 个最小的 7 镜像数字分别为:
46+
1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596
47+
</pre>
48+
49+
#### 提示:
50+
* `2 <= k <= 9`
51+
* `1 <= n <= 30`
52+
53+
## 题解 (Python)
54+
55+
### 1. 题解
56+
```Python
57+
class Solution:
58+
def kMirror(self, k: int, n: int) -> int:
59+
x = 1
60+
nums = []
61+
62+
while True:
63+
for a in range(x, 10 * x):
64+
b = c = int(str(a) + str(a)[-2::-1])
65+
d = ''
66+
while c > 0:
67+
d = str(c % k) + d
68+
c //= k
69+
if d == d[::-1]:
70+
nums.append(b)
71+
if len(nums) == n:
72+
return sum(nums)
73+
for a in range(x, 10 * x):
74+
b = c = int(str(a) + str(a)[::-1])
75+
d = ''
76+
while c > 0:
77+
d = str(c % k) + d
78+
c //= k
79+
if str(d) == str(d)[::-1]:
80+
nums.append(b)
81+
if len(nums) == n:
82+
return sum(nums)
83+
84+
x *= 10
85+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def kMirror(self, k: int, n: int) -> int:
3+
x = 1
4+
nums = []
5+
6+
while True:
7+
for a in range(x, 10 * x):
8+
b = c = int(str(a) + str(a)[-2::-1])
9+
d = ''
10+
while c > 0:
11+
d = str(c % k) + d
12+
c //= k
13+
if d == d[::-1]:
14+
nums.append(b)
15+
if len(nums) == n:
16+
return sum(nums)
17+
for a in range(x, 10 * x):
18+
b = c = int(str(a) + str(a)[::-1])
19+
d = ''
20+
while c > 0:
21+
d = str(c % k) + d
22+
c //= k
23+
if str(d) == str(d)[::-1]:
24+
nums.append(b)
25+
if len(nums) == n:
26+
return sum(nums)
27+
28+
x *= 10

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,7 @@
947947
[2073][2073l]|[Time Needed to Buy Tickets][2073] |![py]
948948
[2078][2078l]|[Two Furthest Houses With Different Colors][2078] |![rs]
949949
[2079][2079l]|[Watering Plants][2079] |![rs]
950+
[2081][2081l]|[Sum of k-Mirror Numbers][2081] |![py]
950951
[2085][2085l]|[Count Common Words With One Occurrence][2085] |![py]
951952
[2086][2086l]|[Minimum Number of Food Buckets to Feed the Hamsters][2086] |![rs]
952953
[2087][2087l]|[Minimum Cost Homecoming of a Robot in a Grid][2087] |![py]
@@ -2083,6 +2084,7 @@
20832084
[2073]:./Easy/2073-Time%20Needed%20to%20Buy%20Tickets/README.md#2073-time-needed-to-buy-tickets
20842085
[2078]:./Easy/2078-Two%20Furthest%20Houses%20With%20Different%20Colors/README.md#2078-two-furthest-houses-with-different-colors
20852086
[2079]:./Medium/2079-Watering%20Plants/README.md#2079-watering-plants
2087+
[2081]:./Hard/2081-Sum%20of%20k-Mirror%20Numbers/README.md#2081-sum-of-k-mirror-numbers
20862088
[2085]:./Easy/2085-Count%20Common%20Words%20With%20One%20Occurrence/README.md#2085-count-common-words-with-one-occurrence
20872089
[2086]:./Medium/2086-Minimum%20Number%20of%20Food%20Buckets%20to%20Feed%20the%20Hamsters/README.md#2086-minimum-number-of-food-buckets-to-feed-the-hamsters
20882090
[2087]:./Medium/2087-Minimum%20Cost%20Homecoming%20of%20a%20Robot%20in%20a%20Grid/README.md#2087-minimum-cost-homecoming-of-a-robot-in-a-grid
@@ -3224,6 +3226,7 @@
32243226
[2073l]:https://leetcode.com/problems/time-needed-to-buy-tickets/
32253227
[2078l]:https://leetcode.com/problems/two-furthest-houses-with-different-colors/
32263228
[2079l]:https://leetcode.com/problems/watering-plants/
3229+
[2081l]:https://leetcode.com/problems/sum-of-k-mirror-numbers/
32273230
[2085l]:https://leetcode.com/problems/count-common-words-with-one-occurrence/
32283231
[2086l]:https://leetcode.com/problems/minimum-number-of-food-buckets-to-feed-the-hamsters/
32293232
[2087l]:https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,7 @@
947947
[2073][2073l]|[买票需要的时间][2073] |![py]
948948
[2078][2078l]|[两栋颜色不同且距离最远的房子][2078] |![rs]
949949
[2079][2079l]|[给植物浇水][2079] |![rs]
950+
[2081][2081l]|[k 镜像数字的和][2081] |![py]
950951
[2085][2085l]|[统计出现过一次的公共字符串][2085] |![py]
951952
[2086][2086l]|[从房屋收集雨水需要的最少水桶数][2086] |![rs]
952953
[2087][2087l]|[网格图中机器人回家的最小代价][2087] |![py]
@@ -2083,6 +2084,7 @@
20832084
[2073]:./Easy/2073-Time%20Needed%20to%20Buy%20Tickets/README_CN.md#2073-买票需要的时间
20842085
[2078]:./Easy/2078-Two%20Furthest%20Houses%20With%20Different%20Colors/README_CN.md#2078-两栋颜色不同且距离最远的房子
20852086
[2079]:./Medium/2079-Watering%20Plants/README_CN.md#2079-给植物浇水
2087+
[2081]:./Hard/2081-Sum%20of%20k-Mirror%20Numbers/README_CN.md#2081-k-镜像数字的和
20862088
[2085]:./Easy/2085-Count%20Common%20Words%20With%20One%20Occurrence/README_CN.md#2085-统计出现过一次的公共字符串
20872089
[2086]:./Medium/2086-Minimum%20Number%20of%20Food%20Buckets%20to%20Feed%20the%20Hamsters/README_CN.md#2086-从房屋收集雨水需要的最少水桶数
20882090
[2087]:./Medium/2087-Minimum%20Cost%20Homecoming%20of%20a%20Robot%20in%20a%20Grid/README_CN.md#2087-网格图中机器人回家的最小代价
@@ -3224,6 +3226,7 @@
32243226
[2073l]:https://leetcode.cn/problems/time-needed-to-buy-tickets/
32253227
[2078l]:https://leetcode.cn/problems/two-furthest-houses-with-different-colors/
32263228
[2079l]:https://leetcode.cn/problems/watering-plants/
3229+
[2081l]:https://leetcode.cn/problems/sum-of-k-mirror-numbers/
32273230
[2085l]:https://leetcode.cn/problems/count-common-words-with-one-occurrence/
32283231
[2086l]:https://leetcode.cn/problems/minimum-number-of-food-buckets-to-feed-the-hamsters/
32293232
[2087l]:https://leetcode.cn/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/

0 commit comments

Comments
 (0)