-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution7.h
67 lines (52 loc) · 1.06 KB
/
Solution7.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Author: huanglijun
// Date : 2019/5/23
// Desc : LeetCode Solution7
//
#ifndef TESTCODE_SOLUTION7_H
#define TESTCODE_SOLUTION7_H
/*
7 Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
*/
int reverse(int x) {
if(x == INT_MIN) return 0;
bool flag = x >= 0 ? true : false;
if(!flag)
x = -x;
vector<int> vt;
while(x > 0){
if(x >= 10){
int tmp = x%10;
vt.push_back(tmp);
x /= 10;
if(x == 0){
vt.push_back(tmp);
break;
}
} else {
vt.push_back(x);
break;
}
}
double num = 0;
int vt_size = (int)vt.size();
for(int i = 0; i < vt_size; i++){
cout<<vt[i]<<" "<<i<<endl;
num += vt[i];
if(num >= INT_MAX) return 0;
if(i < vt_size-1) num *= 10;
}
num = flag ? num : -num;
return (int)num;
}
#endif //TESTCODE_SOLUTION7_H