Posted on 2014-01-11 02:23
Uriel 閱讀(104)
評論(0) 編輯 收藏 引用 所屬分類:
LeetCode
判斷一個數(shù)字是否是回文的,跟Reverse Integer的做法一樣,然后判斷正過來和倒過來的數(shù)是否相同
注意:小于10的非負(fù)數(shù)都是回文的,所有負(fù)數(shù)都不是回文的,末尾有0的大于等于10的數(shù)也都不是回文的!
1 class Solution {
2 public:
3 bool isPalindrome(int x) {
4 if(x < 0) return false;
5 if(x < 10) return true;
6 if(!(x % 10)) return false;
7 int tp = x, nt = 0;
8 while(tp > 0) {
9 nt = nt * 10 + (tp % 10);
10 tp /= 10;
11 }
12 if(nt == x) return true;
13 return false;
14 }
15 };