[LeetCode] Palindrome Number

The most obvious idea is to maintain two divisors to get the most and least significant digits and compare them. Well, there are much more clever ideas, like this one, whose code is rewritten below. Play with it :-)

复制代码
 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         if (x < 0 || (x && !(x % 10))) return false;
 5         int s = 0;
 6         while (x > s) {
 7             s = s * 10 + x % 10;
 8             x /= 10;
 9         }
10         return x == s || x == s / 10;
11     }
12 };
复制代码

 

posted @   jianchao-li  阅读(159)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示