LeetCode 564: Find the Closest Palindrome(python)
来源:互联网 发布:全知之眼音乐 编辑:程序博客网 时间:2024/06/06 10:46
原题:
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:
The input n is a positive integer represented by string, whose length will not exceed 18.
If there is a tie, return the smaller one as answer.
思路:
题目的大意是给出一个数字(字符串形式),输出距离该数字最近的回文数字(字符串形式)。比如,输入”123”,距离123最近的回文数字为121,则输出”121”。要距离近,那么数字的高位要尽量相同。仔细分析可知,距离一个数字最近的回文数字只有三种可能。以”123456789”为例:首先回文就是要左右对称,”123456789”的左边为”12345”,对应回文数为”123454321”(代码中为midnum)。当然”123454321”不一定是距离最近的,也有可能是”123444321”(代码中为minnum)或者”123464321”(代码中为maxnum),其左边分别为”12344”(”12345-1”)和”12346”(”12345+1”)。另外还要考虑数字左半边”+1”或者”-1”出现位数不同的情况。比如”9999”,左边为”99”,99+1=100,这时距离最近的数不可能把”100001”考虑进去,而应该是”10001”,所以数字左半边”+1”或者”-1”有位数改变的情况应该特殊考虑。算法时间复杂度为O(1)。
代码:(39ms通过,竟然击败了100%python代码!)
class Solution(object): # 判断+1或者-1是否出现位数的变化 def change_bit(self, n): if(len(str(n+1))!=len(str(n))): return 1 elif((len(str(n-1))!=len(str(n)))or(n==1)): return -1 else: return 0 # 找出minnum,midnum,maxnum中离n最近的数。如果距离相同,找到最小的那个数。 def find_min(self,minnum,midnum,maxnum,n): minabs=abs(long(minnum)-long(n)) midabs=abs(long(midnum)-long(n)) maxabs=abs(long(maxnum)-long(n)) if(midabs<=maxabs): if(minabs<=midabs): return minnum else: return midnum else: if(minabs<=maxabs): return minnum else: return maxnum def nearestPalindromic(self, n): # 只有一位数的情况 if(len(n)<2): return str(int(n)-1) # 位数为偶数的情况 if((len(n)%2)==0): substr_front=n[0:len(n)//2] midnum=substr_front+substr_front[::-1] maxsub_front=str(int(substr_front)+1) maxnum=maxsub_front+maxsub_front[::-1] minsub_front=str(int(substr_front)-1) minnum=minsub_front+minsub_front[::-1] # 位数为奇数的情况 else: substr_front=n[0:(len(n)+1)//2] midnum=substr_front+(substr_front[::-1])[1:len(substr_front)] maxsub_front=str(int(substr_front)+1) maxnum=maxsub_front+(maxsub_front[::-1])[1:len(maxsub_front)] minsub_front=str(int(substr_front)-1) minnum=minsub_front+minsub_front[::-1][1:len(minsub_front)] # 本身就是回文的情况,midabs=0,不符合要求,所以把midnum初始化为"0",达到midabs肯定大于max(maxabs,minabs)的目的 if(midnum==n): midnum="0" # 加1出现位数变化的情况 if(self.change_bit(int(substr_front))==1): maxnum="" maxnum='1'+((len(n)-1)*'0')+'1' # 减1出现位数变化的情况,注意要包括1-1=0的情况 if(self.change_bit(int(substr_front))==-1): minnum="" minnum=(len(n)-1)*'9' # print(minnum) # print(midnum) # print(maxnum) return self.find_min(minnum,midnum,maxnum,n)# IndentationError: unindent does not match any outer indentation level# 空格键和tab键混合使用导致的缩进问题# testing# s=Solution()# print(s.nearestPalindromic("1234"))
- LeetCode 564: Find the Closest Palindrome(python)
- leetcode 564. Find the Closest Palindrome(java)
- leetcode 564. Find the Closest Palindrome
- 564. Find the Closest Palindrome (Hard)
- [Leetcode] 564. Find the Closest Palindrome 解题报告
- 564. Find the Closest Palindrome
- 564. Find the Closest Palindrome
- [LeetCode] 564. Find the Closest Palindrome 深入浅出讲解和代码示例
- leetcode 564. Find the Closest Palindrome 距离自己最近的回文数字
- Find the Closest Palindrome:寻找与给定目标大小最接近且尽可能小的回文
- Leetcode--Find the Difference[python]
- LeetCode--Find the Duplicate Number(查找重复数字)Python
- leetcode 【287 Find the Duplicate Number】【Python】
- [leetcode: Python]389. Find the Difference
- NOJ [1251] Find the Palindrome
- Leetcode 9. Palindrome Number The Solution of Python
- leetcode 658. Find K Closest Elements
- Leetcode 658. Find K Closest Elements
- SHA1算法实现及详解
- js基本知识比较系列二:DOM操作
- 离殇
- 题目1454:Piggy-Bank
- EXCEL-VBA:弹出对话框,打开文件(指定扩展名)
- LeetCode 564: Find the Closest Palindrome(python)
- UIButton循环创建
- Plug-in 高级控件
- JavaScript---基本语法学习(一)JavaScript概述、Java和JavaScript的区别、和HTML结合方式、数据类型、运算符和表达式
- maven工程中sqlMap配置文件无法读取
- Android 6.0上sdcard和U盘路径获取和区分
- 题目1455:珍惜现在,感恩生活
- Linux 设备驱动篇之I2c设备驱动
- Aspose.Words组件介绍及使用—基本介绍与DOM概述