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;
    }
}

This site Original article All followed" Attribution—NonCommercial—ShareAlike 4.0 (CC BY-NC-SA 4.0) ”。 Please keep the following marks for sharing and interpretation:

Original author: Jake Tao Source: 「LeetCode – 9. Palindrome Number」

Praise 147
0 0 147

Further reading

Post a reply

Log in can only be commented on later
Share this page
Back to top