Skip to content

Commit 9006a52

Browse files
authored
Merge pull request doocs#230 from ElectricBubble/master
Add Solution.go for 0067.Add Binary
2 parents 92cf51a + 86058fa commit 9006a52

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

solution/0067.Add Binary/Solution.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func addBinary(a string, b string) string {
2+
for len(a) > len(b) {
3+
b = "0" + b
4+
}
5+
for len(a) < len(b) {
6+
a = "0" + a
7+
}
8+
zero := []byte("0")[0]
9+
ret := make([]byte, len(a))
10+
for right := len(a) - 1; right > 0; right-- {
11+
t := ret[right] + a[right] + b[right] - zero*2
12+
ret[right] = t%2 + zero
13+
if t >= 2 {
14+
ret[right-1] = 1
15+
}
16+
}
17+
t := ret[0] + a[0] + b[0] - zero*2
18+
ret[0] = t%2 + zero
19+
if t >= 2 {
20+
ret = append([]byte("1"), ret...)
21+
}
22+
23+
return string(ret)
24+
}

0 commit comments

Comments
 (0)