-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-Reverse.h
More file actions
59 lines (49 loc) · 834 Bytes
/
07-Reverse.h
File metadata and controls
59 lines (49 loc) · 834 Bytes
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
#pragma once
#include <iostream>
#include <vector>
#include <math.h>
#include <limits.h>
using namespace std;
class Solution
{
public:
void Test07();
int reverse(int x);
protected:
private:
};
void Solution::Test07()
{
int nTest = reverse(1463847412);
}
int Solution::reverse(int x)
{
int nFlag = 1;
vector<int> Array;
if (x < 0)
nFlag = -1;
if (x > -9 && x < 9)
return x;
while (x)
{
int nResidul = x % 10;
nResidul *= nFlag;
Array.push_back(nResidul);
x = x / 10;
}
int nReverse = 0;
for (int i = 0; i< Array.size();i++)
{
if(Array[i]==0 && nReverse == 0)
continue;
nReverse *= nFlag;
if (nReverse > INT_MAX / 10)
return 0;
nReverse *= nFlag;
nReverse = nReverse * 10 + Array[i] * nFlag;
if (nReverse > INT_MAX)
return 0;
}
cout << nReverse << endl;
return nReverse;
}