Skip to content

Commit 1a9615b

Browse files
Added new solutions and tests
1 parent 9aabad5 commit 1a9615b

File tree

21 files changed

+765
-72
lines changed

21 files changed

+765
-72
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// LeetCode: https://leetcode.com/problems/add-binary/description/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func addBinary(_ a: String, _ b: String) -> String {
7+
var arrA = Array(a.reversed())
8+
var arrB = Array(b.reversed())
9+
var shouldAdd = 0
10+
var output = ""
11+
for idx in 0..<max(arrA.count, arrB.count) {
12+
var valA = 0
13+
var valB = 0
14+
if idx < arrA.count {
15+
valA = Int(String(arrA[idx]))!
16+
}
17+
if idx < arrB.count {
18+
valB = Int(String(arrB[idx]))!
19+
}
20+
if valA + valB + shouldAdd > 1 {
21+
output.append("\(valA + valB + shouldAdd - 2)")
22+
shouldAdd = 1
23+
} else {
24+
output.append("\(valA + valB + shouldAdd)")
25+
shouldAdd = 0
26+
}
27+
}
28+
if shouldAdd == 1 {
29+
output.append("1")
30+
}
31+
output = String(output.reversed())
32+
return output
33+
}
34+
}
35+
36+
class Tests: XCTestCase {
37+
let s = Solution()
38+
39+
func testSample1() {
40+
let input = ("1010", "1011")
41+
let expected = "10101"
42+
let output = s.addBinary(input.0, input.1)
43+
XCTAssertEqual(expected, output)
44+
}
45+
46+
func testSample2() {
47+
let input = ("11", "1")
48+
let expected = "100"
49+
let output = s.addBinary(input.0, input.1)
50+
XCTAssertEqual(expected, output)
51+
}
52+
53+
func testSample3() {
54+
let input = ("0", "0")
55+
let expected = "0"
56+
let output = s.addBinary(input.0, input.1)
57+
XCTAssertEqual(expected, output)
58+
}
59+
60+
func testSample4() {
61+
let input = ("101", "110")
62+
let expected = "1011"
63+
let output = s.addBinary(input.0, input.1)
64+
XCTAssertEqual(expected, output)
65+
}
66+
}
67+
68+
Tests.defaultTestSuite.run()
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='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Medium/0003-LongestSubstringWithoutRepeatingCharacters.playground/Contents.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// LeetCode: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
2+
// Note: Use Set() to solve the problem: time limit exceeded.
3+
4+
import XCTest
25

