Palindrome Number

Problem: Palindrome Number

My solution simply converts the number into string and tell the answer. The actual math solution is here.

Code in Python:

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        return str(x) == str(x)[::-1]

Code in Java:

public class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0)
            return false;

        int y = 0, temp = x;
        while (temp != 0) {
            y = y * 10 + temp % 10;
            temp /= 10;
        }
        return x == y;
    }
}

results matching ""

    No results matching ""