- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrev_int.java
More file actions
Latest commit
22 lines (18 loc) · 589 Bytes
/
Copy pathrev_int.java
File metadata and controls
22 lines (18 loc) · 589 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Link: https://leetcode.com/problems/reverse-integer/
classSolution {
publicintreverse(intx) {
if(x==0){
return0;
}
intpop;
intrev=0;
while((x>0)||(x<0)){
pop = x%10;
x = x/10;
if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return0;
if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return0;
rev = (rev*10) + pop;
}
returnrev;
}
}