-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
44 lines (30 loc) · 939 Bytes
/
Contents.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
import Foundation
/**
67. Add Binary
Tags: Math、String
https://leetcode.com/problems/add-binary/description/
*/
/**
Math实现, 24ms. 时间复杂度(a.count+b.count), 空间复杂度O(n)
*/
class Solution {
func addBinary(_ a: String, _ b: String) -> String {
let aChars = Array(a)
let bChars = Array(b)
var aIndex = aChars.count - 1
var bIndex = bChars.count - 1
var result = ""
var addOne = false
while aIndex >= 0 || bIndex >= 0 || addOne {
let aValue = aIndex >= 0 ? Int(String(aChars[aIndex]))! : 0
let bValue = bIndex >= 0 ? Int(String(bChars[bIndex]))! : 0
let sum = aValue + bValue + (addOne ? 1 : 0)
addOne = sum > 1
result = "\(sum % 2)" + result
aIndex -= 1
bIndex -= 1
}
return result
}
}
Solution().addBinary("1010", "1011")