Leetcode 7 Reverse Integer

2018-01-12 15:09:26 浏览数 (1)

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

Easy,没什么好说的注意负数和溢出的情况

代码语言:javascript复制
class Solution 
{
public:
    int reverse(int x) 
    {
        long long ans=0,flag=0,x1=x;
        if(x1<0)
        {
            x1=-x1;
            flag=1;
        }
        while(x1)
        {
            ans*=(long long)10;
            ans =((long long)x1);
            x1/=10;
        }   
        if(ans>INT_MAX)
            return 0;
        if(flag)
            ans=-ans;
        return (int)ans;
    }
};

0 人点赞