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

Commit 30810c0

Browse files
Update 564._Find_the_Closest_Palindrome.md
1 parent 86b05c2 commit 30810c0

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed

‎docs/Leetcode_Solutions/564._Find_the_Closest_Palindrome.md‎

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ If there is a tie, return the smaller one as answer.
3030

3131
前前后后搞了2天,感谢今天下午女朋友和我一起讨论,最终搞出来了。
3232

33-
总共三种情况,算出后面的,前面的,还有当前的前半部分直接反转黏贴到后半部分。
33+
- 总共三种情况,算出后面的,前面的,还有当前的前半部分直接反转黏贴到后半部分。总结一下就是说[前半部分+1,前半部分-1,前半部分自身] + 前面得出结果的反转就是我们可能的结果之一。
34+
- 另外两种情况就是进位和减位,格式为1000..0001, 999...999
3435

35-
三个部分看看哪个更近,唯一需要注意的是输入为10和11的时候handle不了,要在最前面手动处理一下。
36+
5个部分看看哪个更近,唯一需要注意的是输入为10和11的时候handle不了,要在最前面手动处理一下。
3637

3738
beats 100%,功夫不负有心人!
3839

39-
```python
40+
```
4041
class Solution(object):
4142
def nearestPalindromic(self, n):
4243
"""
@@ -47,26 +48,71 @@ class Solution(object):
4748
if 9 < int(n) < 12:
4849
return '9'
4950
r_half_len = len(tmp) // 2
50-
if len(tmp) & 1 == 0:
51+
if len(tmp) & 1 == 0: # 长度为偶数
5152
num_digits = len(str(int(tmp[:len(tmp)/2])))
5253
half = tmp[:len(tmp)/2]
53-
else:
54+
else: # 长度为奇数
5455
num_digits = len(str(int(tmp[:(len(tmp)+1)/2])))
5556
half = tmp[:(len(tmp)+1)/2]
5657
57-
if len(str(int(half)+1)) > num_digits:
58+
if len(str(int(half)+1)) > num_digits: # 进位了
5859
behind = '1' + '0' * (len(tmp)-1) + '1'
5960
else:
60-
behind = str(int(half) + 1)[:r_half_len] +str(int(half) +1)[r_half_len:]+ str(int(half) + 1)[:r_half_len][::-1]
61-
if len(str(int(half)-1)) < num_digits:
61+
behind = str(int(half) + 1)+ str(int(half) + 1)[:r_half_len][::-1]
62+
if len(str(int(half)-1)) < num_digits: # 减位了
6263
before = '9' * (len(tmp)-1)
6364
else:
64-
before = str(int(half) - 1)[:r_half_len] +str(int(half) -1)[r_half_len:]+ str(int(half) - 1)[:r_half_len][::-1]
65-
66-
cur = str(int(half))[:r_half_len] + str(int(half))[r_half_len:]+str(int(half))[:r_half_len][::-1]
65+
before = str(int(half) - 1)+ str(int(half) - 1)[:r_half_len][::-1]
66+
# 当前的前半部分直接反转,如1002,变成了1001
67+
cur = str(int(half))+ str(int(half))[:r_half_len][::-1]
6768
if cur == tmp[::-1]:
6869
return behind if abs(int(tmp)-int(behind)) < abs(int(tmp)-int(before)) else before
6970
abss = map(lambda x: abs(int(x)-int(tmp)), [before, cur, behind])
7071
selects = [before, cur, behind]
7172
return selects[abss.index(min(abss))]
7273
```
74+
75+
后面我觉得完全可以重构一下代码,behind和before不用非得算出来,我只要把所有的可能性全都放到一个list里面去,最后来判断就行了
76+
77+
```python
78+
class Solution(object):
79+
def nearestPalindromic(self, n):
80+
"""
81+
:type n: str
82+
:rtype: str
83+
"""
84+
prefix = int(n[:(len(n)+1)//2])
85+
86+
candidates = set(['1' + '0' * (len(n)-1) + '1', '9' * (len(n)-1)]) # 进位减位可能性
87+
88+
for i in map(str, [prefix-1, prefix, prefix+1]): # 前半部分+1,-1,+0可能性
89+
candidates.add(i + [i, i[:-1]][len(n) & 1][::-1])
90+
91+
candidates.discard(n) # 除去自身可能就是Palindrome的可能性
92+
candidates.discard('') # 输入n为个位数的话,我们还会加入空字符串,必须要去掉
93+
94+
return min(candidates, key = lambda x: (abs(int(x) - int(n)), int(x)))
95+
```
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
118+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /