题目如下:

解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的。要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设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. Hadoop ”No room for reduce task“问题处理

    早上发现一个任务有20个reduce,但是只有四个正常完成,剩余16个等待了8个小时才分配执行(集群槽位资源充足) 解决方法:查看了集群的log,发现有这种warn: -- ::, WARN org. ...

  2. java 将MySql数据库中的数据(某一列)取出放入数组中 转

    转:http://blog.csdn.net/ewili/article/details/8605638 假设mysql中test数据库中有个表式score,有数据,我只取第一列的数据出来: publ ...

  3. spring-cloud eureka注册发现

    idea新建一个eureka server服务 application.yml 配置: spring: application: name: eureka-server server: port: 7 ...

  4. H3C CAS 介绍 & 基本概念

    目录 目录 基本概念 H3C CAS 中的虚拟机 虚拟机中的虚拟设备 虚拟 CPU 的 3 种工作模式 虚拟网卡的 3 种类型 虚拟磁盘的三种类型 虚拟机辅助工具 CAS Tools 虚拟机外的虚拟设 ...

  5. 用notepad++ 打造轻量级Java编译器

    http://blog.163.com/jackie_howe/blog/static/19949134720125591752396/ 用notepad++ 打造轻量级Java编译器 2012-06 ...

  6. AtCoder ABC 140D Face Produces Unhappiness

    题目链接:https://atcoder.jp/contests/abc140/tasks/abc140_d 题目大意 有一对 N 个人, 用字符串 S 表示, S[i] 如果等于 'L' 说明这个人 ...

  7. Git008--远程仓库

    Git--远程仓库 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ...

  8. python 丰田经销商

    import requests import json from dbutil.pgsql import PgsqlPipeline from datetime import date headers ...

  9. Xshell实现Windows和使用跳板机跳转的远程Linux互传文件

    适用于Linux CentOS版本,本地电脑是Win10版本 查询Linux版本: $lsb_release -a $cat /etc/issue 1.使用Xshell 连接上服务器 2.安装文件传输 ...

  10. MVC与设计模式的关系及MVC的实现原理和设计原理

    1 MVC介绍 众所周知MVC不是设计模式,是一个比设计模式更大一点的模式,称作设计模式不合理,应该说MVC它是一种软件开发架构模式,它包含了很多的设计模式,最为密切是以下三种:Observer (观 ...