1classSolution(object):2defisPalindrome(self, x):3"""4:type x: int5:rtype: bool6"""7x2 = str(x)8ifx2 == x2[::-1]:9returnTrue10else:11returnFalse 一个比较精简的代码 运行时间打败了97%的代码 但是很占内存
解法1. 简单解法:将整数转换为字符串 转换之后,Python有转换的 reverse 函数,将字符串进行反转:str[::-1]。 代码如下: ## LeetCode 9, 回文数,简单写法1:classSolution:defisPalindrome(self,x:int)->bool:y=str(x)## 转换为字符串z=y[::-1]## 对字符串进行反转returny==z 解法2. 简单写法的精简...
publicintlongestPalindrome(String s){int[] lowercase =newint[26];int[] uppercase =newint[26];intres=0;for(inti=0; i < s.length(); i++){chartemp=s.charAt(i);if(temp >=97) lowercase[temp-'a']++;elseuppercase[temp-'A']++; }for(inti=0; i <26; i++){ res+=(lowercase[i...
另外,以0结尾的非零数都不是回文。 class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool """ if x < 0 or (x != 0 and x%10 == 0): return False y = 0 while x > y: y = y*10 + x%10 x = x/10 return x == y or y/10 == x 1. 2...
classSolution(object):deflongestPalindrome(self,s):""":type s: str:rtype: str"""palindrome=""foriinrange(len(s)):len1=len(self.getPalindrome(s,i,i))iflen1>len(palindrome):palindrome=self.getPalindrome(s,i,i)len2=len(self.getPalindrome(s,i,i+1))iflen2>len(palindrome):palindrome...
题目 思路 给出一串罗马数字,需要转换成阿拉伯数字,其中4,9,40,90,400,900的表达方式会特殊。解决办法:只需要将罗马字符放入一个列表,从左到右(罗马数字左大右小...
classSolution(object):defisPalindrome(self,x):""":type x:int:rtype:bool"""ifx<0or(x!=0and x%10==0):returnFalse y=0whilex>y:y=y*10+x%10x=x/10returnx==y or y/10==x 我提交的 代码语言:javascript 复制 classSolution(object):defisPalindrome(self,x):""":type x:int:rtype:boo...
e.g. 123 --> 321 ans = ans*10 + x%10 class Solution{public:boolisPalindrome(intx){if(x<0)return0;intorigin=x;intans=0;while(x){ans=ans*10+x%10;x=x/10;}returnorigin==ans;}}; image.png 当然 可以用python赖皮 ...(逃
Python Code:class Solution: def isPalindrome(self, s: str) -> bool: left, right = 0, len(s) - 1 while left < right: if not s[left].isalnum(): left += 1 continue if not s[right].isalnum(): right -= 1 continue if s[left].lower() == s[right...
class Solution: def longestPalindrome(self, s: str) -> str: if s is None: return s if len(s) == 0 or len(s) > 1000: return '' n = len(s) # 创建dp table dp = [[False] * n]*n ans = "" # 遍历dp table, l表示回文字符串长度 for l in range(n): # 枚举子串的起始位...