【leetcode】564. Find the Closest Palindrome
题目如下:

解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的。要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设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的更多相关文章
- 【LeetCode】658. Find K Closest Elements 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/find-k-c ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】从contest-21开始。(一般是10个contest写一篇文章)
[LeetCode Weekly Contest 29][2017/04/23] 第17周 Binary Tree Tilt (3) Array Partition I (6) Longest Lin ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【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 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- XX-Net 使用教程(Across the Great Wall)
注意: 由于封锁严重,软件自带IP已经被封杀殆尽.因此需要数分钟到数小时的初始化IP扫描,方能正常运行. 虽然系统内置了公共appid, 还是建议部署自己的appid,公共appid限制看视频.需要注 ...
- Note:目录1
ylbtech-Note:目录1 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech ...
- leetcode 235. 二叉搜索树的最近公共祖先(c++)
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x ...
- 使用 Select2 下拉框实现复选
使用 Select2 下拉框实现复选 <!DOCTYPE html> <html> <head> <meta http-equiv="Content ...
- (appium+python)UI自动化_07_app UI自动化实例【叮咚搜索加车为例】
前言 初学UI自动化的小伙伴,在配置好appium+python自动化环境后,往往不知道如何下手实现自动化.小编在初期学习的时候也有这种疑惑,在此以叮咚买菜app-搜索加车为实例,展示下appium是 ...
- 阿里云ECS服务器centos6.x安装docker问题盘点
1.首先在centos6.x和centos7.x中yum安装docker的区分. centos6.x: yum install docker-io centos7.x: yum install doc ...
- Java-多线程第一篇多线程相关认识(1)
1.单线程进程 如果程序执行某行代码时遇到了阻塞,则程序将会停滞在该处. 2.进程代表着一个程序,程序是静态的,进程是动态的程序. 进程是系统进行资源分配和调度的一个独立单位.关于进程有如下3个特征: ...
- mybatis问题整理
// List<String> findBuildByProject(String prjName); //单参数时使用<if></if>标签判断采用"_ ...
- SpringBoot实现上传下载(二)
这篇写下载. **1.实现思路** 上一篇的数据库设计中,我们有一个字段始终没有用到fileName,这是用来给Layer对象存储文件名的,以此来完成文件与对象的对应, 
题面 传送门 分析 没想到压轴题是道模板裸题 由于子图的权值=边权和-点权和 将边和点都看成新图中的点 S向原来的边i连边,权值为边权 点i向T连边,权值为点权 边i:(u,v,w)向u,v,连边,权 ...