LeetCode 刷题笔记

7. Reverse Integer

Posted by 飞白 on May 30, 2017

Reverse digits of an integer.

Example1: x = 123, return 321 Example2: x = -123, return -321

Solution 1: Direct Reverse

Construct the new number directly

  1. take care of sign
  2. using long long in case of overflow

Code:

int reverse(int x) {
        if(x == 0) return 0;
        int sign = x>0 ? 1:-1;
        long long t = x;
        t = abs(t);
        
        long long res = 0;
        
        while(t != 0)
        {
            res = 10*res + t%10;
            t /= 10;
        }
        
        res = sign*res;
        
        return (res>INT_MAX || res<INT_MIN) ? 0:(int)res;
    }