-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7. Reverse Integer.cpp
More file actions
27 lines (26 loc) · 859 Bytes
/
7. Reverse Integer.cpp
File metadata and controls
27 lines (26 loc) · 859 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
//DAY 6 PROBLEM 2
class Solution {
public:
int reverse(int x) {
if(x>=INT_MAX)
return 0;
string str=to_string(abs(x));
int len=str.length()-1;
int new_num=0,rem=0;
while(x)
{
rem=x%10;
//INT_MAX has a value of 2147483647 and it is possible that a 10 digit number
//read from front is 1234567809 but on reversing gives a value greater than
//INT_MAX and hence an overflow, so we also have to check overflow while
//calculating new_num
if((new_num+rem*pow(10,len))>=INT_MAX||(new_num+rem*pow(10,len))<=INT_MIN)
return 0;
else
new_num+=rem*pow(10,len);
len--;
x=x/10;
}
return new_num;
}
};