Given an integer x, return true if x is palindrome integer. An integer is a palindrome when it reads the same backward as forward. For example, 121 is a palindrome while 123 is not.
Example 1:
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -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: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
− 2 31 < = x < = 2 31 − 1 -2^{31} <= x <= 2^{31} - 1 −231<=x<=231−1
Follow up: Could you solve it without converting the integer to a string?
From:LeetCode
Link:https://leetcode.cn/problems/palindrome-number
Solution 1: Ideas:Use the method of reversing the numbers, and then judge whether the reversed numbers are equal to the original numbers. Code:
class Solution {
public:
bool isPalindrome(int x) {
double lowerBounds=pow(-2.0, 31.0);
double uppderBounds=pow(2.0, 31.0)-1;
if(x<0) {
return false;
}
if(xuppderBounds) {
return false;
}
int temp, num=x;
double total=0.0; //Beware of overflow when you reverse the integer.
while(num!=0) {
total*=10;
temp=num%10;
num=(num-temp)/10;
total+=temp;
}
if(x==total) {
return true;
}
else {
return false;
}
}
};
Full Version:
#include#include using namespace std; bool isPalindrome(int x); int reverseNum(int x); int main() { int number; cout<<"Enter the nunber: "; cin>>number; if(isPalindrome(number)==true) cout<<"True"< double lowerBounds=pow(-2.0, 31.0); double upperBounds=pow(2.0, 31.0)-1.0; if(x<0) return false; if(x upperBounds) return false; if(x==reverseNum(x)) return true; else return false; } int reverseNum(int x) { int temp, total=0; while(x!=0) { total*=10; temp=x%10; x=(x-temp)/10; total+=temp; } return total; }



