您的位置:首页 > 其它

LeetCode 7

2018-03-16 16:29 176 查看

LeetCode 7

Reverse Integer

Reference:http://blog.csdn.net/booirror/article/details/43150235

Problem Description:

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 reverNum = 0;
while(x != 0) {
int n = x % 10;
x = x / 10;
if (reverNum > INT_MAX/10 || reverNum < INT_MIN/10) {
% 先对reverNum进行溢出判断,如果没有先除以10,后面*10的操作可能会溢出
return 0;
}
reverNum = reverNum*10+n;
}
return reverNum;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 溢出