码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Palindrome Number

时间:2015-08-15 16:15:53      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

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 };

 

[LeetCode] Palindrome Number

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4732539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!