版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cyrususie/article/details/78850507
Analysis
边界问题要考虑,题目中范围是32位的有符号数,所以为
Answer
#include <iostream>
#include <sstream>
#include <math.h>
using namespace std;
class Solution {
public:
int reverse(int x) {
stringstream ss;
ss << x;
string s1 = ss.str();
cout << s1 << endl;
string ans = "";
if (s1.length() == 1) return x;
if (s1[s1.length()-1] == '0') {
} else {
ans += s1[s1.length()-1];
}
for (int i = s1.length()-2; i > 0; i--) {
cout << s1[i] << endl;
ans = ans + s1[i];
}
if (s1[0] == '-') {
ans = '-' + ans;
} else {
ans += s1[0];
}
cout << ans << endl;
stringstream ss2;
ss2 << ans;
long int result = 0;
ss2 >> result;
if (abs(result) > pow(2, 31)-1) return 0;
return result;
}
};