美文网首页
7.Reverse Integer

7.Reverse Integer

作者: 林里icer | 来源:发表于2018-03-28 21:50 被阅读0次

简直是卖萌的...
解决方法:

int reverse(int x) {
        int res = 0;
        while(x){
            if(abs(res) > INT_MAX/10) return 0;
            res = res*10+x%10;
            x/=10;
        }
        return res;
    }

不过这个有点慢 31ms
下面是8ms的版本,不过没啥用,之所以快是因为加快了输入输出的速度。

static int x = []() { 
    std::ios::sync_with_stdio(false); 
    cin.tie(NULL);  
    return 0; 
}();
static int pr = []() { 
    std::ios::sync_with_stdio(false); 
    cin.tie(NULL);  
    return 0; 
}();


class Solution {
public:
    int reverse(int x) {
        int result = 0;
        while (x)
        {
            pr = result * 10 + x % 10;
            if (result != pr / 10) return 0;
            result = pr;
            x /= 10;
        }
        return result;
    }
};

相关文章

网友评论

      本文标题:7.Reverse Integer

      本文链接:https://www.haomeiwen.com/subject/vatpcftx.html