LeetCode7: 反转整数
作者:互联网
https://leetcode.com/problems/reverse-integer/
问题描述
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123 Output: 321
Example 2:
Input: -123 Output: -321
Example 3:
Input: 120 Output: 21
Solution
class Solution {
public:
int reverse(int x) {
int ret = 0;
while (x != 0)
{
int val = x % 10;
x = x /10;
if(ret > INT_MAX / 10 || (ret == INT_MAX / 10 && val > 7))
return 0;
if(ret < INT_MIN / 10 || (ret == INT_MIN / 10 && val < -8))
return 0;
ret = ret * 10 + val;
}
return ret;
}
};
标签:LeetCode7,val,10,int,反转,ret,INT,整数,integer 来源: https://blog.csdn.net/sinat_31275315/article/details/100043133