将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。
样例:
给定 x = 123,返回 321
给定 x = -123,返回 -321
#ifndef C413_H
#define C413_H
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class Solution {
public:
/*
* @param n: the integer to be reversed
* @return: the reversed integer
*/
int reverseInteger(int n) {
// write your code here
int flag = 0;
if (n < 0)
flag = 1;
string str_num = to_string(abs(n));
reverse(str_num.begin(), str_num.end());
long long_num = stol(str_num);
if (long_num>INT_MAX)
return 0;
else
{
if (flag == 0)
return stoi(str_num);
else
return 0 - stoi(str_num);
}
}
};
#endif