-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0067_add_binary.go
50 lines (44 loc) · 944 Bytes
/
0067_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
// https://leetcode.com/problems/add-binary
//
// 0067. Add Binary [Easy]
//
// Given two binary strings a and b, return their sum as a binary string.
//
// Constraints:
//
// 1 <= a.length, b.length <= 104
// a and b consist only of '0' or '1' characters.
// Each string does not contain leading zeros except for the zero itself.
package leetcode
func AddBinary(a, b string) string {
l1, l2 := len(a), len(b)
l := max(l1+1, l2+1)
b1, b2 := binary(a, l), binary(b, l)
carry := byte(0)
sum := make([]rune, l)
for i := l - 1; i >= 0; i-- {
d := b1[i] + b2[i] + carry
carry = d / 2
sum[i] = rune(d%2) + '0'
}
if sum[0] == '0' {
return string(sum[1:])
}
return string(sum)
}
func max(n1, n2 int) int {
if n1 > n2 {
return n1
}
return n2
}
func binary(s string, size int) []byte {
diff := size - len(s)
b := make([]byte, size)
for i := len(s) - 1; i >= 0; i-- {
if s[i] == '1' {
b[i+diff] = 1
}
}
return b
}