[leetcode] 7. Reverse Integer

題目鏈接:https://leetcode.com/problems/reverse-integer/

Reverse digits of an integer.

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

思路

注意溢出情況處理

class Solution {
public:
    int reverse(int x) {
        int result = 0;
        while(x){
            if(INT_MAX/10 < result || INT_MIN/10 > result)
                return 0;
            result = result*10 + x % 10;
            x = x / 10;
        }
        return result;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章