-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathadd_binary.go
59 lines (51 loc) · 1019 Bytes
/
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
/*
67. Add Binary
https://leetcode.com/problems/add-binary/
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.
Example:
Input: a = "11", b = "1"
Output: "100"
*/
// time: 2018-12-19
package addbinary
import (
"strconv"
)
// Time complexity: O( max( len(a), len(b) ) )
// Space complexity: O(1)
func addBinary(a string, b string) string {
var (
lenA = len(a)
lenB = len(b)
carry int
res = ""
)
for lenA > 0 && lenB > 0 {
tmp := int(a[lenA-1]-'0') + int(b[lenB-1]-'0') + carry
res = strconv.Itoa(tmp%2) + res
carry = tmp / 2
lenA--
lenB--
}
if lenA == 0 {
for lenB > 0 {
tmp := int(b[lenB-1]-'0') + carry
res = strconv.Itoa(tmp%2) + res
carry = tmp / 2
lenB--
}
}
if lenB == 0 {
for lenA > 0 {
tmp := int(a[lenA-1]-'0') + carry
res = strconv.Itoa(tmp%2) + res
carry = tmp / 2
lenA--
}
}
if carry == 1 {
res = strconv.Itoa(carry) + res
}
return res
}