您的位置:首页 > 编程语言 > C语言/C++

LeetCode-Reverse Integer-解题报告

2015-07-08 21:26 387 查看
原题链接 https://leetcode.com/problems/reverse-integer/

Reverse digits of an integer.

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

水水
class Solution {
public:
int reverse(int x) {
bool sign = true;
if (x < 0)x = -x, sign = false;
long long ans = 0;
while (x)
{
ans = ans * 10 + x % 10;
x /= 10;
}

if (!sign)ans = -ans;
if (ans > INT_MAX || ans < INT_MIN)return 0;

return ans;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ leetcode