学习时间:
2023年1月27日
题目描述:

题解分享:
/**
* @ Author 繁华倾夏
* @ Date 2023年01月27日
*/
// 力扣(LeetCode):9. 回文数
public class Solution {
public static boolean isPalindrome(int x) { // 调用函数
if (x < 0 || (x % 10 == 0 && x != 0)) { // 当x<0时不是回文数
return false; // 最后一个数字为0时也不是回文数,但是数值0位回文数
}
int re = 0;
while (x > re) { // 遍历x,使之反转
re = re * 10 + x % 10;
x /= 10;
}
return x == re || x == re / 10; // 奇偶数时需要判断
}
// 测试用例
// 输入 x = 121
// 输出 true
public static void main(String[] args) {
int x=121;
boolean re=isPalindrome(x);
System.out.println(re);
}
}
【繁华倾夏】【每日力扣题解分享】【Day13】