1. 程式人生 > >LeetCode7——Reverse Integer(將一個整數反轉,注意溢位的處理)

LeetCode7——Reverse Integer(將一個整數反轉,注意溢位的處理)

題目:


解法一:


注意long long型別,表示64bit數字。

解法二:

class Solution {
public:
    int reverse(int x) {
        int ans = 0;
        while (x) {
            int temp = ans * 10 + x % 10;
            if (temp / 10 != ans)//溢位後,這裡就會不成立了
                return 0;
            ans = temp;
            x /= 10;
        }
        return ans;
    }
};