Count Set Bits with a Byte Lookup Table
Problem statement
Given an array of signed 32-bit integers values, return the total number of one bits across their 32-bit two's-complement representations.
Precompute the set-bit count for every byte value from 0 through 255, then use four table lookups per integer.
Function
countSetBits(values: int[]) → longExamples
Example 1
values = [0,1,255,256]return = 10The values contain 0, 1, 8, and 1 set bits respectively.
Example 2
values = [-1]return = 32Signed -1 has all 32 bits set in two's-complement representation.
Constraints
0 <= values.length <= 1000000- Every value is a signed 32-bit integer.