C语言
结合“7. 整数倒转”求出结果。
#include "math.h"bool ispalindrome(int x){ int max = pow(2, 31) - 1; int min = pow(2, 31) * -1; int y = 0; int n = x; if(x<0){ return false; } else{ while (n!=0){ if(y>max/10 || y<min/10) return false; y = y*10+n%10; n = n/10; } return x == y; }}Python 3
将x变为字符串逐字符进行首尾比较。
class Solution: def ispalindrome(self, x: int) -> bool: if x < 0: return False else: x = str(x) for i in range(round(len(x)/2)): if x[i]!=x[len(x)-i-1]: return False return True
利用python的语法,快速编写程序。
class Solution: def ispalindrome(self, x: int) -> bool: return x>=0 and str(x)[::-1]==str(x)
总结
以上是内存溢出为你收集整理的每日LeetCode - 9. 回文数(C语言和Python 3)全部内容,希望文章能够帮你解决每日LeetCode - 9. 回文数(C语言和Python 3)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)