-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode7.java
52 lines (38 loc) · 1.12 KB
/
LeetCode7.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
public class LeetCode7 {
public static int[] reverse(int x){
// String s = Integer.toString(x);
// char[] rev = new char[s.length()];
// for(int i = 0; i < s.length(); i++){
// rev[i] = s.charAt(s.length() - 1 -i);
// }
// s = String.valueOf(rev);
// return Integer.parseInt(s);
int[] input = new int[32];
input[31] = 0;
int abs = Math.abs(x);
int currBase = 30, remainder = abs;
for(int i = 30; i >= 0; i--){
if(remainder >= Math.pow(2, i)){
input[i] = 1;
remainder -= Math.pow(2, i);
} else {
input[i] = 0;
}
}
if(x < 0){
for(int i = 0; i < 32; i++){
input[i] = (input[i] == 0) ? 1: 0;
}
int carry = (input[0] == 1) ? 1 : 0;
int input[0] = (carry == 0) ? 1 : 0;
for(int i = 1 ; i < 31; i++){
input[i] = (carry != input[i]) ? 1 : 0;
carry = ((carry == 1) && (input[i] == 1))? 1: 0;
}
}
return input;
}
public static void main(String[] args){
for(int i : reverse(724)) System.out.print(i);
}
}