Saturday, June 27, 2015

Count 1 in Binary

Count 1 in Binary

53%
Accepted
Count how many 1 in binary representation of a 32-bit integer.
Have you met this question in a real interview? 
Yes
Example
Given 32, return 1
Given 5, return 2
Given 1023, return 9
Challenge
If the integer is n bits with m 1 bits. Can you do it in O(m) time?

public class Solution {
    /**
     * @param num: an integer
     * @return: an integer, the number of ones in num
     */
    public int countOnes(int num) {
        // write your code here
        int count = 0;
        for(; num > 0; num &= num-1){
            count++;
        }
        return count;
    }
};

No comments:

Post a Comment