Skip to content

Added tasks 189-191. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/g0101_0200/s0189_rotate_array/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g0101_0200.s0189_rotate_array;

public class Solution {
public void reverse(int[] nums, int l, int r) {
while (l <= r) {
int temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l++;
r--;
}
}

public void rotate(int[] nums, int k) {
int n = nums.length;
int t = n - (k % n);
reverse(nums, 0, t - 1);
reverse(nums, t, n - 1);
reverse(nums, 0, n - 1);
}
}
18 changes: 18 additions & 0 deletions src/main/java/g0101_0200/s0190_reverse_bits/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0101_0200.s0190_reverse_bits;

public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int ret = 0;
// because there are 32 bits in total
for (int i = 0; i < 32; i++) {
ret = ret << 1;
// If the bit is 1 we OR it with 1, ie add 1
if ((n & 1) > 0) {
ret = ret | 1;
}
n = n >>> 1;
}
return ret;
}
}
18 changes: 18 additions & 0 deletions src/main/java/g0101_0200/s0191_number_of_1_bits/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0101_0200.s0191_number_of_1_bits;

public class Solution {
public int hammingWeight(int n) {
int sum = 0;
boolean flag = false;
if (n < 0) {
flag = true;
n = n - Integer.MIN_VALUE;
}
while (n > 0) {
int k = n % 2;
sum += k;
n /= 2;
}
return flag ? sum + 1 : sum;
}
}
22 changes: 22 additions & 0 deletions src/test/java/g0101_0200/s0189_rotate_array/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g0101_0200.s0189_rotate_array;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class SolutionTest {
@Test
public void rotate() {
int[] array = new int[] {1, 2, 3, 4, 5, 6, 7};
new Solution().rotate(array, 3);
assertThat(array, equalTo(new int[] {5, 6, 7, 1, 2, 3, 4}));
}

@Test
public void rotate2() {
int[] array = new int[] {-1, -100, 3, 99};
new Solution().rotate(array, 2);
assertThat(array, equalTo(new int[] {3, 99, -1, -100}));
}
}
22 changes: 22 additions & 0 deletions src/test/java/g0101_0200/s0190_reverse_bits/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g0101_0200.s0190_reverse_bits;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class SolutionTest {
@Test
public void reverseBits() {
assertThat(
new Solution().reverseBits(0b00000010100101000001111010011100),
equalTo(0b00111001011110000010100101000000));
}

@Test
public void reverseBits2() {
assertThat(
new Solution().reverseBits(0b11111111111111111111111111111101),
equalTo(0b10111111111111111111111111111111));
}
}
18 changes: 18 additions & 0 deletions src/test/java/g0101_0200/s0191_number_of_1_bits/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0101_0200.s0191_number_of_1_bits;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class SolutionTest {
@Test
public void hammingWeight() {
assertThat(new Solution().hammingWeight(0b00000000000000000000000000001011), equalTo(3));
}

@Test
public void hammingWeight2() {
assertThat(new Solution().hammingWeight(0b00000000000000000000000010000000), equalTo(1));
}
}