题目描述:(链接)
Determine whether an integer is a palindrome. Do this without extra space.
解题思路:
分离出首位,末位,分别比较!
1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 // 负数返回false 5 if (x < 0) { 6 return false; 7 } 8 9 int first = 0; 10 int last = 0; 11 12 // 计算x有多少位 13 int len = 1; 14 while (x / len >= 10) len *= 10; 15 16 int div = 1; 17 while (true) { 18 if (len == 1) break; 19 first = (x / len) % 10; 20 last = (x / div) % 10;; 21 if (first != last) { 22 return false; 23 } 24 25 len /= 10; 26 div *= 10; 27 } 28 29 return true; 30 } 31 };