-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcodeReverseInteger.cpp
More file actions
68 lines (58 loc) · 1.34 KB
/
Copy pathLeetcodeReverseInteger.cpp
File metadata and controls
68 lines (58 loc) · 1.34 KB
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
68
class Solution {
public:
int reverse(int x) {
stack<int>r;
stack<int>tens;
if((x<0&&x>-9)||(x>=0&&x<=9))
return x;
for(int i = 9; i>=0; i--)
{
int a =x/(pow(10,i));
r.push(a);
x=x-(a*(pow(10,i)));
}
bool headzero = true;
while(r.empty()==false||headzero == true)
{
if(r.top()!=0)
{
headzero = false;
}
if(headzero==false)
{
tens.push(r.top());
}
r.pop();
}
headzero = true;
while(headzero == true)
{
if(tens.top()!=0)
{
headzero = false;
}
else
{
tens.pop();
}
}
for(int i = 0; i<=9;i++)
{
if(tens.empty()==true)
{
break;
}
else
{
long temp = x+(tens.top())*pow(10,i);
x+=(temp-x);
if(temp!=x)
{
return 0;
}
tens.pop();
}
}
return x;
}
};