classSolution{publicbooleanisPalindrome(intx){StringreversedStr=(newStringBuilder(x +"")).reverse().toString();return(x +"").equals(reversedStr); } } Python 实现 classSolution:defisPalindrome(self, x):""" :type x: int :rtype: bool """returnstr(x) ==str(x)[::-1] 复杂度分析 时间...
1classSolution(object):2defisPalindrome(self, x):3"""4:type x: int5:rtype: bool6"""7x2 = str(x)8ifx2 == x2[::-1]:9returnTrue10else:11returnFalse 一个比较精简的代码 运行时间打败了97%的代码 但是很占内存
## LeetCode 9, 回文数,简单写法1:classSolution:defisPalindrome(self,x:int)->bool:y=str(x)## 转换为字符串z=y[::-1]## 对字符串进行反转returny==z 解法2. 简单写法的精简版 转换和反转的操作,都可以放入return语句。 ## LeetCode 9, 回文数,简单写法1的精简版classSolution:defisPalindrome(sel...
先将x转为相反的数,存在long long里面,然后比较这个long long 会不会和x相等。其实long也可以,因为int的最大值反过来最多也就long。如果是负数,则直接判断false。如果十以内的就直接返回true。 classSolution {private://leetcode9longlongreverseInt(intx) {longlongresult =0;while(x) { result= result *10...
【LeetCode】9、Palindrome Number(回文数) 题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input:121Output:true Example 2: Input:-121Output:falseExplanation:Fromlefttoright, it reads -121....
LeetCode题解(9)--Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. 思想: 先计算出这个整数的逆序数,然后比较它和原来的数每位是否都相同即可。另外要注意负数没有回文数,还应该考虑overflow一定不是回文数。
【LeetCode】Palindrome Number(回文数) 这道题是LeetCode里的第9道题。 题目说的: 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 示例1: 输入: 121 输出: true 示例2: 输入: -121 输出: false 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- ...
[leetcode]9. Palindrome Number 回文数 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right...
Leetcode #9 Easy <Palindrome Number> 题目如图,下面是我的解决方法: 1classSolution {2publicbooleanisPalindrome(intx) {3if(x < 0) //由题意可知,小于0的数不可能为回数, 因此直接返回false 4returnfalse;5else{67String len = x + "";8int[] arr =newint[len.length()]; //创建数组arr, ...
Leetcode 3——Palindrome Number(回文数) Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过,不过从基础开始尝试吧。 Solution: publicclassSolution {publicbooleanisPalindrome(intx) {intn=1;intcopyx=x;if(x<0)returnfalse;if(x<10)...