-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path067.go
49 lines (45 loc) · 756 Bytes
/
067.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package p067
import (
"bytes"
)
/**
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
*/
func addBinary(a string, b string) string {
carry := byte('0')
la := len(a) - 1
lb := len(b) - 1
ans := bytes.Buffer{}
for la >= 0 || lb >= 0 || carry > '0' {
ca := byte('0')
if la >= 0 {
ca = a[la]
la--
}
cb := byte('0')
if lb >= 0 {
cb = b[lb]
lb--
}
s := (ca + cb + carry) - (3 * '0')
if s == 1 || s == 0 {
ans.WriteByte('0' + s)
carry = '0'
} else if s == 2 || s == 3 {
ans.WriteByte('0' + s - 2)
carry = '1'
}
}
rans := ans.Bytes()
i, j := 0, len(rans)-1
for i < j {
rans[i], rans[j] = rans[j], rans[i]
i++
j--
}
return string(rans)
}