LeetCode9 Palindrome Number

题意:

Determine whether an integer is a palindrome. Do this without extra space.  (Easy)

分析:

自己考虑的方法是利用Reverse Integer,然后查看rev与x是否相等,注意负数和0的判断(这里WA了一次)

代码1:

 1 class Solution {
 2 private:
 3     int reverse(int x) {
 4         long long result = 0;
 5         while (x != 0) {
 6             int temp = x % 10;
 7             x /= 10;
 8             result = result * 10 +  temp;
 9             if (result > 0x7FFFFFFF || result < -0x7FFFFFFF) {
10                 return 0;
11             }
12         }
13         return result;
14     }
15 public:
16     bool isPalindrome(int x) {
17         if (x < 0) {
18             return false;
19         }
20         int rex = reverse(x);
21         if (rex == 0 && x != 0) { //溢出(不是因为等于0而得到rex = 0)
22             return false;
23         }
24         if (rex == x) {
25             return true;
26         }
27         return false;
28     }
29 };

查看讨论区,有只比较一半的解法,循环中的思路和reverse integer类似,更为简洁,实现如下;

代码2:

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         if (x < 0 || (x % 10 == 0 && x != 0) ) {
 5             return false;
 6         }
 7         int sum = 0;
 8         while (x > sum) {
 9             int temp = x % 10;
10             sum = sum * 10 + temp;
11             x /= 10;
12         }
13         if (x == sum || x == sum / 10) {
14             return true;
15         }
16         return false;
17     }
18 };
时间: 2024-10-13 11:43:10

LeetCode9 Palindrome Number的相关文章

leetcode9 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 rig

LeetCode9——Palindrome Number

回文数字,简单处理.将数字各位取出,然后用临时变量累加,当累加完成后这个数字等于原来的数字即是回文数.需要注意的是负数不是回文数. class Solution { public: bool isPalindrome(int x) { if(x < 0) { return false; } if(x < 10 && x > 0) { return true; } int ans = 0; int tmp = x; while(tmp != 0) { ans = ans *

Palindrome Number (回文数)

回文数是指这样的数字:正读和倒读都是一样的.如:595,2332都是回文数,234不是回文数. 注意:负数不是回文数 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

LeetCode【9】. Palindrome Number --java的实现

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra spa

Leetcode 数 Palindrome Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Palindrome Number Total Accepted: 12165 Total Submissions: 41736 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integ

【LeetCode】Palindrome Number

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restri

65. Reverse Integer &amp;&amp; Palindrome Number

Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alr

[LeetCode] [Palindrome Number 2012-01-04]

Determine whether an integer is a palindrome. Do this without extra space. if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the r