Determine whether an integer is a palindrome. An integerisapalindrome when itreads the same backward as forward. Example 1: Input: 121Output: true Example 2: Input: -121Output: falseExplanation: From left to right, it reads -121. From right
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.
思路:回文数,思路简单,负数直接否定,正数把反过来的数求出来和原来数比较即可(仔细想想发现这题好像做过。。。)
Solution:
1 class Solution { 2 public: 3 bool isPalindrome(int x) 4 { 5 bool bRe = false; 6 if(x>=0) 7 { 8 double count = 0; 9 int temp =x; 10 while(temp!=0) 11 { 12 int lstBit = temp%10; 13 count = count*10+lstBit; 14 temp/=10; 15 } 16 if(count == x) 17 { 18 bRe = true; 19 } 20 } 21 return bRe; 22 } 23 };
嗯,第一次提交发现居然会出现count溢出,额,题目都没给出输入范围,真是坑。