Skip to content

Commit 82902ff

Browse files
committed
Add string.
1 parent 57916d1 commit 82902ff

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string.
3+
4+
You must solve the problem without using any built-in library for handling large integers (such as BigInteger). You must also not convert the inputs to integers directly.
5+
6+
 
7+
8+
Example 1:
9+
Input: num1 = "11", num2 = "123"
10+
Output: "134"
11+
12+
Example 2:
13+
Input: num1 = "456", num2 = "77"
14+
Output: "533"
15+
16+
Example 3:
17+
Input: num1 = "0", num2 = "0"
18+
Output: "0"
19+
 
20+
21+
Constraints:
22+
- 1 <= num1.length, num2.length <= 104
23+
- num1 and num2 consist of only digits.
24+
- num1 and num2 don't have any leading zeros except for the zero itself.
25+
*/
26+
class Solution {
27+
func addStrings(_ num1: String, _ num2: String) -> String {
28+
let sample: [Character: Int] = ["0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9]
29+
var num1Arr: [Character] = num1.map { $0 }.reversed()
30+
var num2Arr: [Character] = num2.map { $0 }.reversed()
31+
var carryCount = 0
32+
var result = ""
33+
if num1Arr.count < num2Arr.count {
34+
let temp = num1Arr
35+
num1Arr = num2Arr
36+
num2Arr = temp
37+
}
38+
39+
for (i, c) in num1Arr.enumerated() {
40+
let n1 = sample[c]!
41+
var number = 0
42+
if i <= num2Arr.count - 1 {
43+
let n2 = sample[num2Arr[i]]!
44+
number = n1 + n2
45+
} else {
46+
number = n1
47+
}
48+
if i > 0 && i == carryCount {
49+
number += 1
50+
}
51+
if number >= 10 {
52+
result.append("\(number)".last!)
53+
carryCount = i + 1
54+
} else {
55+
result.append("\(number)")
56+
}
57+
}
58+
if carryCount == num1Arr.count {
59+
result.append("1")
60+
}
61+
62+
let arr: [String] = result.map { String($0) }.reversed()
63+
return arr.joined()
64+
}
65+
}
66+
67+
let s = Solution()
68+
let r = s.addStrings("52", "60")
69+
print(r)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/415.Add Strings.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
82. [Longest Palindrome](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/409.Longest%20Palindrome.playground/Contents.swift)
8787
83. [Fizz Buzz](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/409.Fizz%20Buzz.playground/Contents.swift)
8888
84. [Third Maximum Number](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/414.Third%20Maximum%20Number.playground/Contents.swift)
89+
85. [Add String](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/415.Add%20String.playground/Contents.swift)
8990

9091
#### Medium
9192

0 commit comments

Comments
 (0)