Skip to content

Latest commit

 

History

History
24 lines (20 loc) · 469 Bytes

File metadata and controls

24 lines (20 loc) · 469 Bytes

LeetCode Records - Question 191 Number of 1 Bits

Attempt 1: Count the last bit

class Solution {
    public int hammingWeight(int n) {
        int count = 0;

        for (int i = 0; i < 32; i++) {
            int val = n & 0x1;
            if (val != 0) {
                count++;
            }
            n >>= 1;
        }

        return count;
    }
}
  • Runtime: 0 ms (Beats: 100.00%)
  • Memory: 40.91 MB (Beats: 11.20%)