Skip to content

Commit

Permalink
ad sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Nov 22, 2023
1 parent 9b9b598 commit fa11b36
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions leetcode/67.AddBinary/addBinary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"fmt"
)

func addBinary(a string, b string) string {
result := ""

// 2 pointers
i := len(a) - 1
j := len(b) - 1

var carry byte = 0

for i >= 0 || j >= 0 || carry == 1 {
if i >= 0 {
carry += a[i] - '0'
}
if j >= 0 {
carry += b[j] - '0'
}

result = string(carry%2+'0') + result
carry = carry / 2
i--
j--
}

return result
}

func addBinary1(a string, b string) string {
i, j := len(a)-1, len(b)-1
result := []byte{}

for carry := 0; i >= 0 || j >= 0 || carry > 0; i, j = i-1, j-1 {
if i >= 0 {
carry += int(a[i] - '0')
}
if j >= 0 {
carry += int(b[j] - '0')
}
result = append(result, byte(carry%2+'0'))
carry /= 2
}

for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
result[i], result[j] = result[j], result[i]
}

return string(result)
}

func main() {
a := "11"
b := "1"

fmt.Println(addBinary(a, b))
}

0 comments on commit fa11b36

Please sign in to comment.