Java_是否回文数字

Java_是否回文数字

题目:判断一个数字是否是否回文数字

示例 : 
输入:
    121
返回值:
    true

思路:字符串反转

 

代码:

public static void main(String[] args) {
    // 回文数字
    int num = 1233212;
}

// 我的(这是可以写成一句的)
public static boolean isPalindrome (int x) {
    // write code here
    String text = x + "";
    StringBuilder sb = new StringBuilder(text);
    return text.equals(sb.reverse().toString());
}

// 别人的
public static boolean isPalindrome (int x) {
    // write code here
    return String.valueOf(x).equals(new StringBuilder(String.valueOf(x)).reverse().toString());
}

// 效率高的
public static boolean isPalindrome (int x) {
    // write code here
    //利用指针进行查找
    String str= String.valueOf(x);
    int begin=0;
    int end=str.length()-1;
    while(begin<=end){
        //如果相等,遍历下一个
        if(str.charAt(begin)==str.charAt(end)){
            begin++;
            end--;
        }else{
            return false;
        }
    }
    return true;
}

 

原文地址:https://www.cnblogs.com/mmdz/p/15608274.html