-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.cpp
More file actions
46 lines (41 loc) · 1022 Bytes
/
7.cpp
File metadata and controls
46 lines (41 loc) · 1022 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
/*************************************************************************
> File Name: 7.cpp
> Author: Alan
> Mail: [email protected]
> Created Time: Tue 10 Nov 2015 11:43:19 AM CST
> Problem Name: Reverse Integer
> Difficulty: Easy
> Description:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
************************************************************************/
#include<iostream>
#include<climits>
using namespace std;
class Solution
{
public:
int reverse(int x)
{
if(x == 0)
{
return 0;
}
int flag = x > 0 ? 1 : -1;
long sum = 0;
long tmp = abs((long)x);
while(tmp)
{
sum = sum * 10 + tmp % 10;
tmp /= 10;
}
return (sum > INT_MAX) ? 0 : sum * flag;
}
};
int main()
{
Solution sol = Solution();
int num = 1234567;
cout << "The reversed number is: " << sol.reverse(num) << endl;
}