/**
* Reverse digits of an integer.
*
* Example1: x = 123, return 321
* Example2: x = -123, return -321
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//关键之处:注意边界范围
int reverse(int x)
{
long rs = 0;
while (x)
{
rs = rs * 10 + x % 10;
x /= 10;
}
return (rs > INT_MAX || rs < INT_MIN) ? 0 : rs;
}
int main()
{
cout << reverse(-123);
system("pause");
return 0;
}
【leetcode】7. Reverse Integer
最新推荐文章于 2022-06-06 18:52:41 发布