Skip to content

Commit 35bc865

Browse files
committed
Time: 0 ms (100%), Space: 6.5 MB (12.45%) - LeetHub
1 parent 13701ec commit 35bc865

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int reverse(long long x) {
4+
string str;
5+
str = to_string(x);
6+
string s;
7+
s = str;
8+
for(int i = 0; i < s.length(); i++)
9+
{
10+
str[i] = s[str.length() - 1 - i];
11+
}
12+
if(str.back() == '-')
13+
{
14+
str.insert(str.begin(), '-');
15+
str.pop_back();
16+
}
17+
while(str[0] == '0')
18+
str.erase(str.begin());
19+
if(!str.size())
20+
return 0;
21+
long long temp = stoll(str);
22+
if(temp > INT_MAX || temp < INT_MIN)
23+
return 0;
24+
return stoi(str);
25+
26+
}
27+
};

0 commit comments

Comments
 (0)