1. 程式人生 > >領釦(LeetCode)整數反轉

領釦(LeetCode)整數反轉

參考了答案,不過略有改動

class Solution {
public:
     int reverse(int x) {
        long rev = 0;//定義長整形避免溢位
        int pop;
        while (x != 0) {
            pop= x % 10;
            x/=10;
            rev = rev * 10 + pop;
        }
         if(rev>INT_MAX||rev<INT_MIN)
             return 0;
         else
             return rev;
    }
};