-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-binary.go
71 lines (62 loc) · 1.08 KB
/
add-binary.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
)
// source: https://leetcode.com/problems/add-binary/
func addBinary(a string, b string) string {
var na, nb int
if na, nb = len(a), len(b); na < nb {
a, b = b, a
na, nb = nb, na
}
var res = make([]rune, na+1)
var p = na
var overflow bool
var next int
for i := range a {
next = 0
if a[na-1-i] == '1' {
next++
}
if nb-1-i >= 0 && b[nb-1-i] == '1' {
next++
}
if overflow {
next++
}
switch next {
case 0:
res[p] = '0'
overflow = false
case 1:
res[p] = '1'
overflow = false
case 2:
res[p] = '0'
overflow = true
case 3:
res[p] = '1'
overflow = true
}
p--
}
if overflow {
res[p] = '1'
} else {
res = res[1:]
}
return string(res)
}
func main() {
var a string = "0"
var b string = "0"
fmt.Println("Expected: \"100\" Output: ", addBinary(a, b))
// Example 1
var a1 string = "11"
var b1 string = "1"
fmt.Println("Expected: \"100\" Output: ", addBinary(a1, b1))
// Example 2
var a2 string = "1010"
var b2 string = "1011"
fmt.Println("Expected: \"10101\" Output: ", addBinary(a2, b2))
}