-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_ReverseInt.cpp
49 lines (44 loc) · 1 KB
/
4_ReverseInt.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
47
48
#include <iostream>
#include<vector>
#include<cmath>
using namespace std;
class Solution {
public:
int reverse(int x) {
/* vector<int> vec;
while(x!=0){
int i = x%10;
x /= 10;
vec.push_back(i);
}
long long res = 0;
for(unsigned int i = 0; i < vec.size(); i++){
cout<<vec[i];
res += vec[i] * pow(10, vec.size() - i - 1);
}
cout<<endl;
cout<<res<<endl;
if(res >= pow(2,31) - 1 || res <= -pow(2,31))return 0;
return res;
*/
int array[100];
int size = 0;
while(x!=0){
array[size++] = x%10;
x /= 10;
}
long long res = 0;
for(int i = 0; i < size; i++){
res += array[i] * pow(10,size - i - 1);
}
if(res >= pow(2,31) - 1 || res <= -pow(2,31))return 0;
return res;
}
};
int main()
{
Solution s;
int res = s.reverse(-123);
cout<<res<<endl;
return 0;
}