首页 > 其他 > 详细

[LeetCode] [Palindrome Number 2012-01-04]

时间:2014-05-23 02:44:10      阅读:433      评论:0      收藏:0      [点我收藏+]

Determine whether an integer is a palindrome. Do this without extra space.

if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the remaining would simply be "2", not "02". 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
    bool isPalindrome(int x) {
        //Palindrome means 回文
         
        if(x < 0) return false;
        if(x <= 9) return true;
        int t = x;
        int n=0;
        while(t > 0)
        {
            n++;
            int d = t%10;
            t=(t-d)/10;
        }
         
         
        int* s = new int[n];
        t = x;
        n=0;
        while(t > 0)
        {
            int d = t%10;
            s[n++] = d;
            t=(t-d)/10;
        }
         
        for(int i = 0; i<n/2;i++)
        {
            if(s[i] != s[n-i-1])
            {
                return false;
            }
        }
        return true;
    }
};

 

[LeetCode] [Palindrome Number 2012-01-04],布布扣,bubuko.com

[LeetCode] [Palindrome Number 2012-01-04]

原文:http://www.cnblogs.com/xxiao1119/p/3742662.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!