#include <iostream>
using namespace std;
class Solution {
public:
int reverse(int x) {
long long multiple = 1;
while(multiple < x)
{
multiple = multiple*10;
}
multiple = multiple/10;
long long _x = 0;
while(x > 0 && x < 1073741824)
{
int a = x%10;
_x = _x + a*multiple;
x = x/10;
multiple = multiple/10;
}
if(_x > 1073741824)
{
_x = 0;
}
return _x;
}
};
// 输入:x = 123
// 输出:321
// -231 <= x <= 231 - 1 范围
int main(int argc, char const *argv[])
{
int x = 1073741823;
int _x = Solution().reverse(x);
cout << _x << endl;
return 0;
}
整数反转(数值大小为四个字节,数值大于0)
于 2022-08-05 11:45:26 首次发布