forked from diwu/LeetCode-Solutions-in-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Easy_067_Add_Binary.swift
66 lines (57 loc) · 1.35 KB
/
Easy_067_Add_Binary.swift
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
/*
https://leetcode.com/problems/add-binary/
#67 Add Binary
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
Inspired by @makuiyu at https://leetcode.com/discuss/25593/short-code-by-c
*/
import Foundation
private extension String {
subscript (index: Int) -> Character {
return self[self.index(self.startIndex, offsetBy: index)]
}
}
struct Easy_067_Add_Binary {
static func addBinary(a: String, b: String) -> String {
var s = ""
var c: Int = 0
var i = a.count - 1
var j = b.count - 1
let characterDict: [Character: Int] = [
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
]
let intDict: [Int: String] = [
0: "0",
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
]
while i >= 0 || j >= 0 || c == 1 {
c += i >= 0 ? characterDict[a[i]]! : 0
i -= 1
c += j >= 0 ? characterDict[b[j]]! : 0
j -= 1
s = intDict[c%2]! + s
c /= 2
}
return s
}
}