【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 ...
随机推荐
- 基于MyBatis实现Dao编程
基于MyBatis实现Dao编程 1.建立mybatis-config.xml文件 <?xml version="1.0" encoding="UTF-8" ...
- SQL SERVER 2012文件表(FILETABLE)新体验之一
SQLSERVER 2012 文件表功能很COOL,让我们体验一下吧. 1,创建数据库 [sql] DREATE DATABASE FileTableTest ON PRIMARY ( NA ...
- JDBC链接Mysql失败
错误信息:Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExc ...
- XML scriptlet 连接数据库
<%@ page language="java" contentType="text/html" pageEncoding="GBK" ...
- memcpy复制字符串的注意事项/memcpy不能用来拷贝类类型
strcpy复制src到dst,最后将dst的下一个位置置为'\0',所以dst是以'\0'结尾的字符串 ] = "abcde"; cout << c1 << ...
- (转载)如何在 Github 上发现优秀的开源项目?
转载自:传送门 之前发过一系列有关 GitHub 的文章,有同学问了,GitHub 我大概了解了,Git 也差不多会使用了,但是还是搞不清 GitHub 如何帮助我的工作,怎么提升我的工作效率? 问到 ...
- Pikachu漏洞练习平台实验——SQL注入(四)
1.概述 1.1发生原因 SQL注入漏洞,主要是开发人员在构建代码时,没有对输入边界进行安全考虑,导致攻击者可以通过合法的输入点提交一些精心构造的语句,从而欺骗后台数据库对其进行执行,导致数据库信息泄 ...
- Reading query string values in JavaScript
时间 2016-01-23 13:01:14 CrocoDillon’s Blog 原文 http://crocodillon.com/blog/reading-query-string-valu ...
- Macaca的Python的api整理
整理了下Macaca的API,做成思维脑图,方便阅览. WebDriver 安装 pip install wd git clone https://github.com/macacajs/wd.py. ...
- [HDU 1529]Cashier Employment(差分约束系统)
[HDU 1529]Cashier Employment(差分约束系统) 题面 有一个超市,在24小时对员工都有一定需求量,表示为\(r_i\),意思为在i这个时间至少要有i个员工,现在有n个员工来应 ...