Hyoseo Lee
01 Index 02 Current page Writing 03 Work 04 Notes 05 Games 06 Author
leetcode

191. Number of 1 Bits

Topic Divide and Conquer
Area Algorithms
Summary
from moving on each bits, count the number of ones.

Problem

View on LeetCode →

Difficulty: Easy
Tags: Divide and Conquer, Bit Manipulation

Intuition

this one seems easy and it was. Horray to BIT MANIPULATION!!!

Approach

from moving on each bits, count the number of ones.

Solution

int hammingWeight(int n) {
    int count = 0;
    while(n){
        if(n&1 == 1){
            count++;
        }
        n = n>>1;
    }
    return count;
}

Complexity

  • Time: O(1)O(1)

  • Space: O(1)O(1)

Thoughts

getting more and more familiar with BIT MANIPULATION!!!