【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 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.

思路:

如果它的input是一個string類型,我們就可以利用string的函數對首尾元素進行分析。只可惜它要求我們不能用多餘空間,並且input還是一個int類型。

所以我的想法是,用一個int n 去記住我們這個數字的長度。然後每次都把最高位和最低位單獨取出來比較。

代碼:

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0) return false;
        if(x/10==0) return true;
        
        int rem = x;
        int n = 0;  //to keep the value of how long the number is.
        while(x/10!=0){
            n++;
            x=x/10;
        }
        n++;
        
        int head = rem, tail = 0;
        for(int i = n-1; i>0 && n>0; n-=2, i = n-1){
            tail = rem%10;
            while(i>0){
                head = head/10;
                i--;
            }
            if (head!=tail) return false;
            rem -=head*pow(head,10);
            rem = rem % 10;
        }
        return true;
    }
};
不過卡在了一個很奇怪的數字上:

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0) return false;
        if(x/10==0) return true;
        
        int rem = x;
        int n = 0;  //to keep the value of how long the number is.
        while(x/10!=0){
            n++;
            x=x/10;
        }
        n++;
        
        int head = rem, tail = 0;
        for(int i = n-1; i>0 && n>0; n-=2, i = n-1){
            tail = rem%10;
            while(i>0){
                head = head/10;
                i--;
            }
            if (head!=tail) return false;
            rem = rem / 10;
            rem = rem - head*pow(n-2,10);
            head = rem;
        }
        return true;
    }
};

哭…

=================================================================================================================================

class Solution {
public:
    bool isPalindrome(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (x < 0)
            return false;
        if (x == 0)
            return true;
            
        int base = 1;
        while(x / base >= 10)
            base *= 10;
            
        while(x)
        {
            int leftDigit = x / base;
            int rightDigit = x % 10;
            if (leftDigit != rightDigit)
                return false;
            
            x -= base * leftDigit;
            base /= 100;
            x /= 10;
        }
        
        return true;
    }
};
這是網上的代碼,明明思路一樣啊摔!!!!!!

但是總結一下。雖然很多題每次看着都是很有思路的,而且大部分時候思路都是完全正確的,但依然寫不好代碼。主要是找不到那個最簡化或者說最適合用來寫代碼的點。比如這道題,網絡代碼直接用了一個變量base來記錄位數,而我偏偏把這個base量化成了n位,在後面的計算中其實很不方便,也很容易出錯。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章