7. Reverse Integer(C++)

Reverse digits of an integer.

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

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

Solution:

class Solution {
public:
  int reverse(int x) {
    long long y=0;
    while(x){
      y=y*10+x%10;
      x/=10;
    }
    return (y<INT_MIN || y>INT_MAX)?0:y;
  }
};

posted @ 2017-03-01 20:53  DevinGu  阅读(186)  评论(0编辑  收藏  举报