LeetCode刷题之Palindrome Number

Problem

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

My Solution

public class Solution {
    int[] numbers = new int[20];

    public boolean isPalindrome(int x) {
        if (x >= 0) {
            if (reverse(x) == x) {
                return true;
            }
        }
        return false;
    }

    public int reverse(int x) {
        int i = 0, rX = 0, count = 0;
        for (i = 0; x != 0; i++) {
            numbers[i] = x % 10;
            x /= 10;
        }
        count = i;
        for (i = 0; i < count; i++) {
            rX = rX * 10 + numbers[i];
        }
        return rX;
    }
}
Great Solution

public boolean isPalindrome(int x) {
    if(x<0 || (x!=0 && x%10==0))
        return false;
    int res = 0;
    while(x>res){
        res = res*10 + x%10;
        x = x/10;
     }
    return (x==res || x==res/10);
}

你可能感兴趣的:(LeetCode刷题之Palindrome Number)