1. 程式人生 > >LeetCode-7 反轉整數

LeetCode-7 反轉整數

  • C++
class Solution {
private:
    const static int MAX_INT = 2147483647;
public:
    int reverse(int x) {
        bool mark = false;
        if(x < 0){
            mark = true;
            x *= -1;
        }
        long long reversed = 0;
        while(x > 0){
            reversed *= 10;
            reversed += x % 10;
            x /= 10;
        }
        if(reversed > MAX_INT){return 0;}
        if(mark){
            return -reversed;
        }
        return reversed;
    }
};