mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2026-07-03 15:55:36 +00:00
Create 9. Palindrome Number.md
This commit is contained in:
parent
8dfd049c83
commit
f59904e5a9
23
9. Palindrome Number.md
Normal file
23
9. Palindrome Number.md
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# [9. Palindrome Number](https://leetcode.com/problems/palindrome-number/description/)
|
||||||
|
# 思路
|
||||||
|
判断一个int型数是否为回文数。
|
||||||
|
首先小于0肯定不是可以直接返回false;
|
||||||
|
对于一般的int型数,可以先将其转换成字符串,这样判断是否回文就快多了。
|
||||||
|
# C++
|
||||||
|
```
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool isPalindrome(int x) {
|
||||||
|
if(x < 0) return false;
|
||||||
|
if(x < 10) return true;
|
||||||
|
if(x % 10 == 0) return false;
|
||||||
|
// std::string s = std::to_string(x); // 注意学习这种将int型转字符串的简单方法
|
||||||
|
auto s = std::to_string(x);
|
||||||
|
int low = 0, high = s.size() - 1;
|
||||||
|
while(low < high){
|
||||||
|
if(s[low++] != s[high--]) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
Loading…
Reference in New Issue
Block a user