Skip to content

Commit

Permalink
https://leetcode.com/problems/reverse-integer/submissions/1206449247/
Browse files Browse the repository at this point in the history
  • Loading branch information
s50600822 committed Mar 17, 2024
1 parent c9ebb64 commit caafc3c
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public int reverse(int x) {
int sign = x > 0 ? 1 : -1;
x *= sign;
int reversedNumber = 0;
while(x > 0) {
int digit = x % 10;
int newNumber = reversedNumber * 10 + digit;
if((newNumber - digit) / 10 != reversedNumber) {
return 0;
} else {
reversedNumber = newNumber;
}
x /= 10;
}
return reversedNumber * sign;
}

public static void main(String[] args) {
//https://leetcode.com/problems/reverse-integer/description/
final Solution sol = new Solution();
assert sol.reverse(123) == 321;
assert sol.reverse(-123) == -321;
assert sol.reverse(120) == 21;
assert sol.reverse(-120) == -21;
}
}

0 comments on commit caafc3c

Please sign in to comment.