009 Palindrome Number 判断一个正整数是否是回文数

详见:https://leetcode.com/problems/palindrome-number/description/

实现语言:Java

方法一:

class Solution {
    public boolean isPalindrome(int x) {
        if(x<0){
            return false;
        }
        int reverse=0;
        int origin=x;
        while(x>0){
            reverse=reverse*10+x%10;
            x/=10;
        }
        return reverse==origin;
    }
}

方法二:

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

 实现语言:C++

方法一:

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0)
            return false;
        int reverse=0;
        int origin=x;
        while(x)
        {
            reverse=reverse*10+x%10;
            x/=10;
        }
        return reverse==origin;
    }
};

 方法二:

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0)
        {
            return false;
        }
        string s=to_string(x);
        int start=0;
        int end=s.size()-1;
        while(start<=end)
        {
            if(s[start]!=s[end])
            {
                return false;
            }
            ++start;
            --end;
        }
        return true;
    }
};
原文地址:https://www.cnblogs.com/xidian2014/p/8540239.html