Skip to content

Commit b66da1b

Browse files
Added accepted solutions
1 parent eb84fb8 commit b66da1b

File tree

14 files changed

+585
-0
lines changed

14 files changed

+585
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// LeetCode: https://leetcode.com/problems/pascals-triangle/description/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func generate(_ numRows: Int) -> [[Int]] {
7+
var output: [[Int]] = []
8+
if numRows < 1 {
9+
return output
10+
}
11+
output.append([1])
12+
if numRows < 2 {
13+
return output
14+
}
15+
output.append([1,1])
16+
if numRows < 3 {
17+
return output
18+
}
19+
for i in 2..<numRows {
20+
var arr = Array(repeating: 1, count: i+1)
21+
for j in 1..<arr.count-1 {
22+
arr[j] = output[i-1][j-1] + output[i-1][j]
23+
}
24+
25+
output.append(arr)
26+
}
27+
return output
28+
}
29+
30+
func getRow(_ rowIndex: Int) -> [Int] {
31+
var output: [Int] = [1]
32+
guard rowIndex > 0 else {
33+
return output
34+
}
35+
for _ in 1...rowIndex {
36+
var temp = output
37+
for j in 0..<output.count {
38+
if j-1 >= 0 {
39+
output[j] = temp[j] + temp[j-1]
40+
}
41+
}
42+
output.append(1)
43+
}
44+
return output
45+
}
46+
}
47+
48+
class Tests: XCTestCase {
49+
let s = Solution()
50+
51+
func testSample1() {
52+
let input = 5
53+
let expected = [
54+
[1],
55+
[1,1],
56+
[1,2,1],
57+
[1,3,3,1],
58+
[1,4,6,4,1]
59+
]
60+
XCTAssertEqual(s.generate(input), expected)
61+
}
62+
63+
func testSample2() {
64+
let input = 0
65+
let expected: [[Int]] = []
66+
XCTAssertEqual(s.generate(input), expected)
67+
}
68+
69+
func testSample3() {
70+
let input = 5
71+
let expected = [1,5,10,10,5,1]
72+
XCTAssertEqual(s.getRow(input), expected)
73+
}
74+
75+
func testSample4() {
76+
let input = 3
77+
let expected = [1,3,3,1]
78+
XCTAssertEqual(s.getRow(input), expected)
79+
}
80+
81+
func testSample5() {
82+
let input = 1
83+
let expected = [1,1]
84+
XCTAssertEqual(s.getRow(input), expected)
85+
}
86+
}
87+
88+
Tests.defaultTestSuite.run()
89+
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' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// LeetCode: https://leetcode.com/problems/basic-calculator/description/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func calculate(_ s: String) -> Int {
7+
var output: [Int] = []
8+
let strArr = Array(s + "+")
9+
var open: [[Int]] = []
10+
var num = 0
11+
var sign: Character = "+"
12+
13+
for ch in strArr {
14+
if ch >= "0" && ch <= "9" {
15+
num = num*10 + Int(String(ch))!
16+
} else if ch == "+" || ch == "-" {
17+
if open.count > 0 {
18+
open[0].append(sign == "+" ? num : -num)
19+
} else {
20+
output.append(sign == "+" ? num : -num)
21+
}
22+
num = 0
23+
sign = ch
24+
} else if ch == "(" {
25+
var newSign = sign == "+" ? 1 : -1
26+
if open.count > 0 {
27+
newSign *= open[0][0]
28+
}
29+
var arr: [Int] = [newSign]
30+
open.insert(arr, at: 0)
31+
sign = "+"
32+
} else if ch == ")" {
33+
if num != 0 {
34+
open[0].append(sign == "+" ? num : -num)
35+
num = 0
36+
}
37+
let sign = open[0].removeFirst()
38+
output.append(sign * open[0].reduce(0, {x,y in
39+
x+y
40+
}))
41+
open.removeFirst()
42+
}
43+
}
44+
return output.reduce(0, {x,y in
45+
x+y
46+
})
47+
}
48+
}
49+
50+
class Tests: XCTestCase {
51+
let s = Solution()
52+
53+
func testSample1() {
54+
let input = "1 + 1"
55+
let expected = 2
56+
XCTAssertEqual(s.calculate(input), expected)
57+
}
58+
59+
func testSample2() {
60+
let input = "(3-(2-(5-(9-(4)))))"
61+
let expected = 1
62+
XCTAssertEqual(s.calculate(input), expected)
63+
}
64+
65+
func testSample3() {
66+
let input = "(1+(4+5+2)-3)+(6+8)"
67+
let expected = 23
68+
XCTAssertEqual(s.calculate(input), expected)
69+
}
70+
71+
func testSample4() {
72+
let input = "1-(5)"
73+
let expected = -4
74+
XCTAssertEqual(s.calculate(input), expected)
75+
}
76+
77+
func testSample5() {
78+
let input = "(5-(1+(5)))"
79+
let expected = -1
80+
XCTAssertEqual(s.calculate(input), expected)
81+
}
82+
}
83+
84+
Tests.defaultTestSuite.run()
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' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// LeetCode: https://leetcode.com/problems/permutations-ii/description/
2+
// Mind time complexity
3+
4+
import XCTest
5+
6+
class Solution {
7+
func permuteUnique(_ nums: [Int]) -> [[Int]] {
8+
var output: [[Int]] = []
9+
let current: [Int] = []
10+
let sorted = nums.sorted()
11+
permuteUnique(sorted, current, &output)
12+
return output
13+
}
14+
15+
private func permuteUnique(_ nums: [Int], _ current: [Int], _ output: inout [[Int]]) {
16+
if nums.count == 0, !output.contains(current) {
17+
output.append(current)
18+
return
19+
}
20+
var idx = 0
21+
while idx < nums.count {
22+
if idx - 1 >= 0, nums[idx] == nums[idx-1] {
23+
idx += 1
24+
continue
25+
}
26+
var temp = current
27+
temp.append(nums[idx])
28+
var tempNum = nums
29+
tempNum.remove(at: idx)
30+
permuteUnique(tempNum, temp, &output)
31+
idx += 1
32+
}
33+
}
34+
}
35+
36+
class Tests: XCTestCase {
37+
let s = Solution()
38+
39+
func testSample1() {
40+
let input = [1,1,2]
41+
let expected = [
42+
[1,1,2],
43+
[1,2,1],
44+
[2,1,1]
45+
]
46+
let output = s.permuteUnique(input)
47+
XCTAssertEqual(output.count, expected.count)
48+
for o in output {
49+
XCTAssertTrue(expected.contains(o))
50+
}
51+
}
52+
}
53+
54+
Tests.defaultTestSuite.run()
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' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// LeetCode: https://leetcode.com/problems/permutation-sequence/description/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func getPermutation(_ n: Int, _ k: Int) -> String {
7+
guard n > 0, k > 0 else { return "" }
8+
if n-1 < 1 {
9+
return "\(n)"
10+
}
11+
var arr: [Int] = []
12+
for i in 1...n {
13+
arr.append(i)
14+
}
15+
16+
var factList: [Int] = []
17+
var current = 1
18+
for i in 1...n {
19+
current = current * i
20+
factList.append(current)
21+
}
22+
23+
var output = ""
24+
for i in 1...n-1 {
25+
var idx = Int((Double((k % factList[n-i])*arr.count)/Double(factList[n-i])).rounded(.up))-1
26+
if idx < 0 {
27+
idx += arr.count
28+
}
29+
output.append("\(arr.remove(at: idx))")
30+
}
31+
output.append("\(arr.removeLast())")
32+
return output
33+
}
34+
}
35+
36+
class Tests: XCTestCase {
37+
let s = Solution()
38+
39+
func testSample1() {
40+
let input = (3, 3)
41+
let expected = "213"
42+
XCTAssertEqual(s.getPermutation(input.0, input.1), expected)
43+
}
44+
45+
func testSample2() {
46+
let input = (4, 9)
47+
let expected = "2314"
48+
XCTAssertEqual(s.getPermutation(input.0, input.1), expected)
49+
}
50+
51+
func testSample3() {
52+
let input = (3, 2)
53+
let expected = "132"
54+
XCTAssertEqual(s.getPermutation(input.0, input.1), expected)
55+
}
56+
57+
func testSample4() {
58+
let input = (1, 1)
59+
let expected = "1"
60+
XCTAssertEqual(s.getPermutation(input.0, input.1), expected)
61+
}
62+
}
63+
64+
Tests.defaultTestSuite.run()
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' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// LeetCode:
2+
3+
import XCTest
4+
5+
class Solution {
6+
func searchMatrix(_ matrix: [[Int]], _ target: Int) -> Bool {
7+
guard !matrix.isEmpty else { return false }
8+
if matrix.count < 2 {
9+
return matrix[0].contains(target)
10+
}
11+
var i = matrix.count/2
12+
var j = 0
13+
var upper: Int?
14+
while i < matrix.count, j < matrix[i].count {
15+
if target == matrix[i][j] {
16+
return true
17+
} else if target < matrix[i][j] {
18+
guard i > 0 else { return false }
19+
upper = i
20+
i = i/2
21+
} else {
22+
if j + 1 >= matrix[i].count, (upper == nil || i+1 < upper!) {
23+
i += 1
24+
j = 0
25+
} else {
26+
j += 1
27+
}
28+
}
29+
}
30+
return false
31+
}
32+
}
33+
34+
class Tests: XCTestCase {
35+
let s = Solution()
36+
37+
func testSample1() {
38+
let input = ([
39+
[1, 3, 5, 7],
40+
[10, 11, 16, 20],
41+
[23, 30, 34, 50]
42+
], 3)
43+
let expected = true
44+
XCTAssertEqual(s.searchMatrix(input.0, input.1), expected)
45+
}
46+
47+
func testSample2() {
48+
let input = ([
49+
[1, 3, 5, 7],
50+
[10, 11, 16, 20],
51+
[23, 30, 34, 50]
52+
], 13)
53+
let expected = false
54+
XCTAssertEqual(s.searchMatrix(input.0, input.1), expected)
55+
}
56+
57+
func testSample3() {
58+
let input = ([[1], [3]], 0)
59+
let expected = false
60+
XCTAssertEqual(s.searchMatrix(input.0, input.1), expected)
61+
}
62+
63+
func testSample4() {
64+
let input = ([[1], [3], [5]], 5)
65+
let expected = true
66+
XCTAssertEqual(s.searchMatrix(input.0, input.1), expected)
67+
}
68+
}
69+
70+
Tests.defaultTestSuite.run()
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' display-mode='raw' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)