LC9. 回文数

1. 转字符串

public class Solution9 {
    public boolean isPalindrome(int x) {
        String s = x + "";
        for (int i = 0; i < s.length()/2; i++) {
            if(s.charAt(i)!=s.charAt(s.length()-1-i))
                return false;
        }
        return true;
    }
}

2. 整数反转

public class Solution9 {
    public boolean isPalindrome(int x) {
        if(x<0)return false;
        int origin = x;
        int compare = 0;
        while (origin != 0) {
            compare*=10;
            compare+=origin%10;
            origin/=10;
        }
        return compare==x;
    }
}
发布了140 篇原创文章 · 获赞 2 · 访问量 1902

猜你喜欢

转载自blog.csdn.net/weixin_40602200/article/details/103946584
今日推荐