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);
}
    原文作者:Gandalf0
    原文地址: https://www.jianshu.com/p/263fcf866c31
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