---
# System prepended metadata

title: 9. Palindrome Number
tags: [math, Leetcode, python, c++, easy]

---

###### tags: `Leetcode` `easy` `math` `python` `c++`

# 9. Palindrome Number

## [題目來源:]https://leetcode.com/problems/palindrome-number/

## 題目:
Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward.

For example, 121 is a palindrome while 123 is not.

## 解題想法:
反轉: 逐一取餘數再*10還原

# Python:
``` python=
class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x<0:
            return False
        y=x 
        res=0
        while y>0:
            tmp=y%10
            res=(res*10)+tmp
            y=y//10
        return x==res
        
if __name__ == '__main__':
    result = Solution()
    print(result.isPalindrome(121))
```

## C++:
``` cpp=
#include <iostream>
using namespace std;

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

int main()
{
    Solution res;
    int target = 121;
    cout << res.isPalindrome(target) << endl;
    return 0;
}
```
