Fetch the repository succeeded.
/**
* Question Link: https://leetcode.com/problems/add-binary/
* Primary idea: use Carry and iterate from last to start
*
* Note: Swift does not have a way to access a character in a string with O(1),
* thus we have to first transfer the string to a character array
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class AddBinary {
func addBinary(_ a: String, _ b: String) -> String {
var sum = 0, carry = 0, res = ""
let aChars = Array(a.characters), bChars = Array(b.characters)
var i = aChars.count - 1, j = bChars.count - 1
while i >= 0 || j >= 0 || carry > 0 {
sum = carry
if i >= 0 {
sum += Int(String(aChars[i]))!
i -= 1
}
if j >= 0 {
sum += Int(String(bChars[j]))!
j -= 1
}
carry = sum / 2
sum = sum % 2
res = String(sum) + res
}
return res
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。