Reverse Integer

Reverse Integer 

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

题目比较简单,注意越界和负数情况即可!

 Runtime: 16 ms

#include <stdio.h>
#include <stdlib.h>

class Solution {
public:
    int reverse(int x) {
        long long  num = 0;
        bool isPositive = true;
        if (x < 0){
            isPositive = false;
            x = -x;
        }
        while (x > 0){
            int temp = x % 10;
            num = num * 10 + temp;
            x = x / 10;
        }
        if (num > INT_MAX){
            return 0;
        }
        else{
            if (isPositive == false)
                num = num*-1;
            return num;
        }
    }
};

int main(){
    Solution solution;
    printf("%d", solution.reverse(-123));
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/JeromeHuang/p/4442551.html