Skip to content

Commit 2682dc4

Browse files
committed
reverse
1 parent 4f36155 commit 2682dc4

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

sequence/Reverse.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
package Algorithms.sequence;
22
public class Reverse {
3-
public int reverse(int x) {
3+
public static void main(String[] args) {
4+
System.out.println(reverse(-1234));
5+
System.out.println(reverse(100));
6+
}
7+
8+
public static int reverse(int x) {
49
long n = x;
510

6-
boolean neg = false;
7-
if (n < 0) {
8-
neg = true;
9-
n = -n;
10-
}
11-
1211
long ret = 0;
13-
while (n > 0) {
12+
while (n != 0) {
1413
long left = n % 10;
1514
ret *= 10;
1615
ret += left;
1716
n /= 10;
1817
}
1918

20-
if (neg) {
21-
ret = -ret;
22-
}
23-
2419
return (int)ret;
2520
}
2621
}

0 commit comments

Comments
 (0)