Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public int reverse(int x) {
- int result = 0;
- while (x != 0){
- int ones = x % 10;
- x = x / 10;
- Integer tempMult = safeMult(result, 10);
- if (tempMult == null){
- return 0;
- }
- result = tempMult;
- result += ones;
- }
- return result;
- }
- private int sigMax(int x, int y){
- return Integer.signum(x) == Integer.signum(y) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
- }
- private Integer safeMult(int a, int b){
- int maximum = sigMax(a, b);
- if (a != 0 && (b > 0 && b > maximum / a ||
- b < 0 && b < maximum / a))
- {
- return null;
- }
- return a*b;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement