Skip to content

Commit 383f9dc

Browse files
committed
feat: add solutions to lc problem: No.0564
No.0564.Find the Closest Palindrome
1 parent 8abb8c7 commit 383f9dc

File tree

6 files changed

+366
-11
lines changed

6 files changed

+366
-11
lines changed

solution/0500-0599/0564.Find the Closest Palindrome/README.md

+145-10
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,179 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给定一个整数 n ,你需要找到与它最近的回文数(不包括自身)。</p>
9+
<p>给定一个表示整数的字符串&nbsp;<code>n</code> ,返回与它最近的回文整数(不包括自身)。如果不止一个,返回较小的那个。</p>
1010

11-
<p>&ldquo;最近的&rdquo;定义为两个整数<strong>差的绝对值</strong>最小。</p>
11+
<p>“最近的”定义为两个整数<strong>差的绝对值</strong>最小。</p>
12+
13+
<p>&nbsp;</p>
1214

1315
<p><strong>示例 1:</strong></p>
1416

1517
<pre>
16-
<strong>输入:</strong> &quot;123&quot;
17-
<strong>输出:</strong> &quot;121&quot;
18+
<strong>输入:</strong> n = "123"
19+
<strong>输出:</strong> "121"
20+
</pre>
21+
22+
<p><strong>示例 2:</strong></p>
23+
24+
<pre>
25+
<strong>输入:</strong> n = "1"
26+
<strong>输出:</strong> "0"
27+
<strong>解释:</strong> 0 和 2是最近的回文,但我们返回最小的,也就是 0。
1828
</pre>
1929

20-
<p><strong>注意:</strong></p>
30+
<p>&nbsp;</p>
31+
32+
<p><strong>提示:</strong></p>
2133

22-
<ol>
23-
<li><strong>n </strong>是由字符串表示的正整数,其长度不超过18。</li>
24-
<li>如果有多个结果,返回最小的那个。</li>
25-
</ol>
34+
<ul>
35+
<li><code>1 &lt;= n.length &lt;= 18</code></li>
36+
<li><code>n</code>&nbsp;只由数字组成</li>
37+
<li><code>n</code>&nbsp;不含前导 0</li>
38+
<li><code>n</code>&nbsp;代表在&nbsp;<code>[1, 10<sup>18</sup>&nbsp;- 1]</code> 范围内的整数</li>
39+
</ul>
2640

2741
## 解法
2842

2943
<!-- 这里可写通用的实现逻辑 -->
3044

45+
- 用原数的前半部分替换后半部分得到的回文整数。
46+
- 用原数的前半部分加一后的结果替换后半部分得到的回文整数。
47+
- 用原数的前半部分减一后的结果替换后半部分得到的回文整数。
48+
- 为防止位数变化导致构造的回文整数错误,因此直接构造 999999…999 和 100…001 作为备选答案。
49+
50+
求以上数字中,最接近原数且不等于原数的最小数字。
51+
3152
<!-- tabs:start -->
3253

3354
### **Python3**
3455

3556
<!-- 这里可写当前语言的特殊实现逻辑 -->
3657

3758
```python
38-
59+
class Solution:
60+
def nearestPalindromic(self, n: str) -> str:
61+
x = int(n)
62+
l = len(n)
63+
res = {10 ** (l - 1) - 1, 10 ** l + 1}
64+
left = int(n[:(l + 1) >> 1])
65+
for i in range(left - 1, left + 2):
66+
j = i if l % 2 == 0 else i // 10
67+
while j:
68+
i = i * 10 + j % 10
69+
j //= 10
70+
res.add(i)
71+
res.discard(x)
72+
73+
ans = -1
74+
for t in res:
75+
if ans == -1 or abs(t - x) < abs(ans - x) or (abs(t - x) == abs(ans - x) and t < ans):
76+
ans = t
77+
return str(ans)
3978
```
4079

4180
### **Java**
4281

4382
<!-- 这里可写当前语言的特殊实现逻辑 -->
4483

