-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution.go
More file actions
48 lines (42 loc) · 703 Bytes
/
solution.go
File metadata and controls
48 lines (42 loc) · 703 Bytes
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
package add_binary
func addBinary(a string, b string) string {
additional := 0
aIndex := len(a) - 1
bIndex := len(b) - 1
result := ""
for aIndex >= 0 || bIndex >= 0 {
aSub := 0
bSub := 0
if aIndex >= 0 {
if a[aIndex] == '1' {
aSub = 1
}
}
if bIndex >= 0 {
if b[bIndex] == '1' {
bSub = 1
}
}
subResult := additional + aSub + bSub
if subResult > 2 {
additional = 1
subResult = 1
} else if subResult > 1 {
additional = 1
subResult = 0
} else {
additional = 0
}
if subResult == 0 {
result = "0" + result
} else {
result = "1" + result
}
aIndex--
bIndex--
}
if additional > 0 {
result = "1" + result
}
return result
}