[LeetCode] Palindrome Number [13]

题目

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

click to show spoilers.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

原题链接(点我)

解题思路

判断一个int型整数是不是回文数字,这个题也不难,依次取得数字最高位和最低位进行比较,就可以判断是不是回文数字。需要注意的是负数不是回文数字。

代码实现

代码一

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0) return false;
        int times = 1;
        int p = x;
        while(p>=10){
            p /= 10;
            times *= 10;
        }
        int low = 0, high = 0;
        while(x!=0){
            low = x%10;
            high = x/times;
            if(low != high) return false;
            x = x%times;
            x /= 10;
            times /= 100;
        }
        return true;
    }
};

代码二

思路相同,不同算法,明显代码一要简洁很多

class Solution {
public:
    bool isPalindrome(int x) {
        if(x==0x80000000 || x<0) return false;
        if(x<0) x= -x;
        if(-9<=x && x<=9)
            return true;
        int n=0;
        int copy = x;
        while(copy != 0){
            ++n;
            copy /= 10;
        }
        copy = x;
        int i=n-1;
        while(i>=n/2){
            int h = nIndex(i);
            int high = x/h;
            int low = copy%10;
            if(high!=low) return false;
            x %= h;
            copy /= 10;
            --i;
        }
        return true;
    }
    int nIndex(int n){
        int temp =1;
        for(int i=0; i<n; i++)
            temp *= 10;
        return temp;
    }
};

-----------------------------------------------------------------------------------------------------------------------------------------

如果你觉得本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.

你可以搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/28913069
)

[LeetCode] Palindrome Number [13]

时间: 2024-10-27 18:54:45

[LeetCode] Palindrome Number [13]的相关文章

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

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 r

LeetCode——Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

LeetCode:Palindrome Number

基本思路:把 int 转换成string  借用stringstream  时间复杂度O(n) 1 class Solution { 2 public: 3 public: 4 bool isPalindrome(int x) { 5 6 stringstream ss; 7 ss<<x; 8 string str=ss.str(); 9 int sfront = 0; 10 int sback=str.length()-1; 11 while(sfront<sback) 12 { 13

[LeetCode] Palindrome Number 验证回文数字

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

(LeetCode)Palindrome Number -- 判断回文数

Determine whether an integer is a palindrome. Do this without extra space. 解题分析: 题目很简单,但是对于要求需要看清楚. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ex

LeetCode—Palindrome Number 数字是否是回文数字

Determine whether an integer is a palindrome. Do this without extra space. 检测当前数字是否是回文数字,同时不能增加额外的内存空间,这里一个注意的点就是 负数 都不可能是回文数字 然后是检测出来每一位数字进行比较 代码还是写得比较繁琐,主要的一个点就是数字的位数是基数位和偶数位的时候处理的过程是不同的 class Solution { public: int GetH(int x,int num)//获得对应位置的数字 {

LeetCode:Palindrome Number,Reverse Integer

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

[leetcode] Palindrome Number(不使用额外空间)

本来判断回文串是一件很容易的事情,只需要反转字符串后在与原字符串相比较即可.这道题目明确说明不能使用额外的空间,那么使用将其分解连接成字符串的方法便不是可行的.只好采用数学的方式: 每次取最高位和最低位相比较,总的位数可以用一个while先处理出来,循环直至取余和除数相等. 具体见代码: class Solution { public: bool isPalindrome(int x) { if(x<0) //special due return false; if(x<10) return

leetcode:Palindrome Number【Python版】

一次AC 题目要求中有空间限制,因此没有采用字符串由量变向中间逐个对比的方法,而是采用计算翻转之后的数字与x是否相等的方法: 1 class Solution: 2 # @return a boolean 3 def isPalindrome(self, x): 4 o = x 5 ret = 0 6 flag = 1 7 if x < 0: 8 return False 9 while(x!=0): 10 ret = ret*10+x%10 11 x = x/10 12 return ret