4584
```java
85+
class Solution {
86+
public String nearestPalindromic(String n) {
87+
long x = Long.parseLong(n);
88+
long ans = -1;
89+
for (long t : get(n)) {
90+
if (ans == -1 || Math.abs(t - x) < Math.abs(ans - x) || (Math.abs(t - x) == Math.abs(ans - x) && t < ans)) {
91+
ans = t;
92+
}
93+
}
94+
return Long.toString(ans);
95+
}
96+
97+
private Set<Long> get(String n) {
98+
int l = n.length();
99+
Set<Long> res = new HashSet<>();
100+
res.add((long) Math.pow(10, l - 1) - 1);
101+
res.add((long) Math.pow(10, l) + 1);
102+
long left = Long.parseLong(n.substring(0, (l + 1) / 2));
103+
for (long i = left - 1; i <= left + 1; ++i) {
104+
StringBuilder sb = new StringBuilder();
105+
sb.append(i);
106+
sb.append(new StringBuilder(i + "").reverse().substring(l & 1));
107+
res.add(Long.parseLong(sb.toString()));
108+
}
109+
res.remove(Long.parseLong(n));
110+
return res;
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
string nearestPalindromic(string n) {
121+
long x = stol(n);
122+
long ans = -1;
123+
for (long t : get(n))
124+
if (ans == -1 || abs(t - x) < abs(ans - x) || (abs(t - x) == abs(ans - x) && t < ans))
125+
ans = t;
126+
return to_string(ans);
127+
}
128+
129+
unordered_set<long> get(string& n) {
130+
int l = n.size();
131+
unordered_set<long> res;
132+
res.insert((long)pow(10, l - 1) - 1);
133+
res.insert((long)pow(10, l) + 1);
134+
long left = stol(n.substr(0, (l + 1) / 2));
135+
for (long i = left - 1; i <= left + 1; ++i)
136+
{
137+
string prefix = to_string(i);
138+
string t = prefix + string(prefix.rbegin() + (l & 1), prefix.rend());
139+
res.insert(stol(t));
140+
}
141+
res.erase(stol(n));
142+
return res;
143+
}
144+
};
145+
```
46146

147+
### **Go**
148+
149+
```go
150+
func nearestPalindromic(n string) string {
151+
l := len(n)
152+
res := []int{int(math.Pow10(l-1)) - 1, int(math.Pow10(l)) + 1}
153+
left, _ := strconv.Atoi(n[:(l+1)/2])
154+
for _, x := range []int{left - 1, left, left + 1} {
155+
y := x
156+
if l&1 == 1 {
157+
y /= 10
158+
}
159+
for ; y > 0; y /= 10 {
160+
x = x*10 + y%10
161+
}
162+
res = append(res, x)
163+
}
164+
ans := -1
165+
x, _ := strconv.Atoi(n)
166+
for _, t := range res {
167+
if t != x {
168+
if ans == -1 || abs(t-x) < abs(ans-x) || abs(t-x) == abs(ans-x) && t < ans {
169+
ans = t
170+
}
171+
}
172+
}
173+
return strconv.Itoa(ans)
174+
}
175+
176+
func abs(x int) int {
177+
if x < 0 {
178+
return -x
179+
}
180+
return x
181+
}
47182
```
48183

49184
### **...**

solution/0500-0599/0564.Find the Closest Palindrome/README_EN.md

+115-1
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,127 @@
4141
### **Python3**
4242

4343
```python
44-
44+
class Solution:
45+
def nearestPalindromic(self, n: str) -> str:
46+
x = int(n)
47+
l = len(n)
48+
res = {10 ** (l - 1) - 1, 10 ** l + 1}
49+
left = int(n[:(l + 1) >> 1])
50+
for i in range(left - 1, left + 2):
51+
j = i if l % 2 == 0 else i // 10
52+
while j:
53+
i = i * 10 + j % 10
54+
j //= 10
55+
res.add(i)
56+
res.discard(x)
57+
58+
ans = -1
59+
for t in res:
60+
if ans == -1 or abs(t - x) < abs(ans - x) or (abs(t - x) == abs(ans - x) and t < ans):
61+
ans = t
62+
return str(ans)
4563
```
4664

4765
### **Java**
4866

