题目如下:

解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的。要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1,2和3对称把3变成2,得到1221,看起来好像没问题了。可是如果n=1283,按照这个方法得到的结果是1221,而预期的结果应该是1331,所以需要考虑到进位和退位的问题。这就有点复杂了,简单点的方法也有,就是把进位和退位的所有情况都列举出来再做比较,得出差值最小的那个。例如n=1283,按照对称法则得到的结果是1221,再把处于最中间的两个字符22分别进位和退位就可以得到1331和1111。接下来考虑到数量级的进位退位,对于一个四位数的n来说,与其最接近的三位数长度的回文数是999,最接近的五位数长度是10001。所以最后在这五个数字中得出符合题目要求的答案。

代码如下:

class Solution(object):
def nearestPalindromic(self, n):
"""
:type n: str
:rtype: str
"""
l = list(n)
low = 0
high = len(l) - 1
while low <= high:
if l[low] != l[high]:
l[high] = l[low]
low += 1
high -= 1 candidateList = ['' + ''*(len(n)-1) + '','' * (len(n) - 1),''.join(l)]
mid = len(l) / 2
if len(l) % 2 == 0:
if l[mid] == '':
candidateList.append(''.join(l[0:mid - 1] + [''] * 2 + l[mid + 1:]))
elif l[mid] == 9:
candidateList.append(''.join(l[0:mid - 1] + [''] * 2 + l[mid + 1:]))
else:
candidateList.append(''.join(l[0:mid - 1] + [str(int(l[mid]) - 1)] * 2 + l[mid + 1:]))
candidateList.append(''.join(l[0:mid - 1] + [str(int(l[mid]) + 1)] * 2 + l[mid + 1:]))
else:
if l[mid] == '':
candidateList.append(''.join(l[0:mid] + [''] * 1 + l[mid + 1:]))
elif l[mid] == 9:
candidateList.append(''.join(l[0:mid] + [''] * 1 + l[mid + 1:]))
else:
candidateList.append(''.join(l[0:mid] + [str(int(l[mid]) - 1)] * 1 + l[mid + 1:]))
candidateList.append(''.join(l[0:mid] + [str(int(l[mid]) + 1)] * 1 + l[mid + 1:])) res = ''
mDiff = 0
for i in candidateList:
if i == n or i == '':
continue
diff = abs(int(n) - int(i))
if mDiff == 0 or mDiff > diff:
res = i
mDiff = diff
elif mDiff == diff:
res = str(min(int(res), int(i))) return res

【leetcode】564. Find the Closest Palindrome的更多相关文章

  1. 【LeetCode】658. Find K Closest Elements 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/find-k-c ...

  2. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  3. 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】从contest-21开始。(一般是10个contest写一篇文章)

    [LeetCode Weekly Contest 29][2017/04/23] 第17周 Binary Tree Tilt (3) Array Partition I (6) Longest Lin ...

  5. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  6. 【LeetCode】4Sum 解题报告

    [题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. EhCache缓存框架的使用

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 我们使用EhCache缓存框架主要是为了判断重复Url,每次爬取一个网 ...

  2. python比C程序相比非常慢

    w http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001374738136930 ...

  3. eclipse下实现热部署,tomcat不重新reload context

    1. 打开server的编辑器 2. 在modules页签内,修改auto load属性为disabled

  4. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  5. (转载)STL map与Boost unordered_map的比较

    原链接:传送门 今天看到 boost::unordered_map,它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合 ...

  6. < python PIL - 批量图像处理 - 生成自定义大小图像 >

    < python PIL - 批量图像处理 - 生成自定义大小图像 > 直接用python自带的PIL图像库,对一个文件夹下所有jpg/png的图像进行自定义像素变换 from PIL i ...

  7. JavaScript Is or isNot

    读书笔记,简化代码--不对外公布,只是做笔记使用. var superman = { name: "Superman", strength: "Super", ...

  8. Spring cloud学习--Zuul01

    Zuul解决的问题 作为系统的统一入口,屏蔽了系统内部各个微服务的细节 可以与微服务治理框架结合,实现自动化的服务实例维护以及负载均衡的路由转发 实现接口权限校验与微服务业务逻辑的解耦 搭建Zuul服 ...

  9. 洛谷 P1731 [NOI1999]生日蛋糕(搜索剪枝)

    题目链接 https://www.luogu.org/problemnew/show/P1731 解题思路 既然看不出什么特殊的算法,显然是搜索... dfs(u,v,s,r0,h0)分别表示: u为 ...

  10. python——列表操作函数和方法

    1.添加新元素 1.1 append()函数 描述:append() 方法用于在列表末尾添加新的对象. 语法:list.append(obj) 参数:obj -- 添加到列表末尾的对象. 返回值:该方 ...