hashmap - Lookup table for counting number of set bits in an Integer -
was trying solve popular interview question - http://www.careercup.com/question?id=3406682
there 2 approaches able grasp -
brian kernighan's algo - bits counting algorithm (brian kernighan) in integer time complexity
lookup table.
i assume when people use lookup table, mean hashmap integer key, , count of number of set bits value.
how 1 construct lookup table? use brian's algo to count number of bits first time encounter integer, put in hashtable, , next time encounter integer, retrieve value hashtable?
ps: aware of hardware , software api's available perform popcount (integer.bitcount()), in context of interview question, not allowed use methods.
integers can directly used index arrays; e.g. have simple array of unsigned 8bit integers containing set-bit-count 0x0001, 0x0002, 0x0003... , array[number_to_test]
.
you don't need implement hash function map 16 bit integer can order can have function!
Comments
Post a Comment