首页 > 代码库 > LeeCode-Reverse Integer

LeeCode-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 Answer=0;
        
        if(-10 < x && x < 10) return x;  
        bool z = true;  
        if(x < 0) {z = false; x = -x;}  
    
        while(x!=0)
        {
            Answer=Answer*10+x%10;
            x=x/10;
        }
        if(z==false)
            Answer=-Answer;
            
       return Answer;
    }
};


LeeCode-Reverse Integer