@@ -30,13 +30,14 @@ If there is a tie, return the smaller one as answer.
30
30
31
31
前前后后搞了2天,感谢今天下午女朋友和我一起讨论,最终搞出来了。
32
32
33
- 总共三种情况,算出后面的,前面的,还有当前的前半部分直接反转黏贴到后半部分。
33
+ - 总共三种情况,算出后面的,前面的,还有当前的前半部分直接反转黏贴到后半部分。总结一下就是说[ 前半部分+1,前半部分-1,前半部分自身] + 前面得出结果的反转就是我们可能的结果之一。
34
+ - 另外两种情况就是进位和减位,格式为1000..0001, 999...999
34
35
35
- 三个部分看看哪个更近 ,唯一需要注意的是输入为10和11的时候handle不了,要在最前面手动处理一下。
36
+ 5个部分看看哪个更近 ,唯一需要注意的是输入为10和11的时候handle不了,要在最前面手动处理一下。
36
37
37
38
beats 100%,功夫不负有心人!
38
39
39
- ``` python
40
+ ```
40
41
class Solution(object):
41
42
def nearestPalindromic(self, n):
42
43
"""
@@ -47,26 +48,71 @@ class Solution(object):
47
48
if 9 < int(n) < 12:
48
49
return '9'
49
50
r_half_len = len(tmp) // 2
50
- if len (tmp) & 1 == 0 :
51
+ if len(tmp) & 1 == 0: # 长度为偶数
51
52
num_digits = len(str(int(tmp[:len(tmp)/2])))
52
53
half = tmp[:len(tmp)/2]
53
- else :
54
+ else: # 长度为奇数
54
55
num_digits = len(str(int(tmp[:(len(tmp)+1)/2])))
55
56
half = tmp[:(len(tmp)+1)/2]
56
57
57
- if len (str (int (half)+ 1 )) > num_digits:
58
+ if len(str(int(half)+1)) > num_digits: # 进位了
58
59
behind = '1' + '0' * (len(tmp)-1) + '1'
59
60
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: # 减位了
62
63
before = '9' * (len(tmp)-1)
63
64
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]
67
68
if cur == tmp[::-1]:
68
69
return behind if abs(int(tmp)-int(behind)) < abs(int(tmp)-int(before)) else before
69
70
abss = map(lambda x: abs(int(x)-int(tmp)), [before, cur, behind])
70
71
selects = [before, cur, behind]
71
72
return selects[abss.index(min(abss))]
72
73
```
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