decimal sum then binary?


  • 0
    N

    not sure if it's right.
    pseudocode:

    
    func addBinary(a: String, b: String) -> String {
        var sum = Int(a) + Int(b)
        var target = 2
        
        var i = 0
        var next = 0
        var res = String()
        
        while sum / 10 > 0 || sum > 0 {
            let digit = sum % 10
            let cur: String = String(digit % target)
            res.insert(cur, atIndex: 0)
            next = digit / target
            
            sum = sum / 10 + next
            i = i + 1
        }
        
        return String(res)
    }
    
    

Log in to reply
 

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