1. 程式人生 > >演算法習題篇之Palindrome

演算法習題篇之Palindrome

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true
Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:

Coud you solve it without converting the integer to a string?

判斷一個數是不是迴文數,這個數從前面和從後面讀起來都一樣。

你能夠不把這個數轉化為字串來進行判斷嘛!

思路很簡單,如果是負數肯定不對,是個位數為true;

其他的情況,先算出位數,然後對比相應位置的數是否相等就OK了

接下來就是我的垃圾程式碼;

class Solution {
    public boolean isPalindrome(int x) {
        if(x<0){
            return false;
        }
        if(x<10){
            return true;
        }
        int n=1;
        int temp = x;
        for(int i =1 ;i<=10;i++){
            temp = temp/10;
            if(temp>0){
                n++;
            }else {
                break;
            }
        }
        for(int i = 1;i<=n/2;i++){
            int temp1 = (int)(x%java.lang.Math.pow(10,i));
            int temp2 =(int) (x%java.lang.Math.pow(10,n+1-i));
            if(temp1==0||temp2==0){
                if(temp1!=0||temp2!=0){
                    return false;
                }
            }
            int temp3 = (int)(java.lang.Math.pow(10,(i-1)));
            int temp4 = (int)(java.lang.Math.pow(10,(n-i)));
            if(temp1/temp3!=temp2/temp4){
                return false;
            }
        }
        return true;
    }
}

當然還是有大神的乾貨的,但是差評比著好評多就不知道為什麼了,反正比我寫的好就對了,

然後程式碼如下,思路很簡單,設定一個新數y,從x的最後一位開始複製,直到y的值比著x大,迴圈結束

然後比較x和y。 或x和y/10 是否相等就ok了。

public class Solution {
    public bool IsPalindrome(int x) {
        // Special cases:
        // As discussed above, when x < 0, x is not a palindrome.
        // Also if the last digit of the number is 0, in order to be a palindrome,
        // the first digit of the number also needs to be 0.
        // Only 0 satisfy this property.
        if(x < 0 || (x % 10 == 0 && x != 0)) {
            return false;
        }

        int revertedNumber = 0;
        while(x > revertedNumber) {
            revertedNumber = revertedNumber * 10 + x % 10;
            x /= 10;
        }

        // When the length is an odd number, we can get rid of the middle digit by revertedNumber/10
        // For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123,
        // since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it.
        return x == revertedNumber || x == revertedNumber/10;
    }
}

比較清晰的一個思路:

設定一個list集合把x的每一位放到集合裡,嗯在比較就OK了!

class Solution {
public boolean isPalindrome(int x) {
if(x < 0){
return false;
}
else if(x < 10){
return true;
}

    ArrayList<Integer> original = new ArrayList<Integer>();
    
    while(x > 0){
        int remainder = x % 10;
        original.add(remainder);
        x = x / 10;
    }
    
    int left = 0;
    int right = original.size() - 1;

    while(left < right){
        if(original.get(right) != original.get(left)){
            return false;
        }
        left++;
        right--;
    }
    return true;
}
}

ok 這道簡單的題也就分析到這裡了。