forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddStrings.swift
38 lines (32 loc) · 1.05 KB
/
AddStrings.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
/**
* Question Link: https://leetcode.com/problems/add-strings/
* Primary idea: reverse two strings and add them using sum && carry idea
*
* Note: do not forget to reverse afterwards
*
* Time Complexity: O(n), Space Complexity: O(1)
*
*/
class AddStrings {
func addStrings(_ num1: String, _ num2: String) -> String {
let num1Chars = Array(num1.characters.reversed())
let num2Chars = Array(num2.characters.reversed())
var i = 0, j = 0, sum = 0, carry = 0
var res = ""
while i < num1Chars.count || j < num2Chars.count || carry != 0 {
sum = carry
if i < num1Chars.count {
sum += Int(String(num1Chars[i]))!
i += 1
}
if j < num2Chars.count {
sum += Int(String(num2Chars[j]))!
j += 1
}
carry = sum / 10
sum = sum % 10
res.append(String(sum))
}
return String(res.characters.reversed())
}
}