4967
```java
68+
class Solution {
69+
public String nearestPalindromic(String n) {
70+
long x = Long.parseLong(n);
71+
long ans = -1;
72+
for (long t : get(n)) {
73+
if (ans == -1 || Math.abs(t - x) < Math.abs(ans - x) || (Math.abs(t - x) == Math.abs(ans - x) && t < ans)) {
74+
ans = t;
75+
}
76+
}
77+
return Long.toString(ans);
78+
}
79+
80+
private Set<Long> get(String n) {
81+
int l = n.length();
82+
Set<Long> res = new HashSet<>();
83+
res.add((long) Math.pow(10, l - 1) - 1);
84+
res.add((long) Math.pow(10, l) + 1);
85+
long left = Long.parseLong(n.substring(0, (l + 1) / 2));
86+
for (long i = left - 1; i <= left + 1; ++i) {
87+
StringBuilder sb = new StringBuilder();
88+
sb.append(i);
89+
sb.append(new StringBuilder(i + "").reverse().substring(l & 1));
90+
res.add(Long.parseLong(sb.toString()));
91+
}
92+
res.remove(Long.parseLong(n));
93+
return res;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
string nearestPalindromic(string n) {
104+
long x = stol(n);
105+
long ans = -1;
106+
for (long t : get(n))
107+
if (ans == -1 || abs(t - x) < abs(ans - x) || (abs(t - x) == abs(ans - x) && t < ans))
108+
ans = t;
109+
return to_string(ans);
110+
}
111+
112+
unordered_set<long> get(string& n) {
113+
int l = n.size();
114+
unordered_set<long> res;
115+
res.insert((long)pow(10, l - 1) - 1);
116+
res.insert((long)pow(10, l) + 1);
117+
long left = stol(n.substr(0, (l + 1) / 2));
118+
for (long i = left - 1; i <= left + 1; ++i)
119+
{
120+
string prefix = to_string(i);
121+
string t = prefix + string(prefix.rbegin() + (l & 1), prefix.rend());
122+
res.insert(stol(t));
123+
}
124+
res.erase(stol(n));
125+
return res;
126+
}
127+
};
128+
```
50129

130+
### **Go**
131+
132+
```go
133+
func nearestPalindromic(n string) string {
134+
l := len(n)
135+
res := []int{int(math.Pow10(l-1)) - 1, int(math.Pow10(l)) + 1}
136+
left, _ := strconv.Atoi(n[:(l+1)/2])
137+
for _, x := range []int{left - 1, left, left + 1} {
138+
y := x
139+
if l&1 == 1 {
140+
y /= 10
141+
}
142+
for ; y > 0; y /= 10 {
143+
x = x*10 + y%10
144+
}
145+
res = append(res, x)
146+
}
147+
ans := -1
148+
x, _ := strconv.Atoi(n)
149+
for _, t := range res {
150+
if t != x {
151+
if ans == -1 || abs(t-x) < abs(ans-x) || abs(t-x) == abs(ans-x) && t < ans {
152+
ans = t
153+
}
154+
}
155+
}
156+
return strconv.Itoa(ans)
157+
}
158+
159+
func abs(x int) int {
160+
if x < 0 {
161+
return -x
162+
}
163+
return x
164+
}
51165
```
52166

53167
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
string nearestPalindromic(string n) {
4+
long x = stol(n);
5+
long ans = -1;
6+
for (long t : get(n))
7+
if (ans == -1 || abs(t - x) < abs(ans - x) || (abs(t - x) == abs(ans - x) && t < ans))
8+
ans = t;
9+
return to_string(ans);
10+
}
11+
12+
unordered_set<long> get(string& n) {
13+
int l = n.size();
14+
unordered_set<long> res;
15+
res.insert((long)pow(10, l - 1) - 1);
16+
res.insert((long)pow(10, l) + 1);
17+
long left = stol(n.substr(0, (l + 1) / 2));
18+
for (long i = left - 1; i <= left + 1; ++i)
19+
{
20+
string prefix = to_string(i);
21+
string t = prefix + string(prefix.rbegin() + (l & 1), prefix.rend());
22+
res.insert(stol(t));
23+
}
24+
res.erase(stol(n));
25+
return res;
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func nearestPalindromic(n string) string {
2+
l := len(n)
3+
res := []int{int(math.Pow10(l-1)) - 1, int(math.Pow10(l)) + 1}
4+
left, _ := strconv.Atoi(n[:(l+1)/2])
5+
for _, x := range []int{left - 1, left, left + 1} {
6+
y := x
7+
if l&1 == 1 {
8+
y /= 10
9+
}
10+
for ; y > 0; y /= 10 {
11+
x = x*10 + y%10
12+
}
13+
res = append(res, x)
14+
}
15+
ans := -1
16+
x, _ := strconv.Atoi(n)
17+
for _, t := range res {
18+
if t != x {
19+
if ans == -1 || abs(t-x) < abs(ans-x) || abs(t-x) == abs(ans-x) && t < ans {
20+
ans = t
21+
}
22+
}
23+
}
24+
return strconv.Itoa(ans)
25+
}
26+
27+
func abs(x int) int {
28+
if x < 0 {
29+
return -x
30+
}
31+
return x
32+
}

0 commit comments

Comments
 (0)