LeetCode 338. Counting Bits in F#

URL

Counting Bits - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202309/counting_bits/main.fsx

let countBits (n: int) : int list =
    let hammingWeight n =
        let m = n
        let m = (m &&& 0x55555555) + ((m >>> 1) &&& 0x55555555)
        let m = (m &&& 0x33333333) + ((m >>> 2) &&& 0x33333333)
        let m = (m &&& 0x0F0F0F0F) + ((m >>> 4) &&& 0x0F0F0F0F)
        let m = (m &&& 0x00FF00FF) + ((m >>> 8) &&& 0x00FF00FF)
        (m &&& 0x0000FFFF) + ((m >>> 16) &&& 0x0000FFFF)

    let rec countBits' n acc =
        if n < 0 then
            acc
        else
            let bits = hammingWeight n
            countBits' (n - 1) (bits :: acc)

    countBits' n []