问题

palindrome-number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Follow up: Could you solve it without converting the integer to a string?

Example 1:

Input: x = 121
Output: true

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.

Example 4:

Input: x = -101
Output: false

Constraints:

• − 2 31 < = x < = 2 31 − 1 -2^{31} <= x <= 2^{31} - 1

思考

方法1

class Solution {
public boolean isPalindrome(int x) {
// 小于0 返回false
if(x < 0)
return false;

// 计算 x 按位倒装的数 r
int r = 0,temp = x;
while(temp != 0){
r = r*10 + temp%10;
temp /= 10;
}
return r == x;
}
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

好酒不贪杯

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
07-21 356

02-22 51
12-18 35万+
02-02 132
09-01 128
05-04 659
03-01 230