//原题链接https://leetcode.com/problems/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 to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
- 思路分析
1.空间复杂度为O(1)
2.负数都不是回文数,返回false
3.利用消消乐想法,末位与首位相同抵消,相异直接返回false - 源码附录
class Solution { public boolean isPalindrome(int x) { if(x<0){ return false; } else{ long temp = 1; while(temp <= x/10){ temp = temp *10 ; } while(x>0){ if(x%10 != x/temp){ return false; } x = (x % (int)temp)/10; temp = temp / 100; } return true; } } }