[LeetCode] 564. Find the Closest Palindrome 深入浅出讲解和代码示例

来源:互联网 发布:淘宝刷两单会坐牢吗 编辑:程序博客网 时间:2024/05/29 19:02

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:

  1. The input n is a positive integer represented by string, whose length will not exceed 18.
  2. 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 iif __name__ == "__main__":    st = Solution()    res = st.nearestPalindromic(15500987)    print "\nres-------: ",res


---------------------------------------------------------------------------------------------------

本文链接:http://blog.csdn.net/karen0310/article/details/xx

请尊重作者的劳动成果,转载请注明出处!

---------------------------------------------------------------------------------------------------


阅读全文
1 0
原创粉丝点击