-
Notifications
You must be signed in to change notification settings - Fork 0
Add solution for the "Number of 1 Bits" problem with unit tests. #46
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Tags: Bit Manipulation, Divide and Conquer | ||
| package com.leetcode.easy; | ||
|
|
||
| public class NumberOf1Bits191 { | ||
| /** | ||
| * Time complexity: O(1) | ||
| * Space complexity: O(1) | ||
| */ | ||
| public int hammingWeight(int n) { | ||
| int res = 0; | ||
| while (n != 0) { | ||
| if (n % 2 == 1) { | ||
| res++; | ||
| } | ||
| n = n >> 1; | ||
| } | ||
| return res; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.leetcode.easy; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| public class NumberOf1Bits191Test { | ||
| private NumberOf1Bits191 solution; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| solution = new NumberOf1Bits191(); | ||
| } | ||
|
|
||
| @Test | ||
| void testHammingWeight_Example1() { | ||
| int n = 11; // Binary representation: 1011 | ||
| int expected = 3; | ||
| int result = solution.hammingWeight(n); | ||
| assertEquals(expected, result); | ||
|
Comment on lines
+16
to
+21
|
||
| } | ||
|
|
||
| @Test | ||
| void testHammingWeight_Example2() { | ||
| int n = 128; // Binary representation: 10000000 | ||
| int expected = 1; | ||
| int result = solution.hammingWeight(n); | ||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testHammingWeight_Example3() { | ||
| int n = 2147483645; // Binary representation: 1111111111111111111111111111101 | ||
| int expected = 30; | ||
| int result = solution.hammingWeight(n); | ||
| assertEquals(expected, result); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hammingWeightusesn % 2 == 1and an arithmetic right shift (n >> 1). For negative inputs (which occur when interpreting the LeetCode "unsigned 32-bit" value as a Javaint),n % 2is negative andn >> 1preserves the sign bit, sonmay never reach 0 (e.g.,n = -1loops forever) and the bit count is incorrect. Use bit operations that work for all 32 bits, e.g. check(n & 1) != 0and shift withn >>>= 1, or use the commonn &= (n - 1)loop.