From 10c5a7c73a9d9fbde04f63271a98810d8823aa76 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 15 Nov 2021 13:00:10 +0200 Subject: [PATCH 1/3] Added task 189. --- .../s0189_rotate_array/Solution.java | 21 ++++++++++++++++++ .../s0189_rotate_array/SolutionTest.java | 22 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/main/java/g0101_0200/s0189_rotate_array/Solution.java create mode 100644 src/test/java/g0101_0200/s0189_rotate_array/SolutionTest.java diff --git a/src/main/java/g0101_0200/s0189_rotate_array/Solution.java b/src/main/java/g0101_0200/s0189_rotate_array/Solution.java new file mode 100644 index 000000000..84ac051db --- /dev/null +++ b/src/main/java/g0101_0200/s0189_rotate_array/Solution.java @@ -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); + } +} diff --git a/src/test/java/g0101_0200/s0189_rotate_array/SolutionTest.java b/src/test/java/g0101_0200/s0189_rotate_array/SolutionTest.java new file mode 100644 index 000000000..05992cfb9 --- /dev/null +++ b/src/test/java/g0101_0200/s0189_rotate_array/SolutionTest.java @@ -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})); + } +} From a56fc3808dd81ba8ac919b92907200b3a2a6a554 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 15 Nov 2021 13:06:31 +0200 Subject: [PATCH 2/3] Added task 190. --- .../s0190_reverse_bits/Solution.java | 18 +++++++++++++++ .../s0190_reverse_bits/SolutionTest.java | 22 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/main/java/g0101_0200/s0190_reverse_bits/Solution.java create mode 100644 src/test/java/g0101_0200/s0190_reverse_bits/SolutionTest.java diff --git a/src/main/java/g0101_0200/s0190_reverse_bits/Solution.java b/src/main/java/g0101_0200/s0190_reverse_bits/Solution.java new file mode 100644 index 000000000..e452378a1 --- /dev/null +++ b/src/main/java/g0101_0200/s0190_reverse_bits/Solution.java @@ -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; + } +} diff --git a/src/test/java/g0101_0200/s0190_reverse_bits/SolutionTest.java b/src/test/java/g0101_0200/s0190_reverse_bits/SolutionTest.java new file mode 100644 index 000000000..19d8890fa --- /dev/null +++ b/src/test/java/g0101_0200/s0190_reverse_bits/SolutionTest.java @@ -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)); + } +} From 0df7578ea4600243a55c8b0d7e7a0729a740531d Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 15 Nov 2021 13:11:53 +0200 Subject: [PATCH 3/3] Added task 191. --- .../s0191_number_of_1_bits/Solution.java | 18 ++++++++++++++++++ .../s0191_number_of_1_bits/SolutionTest.java | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/main/java/g0101_0200/s0191_number_of_1_bits/Solution.java create mode 100644 src/test/java/g0101_0200/s0191_number_of_1_bits/SolutionTest.java diff --git a/src/main/java/g0101_0200/s0191_number_of_1_bits/Solution.java b/src/main/java/g0101_0200/s0191_number_of_1_bits/Solution.java new file mode 100644 index 000000000..255571440 --- /dev/null +++ b/src/main/java/g0101_0200/s0191_number_of_1_bits/Solution.java @@ -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; + } +} diff --git a/src/test/java/g0101_0200/s0191_number_of_1_bits/SolutionTest.java b/src/test/java/g0101_0200/s0191_number_of_1_bits/SolutionTest.java new file mode 100644 index 000000000..03517bae0 --- /dev/null +++ b/src/test/java/g0101_0200/s0191_number_of_1_bits/SolutionTest.java @@ -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)); + } +}