ogv

Untitled

ogv
Nov 2nd, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.55 KB | None | 0 0
  1. // Runtime: 1 ms, faster than 100.00% of Java online submissions for Reverse Integer.
  2. class Solution {
  3.     public int reverse(int x) {
  4.         if (x == 0) return 0;
  5.         if (x == Integer.MIN_VALUE) return 0;
  6.        
  7.         if (x < 0) return -reverse(-x);
  8.        
  9.         int r = 0;
  10.        
  11.         while (x > 0) {            
  12.             int d = x % 10;
  13.             r = 10 * r + d;
  14.            
  15.             if (r % 10 != d) return 0;
  16.                                  
  17.             x /= 10;
  18.         }
  19.        
  20.         return r;
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment