Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1a52955

Browse files
refactor 190
1 parent f77b9bb commit 1a52955

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

src/main/java/com/fishercoder/solutions/_190.java

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
* 190. Reverse Bits
55
* Reverse bits of a given 32 bits unsigned integer.
66
7-
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100),
8-
return 964176192 (represented in binary as 00111001011110000010100101000000).
7+
Example:
8+
Input: 43261596
9+
Output: 964176192
910
10-
Follow up:
11-
If this function is called many times, how would you optimize it?
11+
Explanation: 43261596 represented in binary as 00000010100101000001111010011100,
12+
return 964176192 represented in binary as 00111001011110000010100101000000.
13+
14+
Follow up:
15+
If this function is called many times, how would you optimize it?
1216
*/
1317

1418
public class _190 {
@@ -23,47 +27,24 @@ public class _190 {
2327
* gives a good explanation between logical right shift: ">>>" and arithmetic right shift: ">>".
2428
* Basically, ">>" preserves the most left bit and treats it as the sign for this number,
2529
* e.g. -2 represented in 8 bits is 11111110, thus -2 >> 1 will become 11111111, i.e. -1
26-
* notice its sign bit (the most left one bit) is preserved
27-
* However, logical right shift ">>>" doesn't care about the first bit on the most left,
30+
* notice its sign bit (the most left one bit) is preserved
31+
* However, logical right shift ">>>" doesn't care about the first bit on the most left,
2832
* it simply shifts every bit to the right.
2933
* e.g. -2 >>> 1 would become 1111111111111111111111111111111, i.e. 2147483647*/
30-
31-
32-
// you need treat n as an unsigned value
33-
public int reverseBits(int n) {
34+
35+
public static class Solution1 {
36+
// you need treat n as an unsigned value
37+
public int reverseBits(int n) {
3438
int res = 0;
3539
for (int i = 0; i < 32; i++) {
36-
res += n & 1;//get the most right bit each time
37-
n >>>= 1;//do UN-signed right shift by 1 each time
38-
if (i < 31) {
39-
res <<= 1;//shift this number to the left by 1 each time, so that eventually, this number is reversed
40-
}
40+
res += n & 1;//get the most right bit each time
41+
n >>>= 1;//do UN-signed right shift by 1 each time
42+
if (i < 31) {
43+
res <<=
44+
1;//shift this number to the left by 1 each time, so that eventually, this number is reversed
45+
}
4146
}
4247
return res;
43-
}
44-
45-
/**follow-up: if this function is called many times, how to improve it?
46-
Divide the integer into 4 bytes,
47-
reverse each byte and then combine them into one in the end,
48-
use cache to store the reversed results for reuse if possible.*/
49-
50-
public static void main(String... strings) {
51-
_190 test = new _190();
52-
// int n = 43261596;
53-
int n = 4;
54-
System.out.println("original number : " + n);
55-
System.out.println("original number in binary format: " + Integer.toBinaryString(n));
56-
int result = test.reverseBits(n);
57-
System.out.println("reversed bit result: " + result);
58-
System.out.println("reversed bit result in binary format: " + Integer.toBinaryString(result));
59-
60-
// System.out.println(Integer.toBinaryString(4));
61-
// System.out.println(Integer.parseInt("11000", 2));
62-
// System.out.println(Integer.parseInt("00011", 2));
63-
// System.out.println(-2 >>> 1);
64-
// System.out.println(Integer.toBinaryString(-2 >>> 1));
65-
// System.out.println(Integer.toBinaryString(-2));
66-
// System.out.println(Integer.toBinaryString(-1));
67-
// System.out.println(Integer.toBinaryString(6));
48+
}
6849
}
6950
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._190;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _190Test {
10+
private static _190.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _190.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(536870912, solution1.reverseBits(4));
20+
}
21+
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(964176192, solution1.reverseBits( 43261596));
26+
}
27+
}

0 commit comments

Comments
 (0)