Simple but no ideal Swift Solution


  • 0
    S
        func findComplement(_ num: Int) -> Int {
      
            var bits = String(num, radix: 2)
      
            let complement: [String] = bits.characters.map {
            if $0 == "0" {
              return "1"
            } else {
              return "0"
            }
            } 
        
            return Int(complement.joined(), radix: 2) ?? 0
        }
    

Log in to reply
 

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