-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReverseInteger.java
58 lines (47 loc) · 2.32 KB
/
ReverseInteger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class ReverseInteger {
// Question 4
// Write a function to reverse an integer using numeric operators and without
// using any arrays or other data structures.
//
// The signature of the function is:
// int f(int n)
// Examples
//
// --------------------------------|--------------------------------------------------------------
// | if the input integer is | return |
// |--------------------------------|--------------------------------------------------------------|
// | 1234 | 4321 |
// |--------------------------------|--------------------------------------------------------------|
// | 12005 | 50021 |
// |--------------------------------|--------------------------------------------------------------|
// | 1 | 1 |
// |--------------------------------|--------------------------------------------------------------|
// | 1000 | 1 |
// |--------------------------------|--------------------------------------------------------------|
// | 0 | 0 |
// |--------------------------------|--------------------------------------------------------------|
// | -12345 | -54321 |
// --------------------------------|--------------------------------------------------------------
static int reverseInteger(int n) {
if (n == 0) return 0;
int sign = n < 0 ? -1 : 1;
n = n < 0 ? -n : n;
int reverse = 0;
while(n!=0) {
reverse = reverse * 10 + n % 10;
n /= 10;
}
return sign * reverse;
}
static void reverseIntegerTest() {
System.out.println(reverseInteger(1234));
System.out.println(reverseInteger(12005));
System.out.println(reverseInteger(1));
System.out.println(reverseInteger(1000));
System.out.println(reverseInteger(0));
System.out.println(reverseInteger(-12345));
}
public static void main(String[] args) {
reverseIntegerTest();
}
}