Skip to content

Commit d035363

Browse files
authored
Added tasks 189-191.
1 parent f374117 commit d035363

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0101_0200.s0189_rotate_array;
2+
3+
public class Solution {
4+
public void reverse(int[] nums, int l, int r) {
5+
while (l <= r) {
6+
int temp = nums[l];
7+
nums[l] = nums[r];
8+
nums[r] = temp;
9+
l++;
10+
r--;
11+
}
12+
}
13+
14+
public void rotate(int[] nums, int k) {
15+
int n = nums.length;
16+
int t = n - (k % n);
17+
reverse(nums, 0, t - 1);
18+
reverse(nums, t, n - 1);
19+
reverse(nums, 0, n - 1);
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0101_0200.s0190_reverse_bits;
2+
3+
public class Solution {
4+
// you need treat n as an unsigned value
5+
public int reverseBits(int n) {
6+
int ret = 0;
7+
// because there are 32 bits in total
8+
for (int i = 0; i < 32; i++) {
9+
ret = ret << 1;
10+
// If the bit is 1 we OR it with 1, ie add 1
11+
if ((n & 1) > 0) {
12+
ret = ret | 1;
13+
}
14+
n = n >>> 1;
15+
}
16+
return ret;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0101_0200.s0191_number_of_1_bits;
2+
3+
public class Solution {
4+
public int hammingWeight(int n) {
5+
int sum = 0;
6+
boolean flag = false;
7+
if (n < 0) {
8+
flag = true;
9+
n = n - Integer.MIN_VALUE;
10+
}
11+
while (n > 0) {
12+
int k = n % 2;
13+
sum += k;
14+
n /= 2;
15+
}
16+
return flag ? sum + 1 : sum;
17+
}
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0101_0200.s0189_rotate_array;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void rotate() {
11+
int[] array = new int[] {1, 2, 3, 4, 5, 6, 7};
12+
new Solution().rotate(array, 3);
13+
assertThat(array, equalTo(new int[] {5, 6, 7, 1, 2, 3, 4}));
14+
}
15+
16+
@Test
17+
public void rotate2() {
18+
int[] array = new int[] {-1, -100, 3, 99};
19+
new Solution().rotate(array, 2);
20+
assertThat(array, equalTo(new int[] {3, 99, -1, -100}));
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0101_0200.s0190_reverse_bits;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void reverseBits() {
11+
assertThat(
12+
new Solution().reverseBits(0b00000010100101000001111010011100),
13+
equalTo(0b00111001011110000010100101000000));
14+
}
15+
16+
@Test
17+
public void reverseBits2() {
18+
assertThat(
19+
new Solution().reverseBits(0b11111111111111111111111111111101),
20+
equalTo(0b10111111111111111111111111111111));
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0101_0200.s0191_number_of_1_bits;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void hammingWeight() {
11+
assertThat(new Solution().hammingWeight(0b00000000000000000000000000001011), equalTo(3));
12+
}
13+
14+
@Test
15+
public void hammingWeight2() {
16+
assertThat(new Solution().hammingWeight(0b00000000000000000000000010000000), equalTo(1));
17+
}
18+
}

0 commit comments

Comments
 (0)