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 4f36155 commit 2682dc4Copy full SHA for 2682dc4
sequence/Reverse.java
@@ -1,26 +1,21 @@
1
package Algorithms.sequence;
2
public class Reverse {
3
- public int reverse(int x) {
+ 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) {
9
long n = x;
10
- boolean neg = false;
- if (n < 0) {
- neg = true;
- n = -n;
- }
11
-
12
long ret = 0;
13
- while (n > 0) {
+ while (n != 0) {
14
long left = n % 10;
15
ret *= 10;
16
ret += left;
17
n /= 10;
18
}
19
20
- if (neg) {
21
- ret = -ret;
22
23
24
return (int)ret;
25
26
0 commit comments