forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-binary.go
More file actions
executable file
·52 lines (42 loc) · 835 Bytes
/
add-binary.go
File metadata and controls
executable file
·52 lines (42 loc) · 835 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
49
50
51
52
package problem0067
func addBinary(a string, b string) string {
if len(a) < len(b) {
a, b = b, a
}
l := len(a)
isA := trans(a, l)
isB := trans(b, l)
return makeString(add(isA, isB))
}
func trans(s string, l int) []int {
res := make([]int, l)
ls := len(s)
for i, b := range s {
res[l-ls+i] = int(b - '0')
}
return res
}
func add(a, b []int) []int {
l := len(a) + 1
res := make([]int, l)
for i := l - 1; i >= 1; i-- {
temp := res[i] + a[i-1] + b[i-1]
res[i] = temp % 2
res[i-1] = temp / 2
}
i := 0
// i < l-1 而不是 < l 的原因是
// "" + "" == "0"
// 需要保留最后一个 '0'
for i < l-1 && res[i] == 0 {
i++
}
return res[i:]
}
func makeString(nums []int) string {
bytes := make([]byte, len(nums))
for i := range bytes {
bytes[i] = byte(nums[i]) + '0'
}
return string(bytes)
}