swift solution


  • 0
    S
    class Solution {
        func findComplement(_ num: Int) -> Int {
            var compNo = ""
            var complement = 0
            let bitNo = String(num, radix:2)
            for i in 0..<bitNo.characters.count {
            let index = bitNo.index(bitNo.startIndex, offsetBy: i)
                //print(bitNo[index])
                if bitNo[index] == "0" {
                    compNo += "1"
                }else {
                    compNo += "0"
                }
        }
            complement = Int(compNo, radix:2)!
            let answer = complement
            print(answer)
            return answer
    
    }
    }
    
    Solution().findComplement(5)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.