LeetCode – 9. Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

这道题其实挺没意思的,不能用extra space。没想到怎么不用extra space,希望有大神指出

public class Solution {
    public boolean isPalindrome(int x) {
        if(x<0) return false;
        String s = Integer.toString(x);
        int j=s.length() -1;
        int i = 0;
        while(i<j){
            if(s.charAt(i) !=s.charAt(j)) return false;
            i++;
            j--;
        }
        return true;
    }
}

本站原创文章皆遵循“署名—非商业性使用—相同方式共享 4.0 协议 (CC BY-NC-SA 4.0)”。共享、演绎请保留以下标注:

原文作者:Jake Tao,来源:「LeetCode – 9. Palindrome Number」

147
0 0 147

延伸阅读

发表回复

登录后才能评论
分享本页
返回顶部