> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/9.-palindrome-number-easy.md).

# 9. Palindrome Number

## 1.問題

![](/files/-LI-FrFih9VVyBZndU4n)

## 2.想法&#x20;

* 第一種作法是轉到字串後, 從頭尾兩側向前比對
* 第二種作法是轉到字串後, 反轉字串並比對兩字串

## 3.程式碼

* 第一種作法

```
class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        string str = to_string(x);
        for (int i = 0; i < str.size(); i++) {
            if (str[i] != str[str.size() - 1 - i]){
                return false;
            }
        }
        return true;
    }
};
```

* 第二種作法

```
class Solution {
public:
    bool isPalindrome(int x) {
        string s=to_string(x);
        string ori=s;
        reverse(s.begin(),s.end());
        return s==ori;
    }
};
```

## 4.Performance

* 第一種作法

![](/files/-LI-HCv59ajKZzf7MXjO)

* 第二種作法

![](/files/-LI-Gz2nI4mWjmd9_h7Q)
