We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 736e574 commit 6c1b307Copy full SHA for 6c1b307
src/ReverseInteger.java
@@ -0,0 +1,24 @@
1
+// https://leetcode.com/problems/reverse-integer/
2
+
3
+import java.util.Scanner;
4
5
+public class ReverseInteger {
6
+ public static void main(String[] args) {
7
+ Scanner scanner = new Scanner(System.in);
8
+ int number = scanner.nextInt();
9
+ System.out.println(reverse(number));
10
+ }
11
12
+ private static int reverse(int number) {
13
+ boolean isPositive = number >= 0;
14
+ StringBuilder accumulator = new StringBuilder(number + "");
15
+ if (!isPositive) {
16
+ accumulator.replace(0, 1, "");
17
18
+ try {
19
+ return Integer.parseInt(accumulator.reverse().insert(0, isPositive ? "" : "-").toString());
20
+ } catch (NumberFormatException exception) {
21
+ return 0;
22
23
24
+}
0 commit comments