7. Reverse Integer
problem description
Input: 123
Output: 321Input: -123
Output: -321Input: 120
Output: 21algorithm thought
code
algorithm analysis
Last updated
Input: 123
Output: 321Input: -123
Output: -321Input: 120
Output: 21Last updated
class Solution {
public:
int reverse(int x) {
bool bo=true;
if(x<0){
if(x==INT_MIN)
return 0;
bo=false;
x=-x;
}
string s = to_string(x);
std::reverse(s.begin(),s.end());
int i=0;
while(s[i]=='0')
i++;
s.erase(0,i);
//cout<<s<<' '<< (s>"2147483647");
if(s.size()>10||(s.size()==10&&s>"2147483647"))
return 0;
i=atol(s.c_str());
return bo?i:-i;
}
};