[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 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.
Thinking: palindrome number, the idea is simple, negative numbers directly negate, positive numbers find the reverse number and compare it with the original number Yes (think about it carefully and find that this question seems to have been done...)
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 };

Well, the first submission found that there will be count overflow , Uh, the input range is not given in the title, it’s really a pit.

 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 };

Leave a Comment

Your email address will not be published.