/**
* @author xienl
* @description 回文数字
* @date 2022/7/6
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
}
/**
* 一个最简单的办法,反转然后比较是否相等
* @param x
* @return
*/
public boolean isPalindrome2 (int x) {
// write code here
String str1 = Integer.toString(x);
String str2 = new StringBuffer(str1).reverse().toString();
return str1.equals(str2);
}
public boolean isPalindrome (int x) {
if (x > 0){
return false;
}
String str1 = Integer.toString(x);
int left = 0 , right = str1.length() - 1;
while (left < right){
if (str1.charAt(left) != str1.charAt(right) ){
return false;
}
left++;
right--;
}
return true;
}
}
牛客网:NC56 回文数字
最新推荐文章于 2025-11-26 15:59:01 发布
博客涉及 Java 开发语言与算法相关内容,虽未给出具体内容,但可知围绕 Java 运用算法进行开发,在信息技术领域,Java 是常用开发语言,算法是核心要素。

600

被折叠的 条评论
为什么被折叠?



