1、汇总概要
xx
2、题目
Given an integer n, find the closest integer (not including itself), which is a palindrome.
The 'closest' is defined as absolute difference minimized between two integers.
Example 1:
Input: "123"
Output: "121"
Note:
- The input n is a positive integer represented by string, whose length will not exceed 18.
- If there is a tie, return the smaller one as answer.
3、审题
给定一个整数,求出它最近的回文串
4、解题思路
5、代码示例 - Python
class Solution(object):
def nearestPalindromic(self, n):
for i in range(n,1,-1):
istr = str(i)
leng = len(istr)
flag = 1
for j in range(0,leng/2):
if istr[j] != istr[leng-1-j]:
flag = 0
if flag == 1: #is the palindromic
return i
if __name__ == "__main__":
st = Solution()
res = st.nearestPalindromic(15500987)
print "\nres-------: ",res
---------------------------------------------------------------------------------------------------
本文链接:http://blog.youkuaiyun.com/karen0310/article/details/xx
请尊重作者的劳动成果,转载请注明出处!
---------------------------------------------------------------------------------------------------