LeetCode 7: Reverse Integer

题目描述

Reverse digits of an integer.

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

数字x不停对10取整,变量result*10加余数,直至数字x==0,那么result就是x的翻转

几个特殊情况:

1.负数

2.溢出

    int reverse(int x) {
        int flag = 1;
        if(x<0){
            flag = -1;
            x = -x;
        }
        long long result = 0;
        while(x){
            result = result*10 + x%10;
            if(result>0x7FFFFFFF||result<(signed int)0x80000000) return 0;
            x = x/10;
        }
        return flag*result;
    }
原文地址:https://www.cnblogs.com/xiamaogeng/p/4350801.html