36
class Solution {
47
func lengthOfLongestSubstring(_ s: String) -> Int {
@@ -27,5 +30,20 @@ class Solution {
2730
}
2831
}
2932

30-
let s = Solution()
31-
print("\(s.lengthOfLongestSubstring("dvdf"))")
33+
class Tests: XCTestCase {
34+
let solution = Solution()
35+
36+
func testSample1() {
37+
let sample = "dvdf"
38+
let expected = 3
39+
XCTAssertEqual(solution.lengthOfLongestSubstring(sample), expected)
40+
}
41+
42+
func testSample2() {
43+
let sample = "abcabcbb"
44+
let expected = 3
45+
XCTAssertEqual(solution.lengthOfLongestSubstring(sample), expected)
46+
}
47+
}
48+
49+
Tests.defaultTestSuite.run()

Medium/0006-ZigZagConversion.playground/Contents.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// LeetCode: https://leetcode.com/problems/zigzag-conversion/description/
22

3+
import XCTest
4+
35
class Solution {
46
func convert(_ s: String, _ numRows: Int) -> String {
57
var output = ""
@@ -24,6 +26,20 @@ class Solution {
2426
}
2527
}
2628

27-
let solution = Solution()
28-
print("\(solution.convert("PAYPALISHIRING", 3))")
29-
print("\(solution.convert("PAYPALISHIRING", 4))")
29+
class Tests: XCTestCase {
30+
let solution = Solution()
31+
32+
func testSample1() {
33+
let input = ("PAYPALISHIRING", 4)
34+
let expected = "PINALSIGYAHRPI"
35+
XCTAssertEqual(solution.convert(input.0, input.1), expected)
36+
}
37+
38+
func testSample2() {
39+
let input = ("PAYPALISHIRING", 3)
40+
let expected = "PAHNAPLSIIGYIR"
41+
XCTAssertEqual(solution.convert(input.0, input.1), expected)
42+
}
43+
}
44+
45+
Tests.defaultTestSuite.run()
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// LeetCode: https://leetcode.com/problems/container-with-most-water/description/
2+
// Try to use O(n) runtime
3+
4+
import XCTest
25

36
class Solution {
47
func maxArea(_ height: [Int]) -> Int {
58
var area = 0
69
var start = 0
710
var end = height.count - 1
811
while start < end {
9-
let areaH = min(height[start], height[end])
10-
let width = end - start
11-
if areaH * width > area {
12-
area = areaH * width
13-
}
12+
area = max(area, min(height[start], height[end]) * (end - start))
1413
if height[start] < height[end] {
1514
start += 1
1615
} else {
@@ -21,5 +20,14 @@ class Solution {
2120
}
2221
}
2322

24-
let solution = Solution()
25-
print("\(solution.maxArea([1,8,6,2,5,4,8,3,7]))") // 49
23+
class Tests: XCTestCase {
24+
let solution = Solution()
25+
26+
func testSample() {
27+
let intput = [1,8,6,2,5,4,8,3,7]
28+
let expected = 49
29+
XCTAssertEqual(solution.maxArea(intput), expected)
30+
}
31+
}
32+
33+
Tests.defaultTestSuite.run()

Medium/0012-IntegerToRoman.playground/Contents.swift

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
1000 : "M"
1111
*/
1212

13+
import XCTest
14+
1315
class Solution {
1416
func intToRoman(_ num: Int) -> String {
1517
var output = ""
@@ -63,9 +65,38 @@ class Solution {
6365
}
6466
}
6567

66-
let solution = Solution()
67-
print("\(solution.intToRoman(3))")
68-
print("\(solution.intToRoman(4))")
69-
print("\(solution.intToRoman(9))")
70-
print("\(solution.intToRoman(58))")
71-
print("\(solution.intToRoman(1994))")
68+
class Tests: XCTestCase {
69+
let solution = Solution()
70+
71+
func sampleTest1() {
72+
let input = 3
73+
let expected = "III"
74+
XCTAssertEqual(solution.intToRoman(input), expected)
75+
}
76+
77+
func sampleTest2() {
78+
let input = 4
79+
let expected = "IV"
80+
XCTAssertEqual(solution.intToRoman(input), expected)
81+
}
82+
83+
func sampleTest3() {
84+
let input = 9
85+
let expected = "IX"
86+
XCTAssertEqual(solution.intToRoman(input), expected)
87+
}
88+
89+
func sampleTest4() {
90+
let input = 58
91+
let expected = "LVIII"
92+
XCTAssertEqual(solution.intToRoman(input), expected)
93+
}
94+
95+
func sampleTest5() {
96+
let input = 1994
97+
let expected = "MCMXCIV"
98+
XCTAssertEqual(solution.intToRoman(input), expected)
99+
}
100+
}
101+
102+
Tests.defaultTestSuite.run()
Lines changed: 91 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// LeetCode: https://leetcode.com/problems/3sum/description/
2-
// Hint: Start from TwoSum, use sorting
2+
// Hint: Start from TwoSum, use sorting, O(n) is allowed in runtime
3+
4+
import XCTest
35

46
class Solution {
57
func threeSum(_ nums: [Int]) -> [[Int]] {
@@ -19,67 +21,102 @@ class Solution {
1921
output.append([sort[i], sort[start], sort[end]])
2022
start += 1
2123
end -= 1
24+
while start < end, start - 1 >= 0, sort[start] == sort[start - 1] {
25+
start += 1
26+
}
27+
while start < end, end + 1 < sort.count, sort[end] == sort[end + 1] {
28+
end -= 1
29+
}
2230
} else if sort[i] + sort[start] + sort[end] < 0 {
2331
start += 1
2432
} else {
2533
end -= 1
2634
}
27-
if start - 1 >= 0 && sort[start] == sort[start - 1] {
28-
start += 1
29-
continue
30-
}
31-
if end + 1 < sort.count && sort[end] == sort[end + 1] {
32-
end -= 1
33-
continue
34-
}
3535
}
3636
}
3737
return output
3838
}
3939
}
4040

41-
let solution = Solution()
42-
print("\(solution.threeSum([-1, 0, 1, 2, -1, -4]))")
43-
print("---------------------------------")
44-
/**
45-
The solution set is:
46-
[
47-
[-1, 0, 1],
48-
[-1, -1, 2]
49-
]
50-
*/
51-
print("\(solution.threeSum([-1,0,1,0]))")
52-
print("---------------------------------")
53-
/**
54-
The solution set is:
55-
[
56-
[-1, 0, 1]
57-
]
58-
*/
59-
print("\(solution.threeSum([3,0,-2,-1,1,2]))")
60-
print("---------------------------------")
61-
/**
62-
The solution set is:
63-
[
64-
[-2, -1, 3],
65-
[-2, 0, 2],
66-
[-1, 0, 1]
67-
]
68-
*/
69-
print("\(solution.threeSum([-2,0,1,1,2]))")
70-
/**
71-
The solution set is:
72-
[
73-
[-2,0,2],
74-
[-2,1,1]
75-
]
76-
*/
77-
print("\(solution.threeSum([1,1,1]))") // [[]]
78-
let longArray1 = [-2,10,-14,11,5,-4,2,0,-10,-10,5,7,-11,10,-2,-5,2,12,-5,14,-11,-15,-5,12,0,13,8,7,10,6,-9,-15,1,14,11,-9,-13,-10,6,-8,-5,-11,6,-9,14,11,-7,-6,8,3,-7,5,-5,3,2,10,-6,-12,3,11,1,1,12,10,-8,0,8,-5,6,-8,-6,8,-12,-14,7,9,12,-15,-12,-2,-4,-4,-12,6,7,-3,-6,-14,-8,4,4,9,-10,-7,-4,-3,1,11,-1,-8,-12,9,7,-9,10,-1,-14,-1,-8,11,12,-5,-7]
79-
print(longArray1.count)
80-
print("\(solution.threeSum(longArray1))") // Test time limit
81-
print("---------------------------------")
82-
let longArray2 = [12,-14,-5,12,-2,9,0,9,-3,-3,-14,-6,-4,13,-11,-8,0,5,-7,-6,-10,-13,-7,-14,-3,0,12,5,-8,7,3,-11,0,6,9,13,-8,-6,7,4,6,0,13,-13,-1,9,-13,6,-1,-13,-15,-4,-11,-15,-11,-7,1,-14,13,8,0,2,4,-15,-15,-2,5,-8,7,-11,11,-10,4,1,-15,10,-5,-13,2,1,11,-6,4,-15,-5,8,-7,3,1,-9,-4,-14,0,-15,8,0,-1,-2,7,13,2,-5,11,13,11,11]
83-
print(longArray2.count)
84-
print("\(solution.threeSum(longArray2))") // Test time limit
85-
print("---------------------------------")
41+
class Tests: XCTestCase {
42+
let s = Solution()
43+
44+
func testSample1() {
45+
let input = [-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6]
46+
let expected = [
47+
[-4,-2,6],
48+
[-4,0,4],
49+
[-4,1,3],
50+
[-4,2,2],
51+
[-2,-2,4],
52+
[-2,0,2]
53+
]
54+
let output = s.threeSum(input)
55+
XCTAssertEqual(output.count, expected.count)
56+
for o in output {
57+
XCTAssertTrue(expected.contains(o))
58+
}
59+
}
60+
61+
func testSample2() {
62+
let input = [0,0,0]
63+
let expected = [[0,0,0]]
64+
let output = s.threeSum(input)
65+
XCTAssertEqual(output.count, expected.count)
66+
for o in output {
67+
XCTAssertTrue(expected.contains(o))
68+
}
69+
}
70+
71+
func testSample3() {
72+
let input = [-2,0,1,1,2]
73+
let expected = [
74+
[-2,0,2],
75+
[-2,1,1]
76+
]
77+
let output = s.threeSum(input)
78+
XCTAssertEqual(output.count, expected.count)
79+
for o in output {
80+
XCTAssertTrue(expected.contains(o))
81+
}
82+
}
83+
84+
func testSample4() {
85+
let input = [3,0,-2,-1,1,2]
86+
let expected = [
87+
[-2, -1, 3],
88+
[-2, 0, 2],
89+
[-1, 0, 1]
90+
]
91+
let output = s.threeSum(input)
92+
XCTAssertEqual(output.count, expected.count)
93+
for o in output {
94+
XCTAssertTrue(expected.contains(o))
95+
}
96+
}
97+
98+
func testSample5() {
99+
let input = [-1,0,1,0]
100+
let expected = [[-1, 0, 1]]
101+
let output = s.threeSum(input)
102+
XCTAssertEqual(output.count, expected.count)
103+
for o in output {
104+
XCTAssertTrue(expected.contains(o))
105+
}
106+
}
107+
108+
func testSample6() {
109+
let input = [-1, 0, 1, 2, -1, -4]
110+
let expected = [
111+
[-1, 0, 1],
112+
[-1, -1, 2]
113+
]
114+
let output = s.threeSum(input)
115+
XCTAssertEqual(output.count, expected.count)
116+
for o in output {
117+
XCTAssertTrue(expected.contains(o))
118+
}
119+
}
120+
}
121+
122+
Tests.defaultTestSuite.run()

0 commit comments

Comments
 (0)