forked from sarbjotsingh1/practicecpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse_Integer
More file actions
29 lines (28 loc) · 808 Bytes
/
Reverse_Integer
File metadata and controls
29 lines (28 loc) · 808 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
//https://leetcode.com/problems/reverse-integer/
//Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
class Solution {
public:
int reverse(int n)
{
int sign = n<0 ? -1 : 1;
long long x = (long long)(n) * sign;
long long remainder,revers=0;
if(x >=0 && x <= 9)
{
return x;
}
while(x != 0)
{
remainder = x%10;
revers = revers*10 + remainder;
x = x/10;
}
if (sign == 1 && revers > 2147483647) {
return 0;
}
if (sign == -1 && revers > 2147483648) {
return 0;
}
return revers*sign;
}
};