forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_7.cpp
41 lines (33 loc) · 859 Bytes
/
_7.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
//Faster than 100% submissions
class Solution {
public:
int reverse(int x) {
long int res = 0;
bool isMinus = false;
//To add the minus sign if any negative number
if(x<0){
isMinus = true;
}
//convert into positive number
x = abs(x);
//Function to reverse number
while(x>0){
int last = x%10;
res = res*10 + last;
x/=10;
}
//Adding minus sign in the result
if(isMinus){
res *= -1;
}
//Range of int
int mn = -2147483648, mx = 2147483647;
//Checking if is in range of int
if(res > mn and res < mx){
res = res;
}else{
res = 0;
}
return res;
}
};