-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.cpp
46 lines (42 loc) · 776 Bytes
/
7.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
class Solution {
public:
int reverse(int x) {
string s = to_string(x);
bool negative = x < 0;
if(negative){
s = s.substr(1);
}
reverseString(s);
int result = 0;
try{
result = stoi(s);
}catch(const out_of_range& e){
result = 0;
}
return result;
}
private:
void reverseString(string &s){
int length = s.length();
if(length==0){
return;
}
int left = 0, right = length-1;
while(left<right){
char tmp = s[left];
s[left] = s[right];
s[right] = tmp;
left++;
right--;
}
}
};
int main(){
Solution s;
int r = s.reverse(1000000003);
cout << r << endl;
}