Reverse Interger

考虑越界的问题

    int reverse(int x) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        bool bNega = false;
        if(x<0)
        {
            bNega = true;
            x = -x;
        }
        
        long long res = 0;
        while(x)
        {
            res = res*10+x%10;
            x /= 10;
        }
        
        if(res>INT_MAX)
            return (bNega?INT_MIN:INT_MAX);
        else
            return (bNega?-res:res);
        
    }

  

posted @ 2013-10-06 09:25  summer_zhou  阅读(166)  评论(0编辑  收藏  举报