Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 1 ms, faster than 100.00% of Java online submissions for Reverse Integer.
- class Solution {
- public int reverse(int x) {
- if (x == 0) return 0;
- if (x == Integer.MIN_VALUE) return 0;
- if (x < 0) return -reverse(-x);
- int r = 0;
- while (x > 0) {
- int d = x % 10;
- r = 10 * r + d;
- if (r % 10 != d) return 0;
- x /= 10;
- }
- return r;
- }
- }
- // Runtime: 1 ms, faster than 100.00% of Java online submissions for Reverse Integer.
- class Solution {
- final int MAX_10_MULTIPLIER = Integer.MAX_VALUE / 10;
- public int reverse(int x) {
- if (x == 0) return 0;
- if (x == Integer.MIN_VALUE) return 0;
- if (x < 0) return -reverse(-x);
- int r = 0;
- while (x > 0) {
- int d = x % 10;
- if (r > MAX_10_MULTIPLIER) return 0;
- r *= 10;
- if (r > Integer.MAX_VALUE - d) return 0;
- r += d;
- x /= 10;
- }
- return r;
- }
- }
Add Comment
Please, Sign In to add comment