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

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

时间:2014-05-23 02:45:15      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:class   blog   c   code   ext   color   

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]

标签:class   blog   c   code   ext   color   

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

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