Table of contents
Palindrome
Given an integer x, return true if x is a palindromic integer; otherwise, return false.
A palindromic number is an integer that reads the same in forward order (from left to right) and in reverse order (from right to left).
For example, 121 is a palindrome, but 123 is not.
Example 1:
Input: x = 121
Output: true
Example 2:Input: x = -121
Output: false
Explanation: Read from left to right, it is -121. Read from right to left, it is 121-. Therefore it is not a palindromic number.
Example 3:Input: x = 10
Output: false
Explanation: Read from right to left, it is 01. Therefore it is not a palindromic number.
hint:
-231 <= x <= 231 - 1
answer:
- Negative numbers must not be palindrome numbers, return false
- A positive number calculates its reversed value, and compares whether it is equal to the original value
- If it is a palindrome, it is equal to return true, if it is not, it is not equal to false
code:
class Solution { public boolean isPalindrome(int x) { //排除负数 if(x < 0) return false; int res = 0;//记录倒叙结果 int num = x;//等于所给整数 //赋值 while(num != 0) { res = res * 10 + num % 10; num /= 10; } //判断 if(res == x){ return true; }else{ return false; } } }