LeetCode 476. Number Complement in F#

URL

https://leetcode.com/problems/number-complement/description/?envType=daily-question&envId=2024-08-22

Code

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

let findComplement (num: int) : int =
    let rec findComplement' num v acc =
        if num = 0 then
            acc
        else
            let bit = if num % 2 = 0 then 1 else 0
            findComplement' (num / 2) (v * 2) (v * bit + acc)

    findComplement' num 1 0