Leet code problem 7: reverse integer digit

Posted nosaferyao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leet code problem 7: reverse integer digit相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    int reverse(int x) {
        int ret = 0;
    int int_max_divide_10 = INT_MAX / 10;
    int int_max_mod_10 = INT_MAX % 10;
    int int_min_divide_10 = INT_MIN / 10;
    int int_min_mod_10 = INT_MIN % 10;
    while (x != 0){
        int remainder =  x % 10;
        if (x > 0 && (ret > int_max_divide_10 ||
                (ret == int_max_divide_10 && remainder > int_max_mod_10))
                ||
                x < 0 && (ret < int_min_divide_10 ||
                    (ret == int_min_divide_10 && remainder < int_min_mod_10)))
        {
            return 0;
        }
        ret *= 10;
        ret += remainder;
        x /= 10;
    }
        return ret;
    }
};

 

以上是关于Leet code problem 7: reverse integer digit的主要内容,如果未能解决你的问题,请参考以下文章

leet code 54. 螺旋矩阵

leet code 54. 螺旋矩阵

leet code 86 分隔链表

leet code 86 分隔链表

leet code Two Sum

Leet Code OJ 189. Rotate Array [Difficulty: Easy]