/**
* @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 回文数字
最新推荐文章于 2024-07-13 13:55:40 发布