Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9006a52

Browse files
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‎

Lines changed: 24 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /