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

LeetCode 7. Reverse Integer C++--带正负号数字反转

2017-08-09 10:29 489 查看
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Note:

The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
#include <iostream>
using namespace std;

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

int main(){
cout<<reverse(-123)<<endl;
return 0;
}

1032 / 1032 test cases passed.

Status: Accepted

Runtime: 22 ms

Your runtime beats 30.40 % of cpp submissions
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息