-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution67.go
74 lines (70 loc) · 1.14 KB
/
solution67.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
72
73
74
package solution67
import (
"fmt"
"strconv"
)
// ============================================================================
// 67. Add Binary
// URL: https://leetcode.com/problems/add-binary/
// ============================================================================
func addBinary_stdlib(a string, b string) string {
va, err := strconv.ParseInt(a, 2, 64)
if err != nil {
panic(err)
}
vb, err := strconv.ParseInt(b, 2, 64)
if err != nil {
panic(err)
}
sum := va + vb
s := fmt.Sprintf("%b", sum)
return s
}
func addBinary_my(a string, b string) string {
sa := len(a)
sb := len(b)
m := sa
if sb > sa {
m = sb
}
s := make([]byte, m+1)
var va int
var vb int
var carry int
var i int = 1
var sum int
for {
va = 0
vb = 0
if i <= sa {
va = int(a[sa-i]) - 48
}
if i <= sb {
vb = int(b[sb-i]) - 48
}
sum = va + vb + carry
switch {
case sum == 0:
s[m] = '0'
carry = 0
case sum == 1:
s[m] = '1'
carry = 0
case sum == 2:
s[m] = '0'
carry = 1
case sum == 3:
s[m] = '1'
carry = 1
}
i++
if i > sa+1 && i > sb+1 {
break
}
m--
}
if s[0] == '0' {
s = s[1:]
}
return string(s)
}