Skip to content

Commit b45a7dd

Browse files
author
Michael Ho
committed
Update solutions from 1 to 50
1 parent bff5ebf commit b45a7dd

File tree

41 files changed

+1131
-282
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1131
-282
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// LeetCode: https://leetcode.com/problems/wildcard-matching/
2+
// Difficulty: hard
3+
4+
import XCTest
5+
6+
// TODO: solve the problem, this solution exceeds the time limit.
7+
class Solution {
8+
func isMatch(_ s: String, _ p: String) -> Bool {
9+
// Handle edge cases
10+
if s.count == 0 && p.count == 0 {
11+
return true
12+
} else if s.count == 0 {
13+
ifContainsAsterishOnly(p)
14+
} else if p.count == 0 {
15+
return false
16+
}
17+
18+
var withStar = false
19+
var idxP = 0
20+
var idxS = 0
21+
let sArr = Array(s)
22+
let pArr = Array(p)
23+
24+
while idxP < p.count, idxS < s.count {
25+
if sArr[idxS] == pArr[idxP] || pArr[idxP] == "?" {
26+
idxS += 1
27+
idxP += 1
28+
} else if pArr[idxP] == "*" {
29+
while idxP + 1 < p.count, pArr[idxP+1] == "*" {
30+
idxP += 1
31+
}
32+
if idxP+1 >= p.count {
33+
return true
34+
}
35+
for i in idxS..<sArr.count {
36+
if pArr[idxP+1] == sArr[i] || pArr[idxP+1] == "?" {
37+
if isMatch(String(sArr[i..<s.count]), String(pArr[idxP + 1..<p.count])) {
38+
return true
39+
}
40+
}
41+
}
42+
return false
43+
} else {
44+
return false
45+
}
46+
}
47+
48+
if idxP == p.count && idxS == s.count {
49+
return true
50+
} else if idxP < p.count {
51+
return ifContainsAsterishOnly(String(pArr[idxP..<p.count]))
52+
} else {
53+
return false
54+
}
55+
}
56+
57+
private func ifContainsAsterishOnly(_ str: String) -> Bool {
58+
for ch in str {
59+
if ch != "*" {
60+
return false
61+
}
62+
}
63+
return true
64+
}
65+
}
66+
67+
class Tests: XCTestCase {
68+
let s = Solution()
69+
70+
func testSampl1() {
71+
let input = ("aa", "a")
72+
XCTAssertFalse(s.isMatch(input.0, input.1))
73+
}
74+
75+
func testSampl2() {
76+
let input = ("aa", "*")
77+
XCTAssertTrue(s.isMatch(input.0, input.1))
78+
}
79+
80+
func testSampl3() {
81+
let input = ("cb", "?a")
82+
XCTAssertFalse(s.isMatch(input.0, input.1))
83+
}
84+
85+
func testSampl4() {
86+
let input = ("adceb", "*a*b")
87+
XCTAssertTrue(s.isMatch(input.0, input.1))
88+
}
89+
90+
func testSampl5() {
91+
let input = ("abefcdgiescdfimde", "ab*cd?i*de")
92+
XCTAssertTrue(s.isMatch(input.0, input.1))
93+
}
94+
95+
func testSampl6() {
96+
let input = ("aaaa", "***a")
97+
XCTAssertTrue(s.isMatch(input.0, input.1))
98+
}
99+
100+
func testSampl7() {
101+
let input = ("", "*")
102+
XCTAssertTrue(s.isMatch(input.0, input.1))
103+
}
104+
105+
func testSampl8() {
106+
let input = ("a", "a*")
107+
XCTAssertTrue(s.isMatch(input.0, input.1))
108+
}
109+
110+
func testSampl9() {
111+
let input = ("c", "*?*")
112+
XCTAssertTrue(s.isMatch(input.0, input.1))
113+
}
114+
}
115+
116+
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>

Problems/1-100/Medium/0040-CombinationSumII.playground/Contents.swift

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,29 @@ import XCTest
44

55
class Solution {
66
func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] {
7-
var output: [[Int]] = []
8-
let current: [Int] = []
97
let sorted = candidates.sorted()
10-
helper(sorted, target, current, &output)
8+
var output = [[Int]]()
9+
let current = [Int]()
10+
helper(sorted, 0, current, target, &output)
1111
return output
1212
}
1313

14-
func helper(_ candidates: [Int], _ target: Int, _ current: [Int], _ output: inout [[Int]]) {
15-
if target <= 0 {
16-
if target == 0 {
17-
output.append(current)
18-
}
14+
private func helper(_ candidates: [Int], _ startIdx: Int, _ current: [Int], _ target: Int, _ output: inout [[Int]]) {
15+
if target == 0 {
16+
output.append(current)
1917
return
2018
}
2119

22-
if candidates.count == 0 {
20+
if target <= 0 || startIdx >= candidates.count {
2321
return
2422
}
25-
26-
var prev: Int?
27-
for (index, num) in candidates.enumerated() {
28-
if num > target {
29-
continue
30-
}
31-
32-
if let prev = prev, prev == num {
23+
for i in startIdx..<candidates.count {
24+
if i > startIdx, i-1 >= 0, candidates[i-1] == candidates[i] {
3325
continue
3426
}
35-
var temp = Array(candidates[index..<candidates.count])
36-
var new = current
37-
new.append(temp.remove(at: 0))
38-
helper(temp, target - num, new, &output)
39-
prev = num
27+
var temp = current
28+
temp.append(candidates[i])
29+
helper(candidates, i+1, temp, target-candidates[i], &output)
4030
}
4131
}
4232
}
@@ -67,7 +57,7 @@ class Tests: XCTestCase {
6757
[5]
6858
]
6959
let output = s.combinationSum2(input.0, input.1)
70-
60+
7161
XCTAssertEqual(output.count, expected.count)
7262
for arr in output {
7363
XCTAssertTrue(expected.contains(arr))

Problems/1-100/Medium/0056-MergeIntervals.playground/Contents.swift

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,84 +2,90 @@
22

33
import XCTest
44

5-
public class Interval {
6-
public var start: Int
7-
public var end: Int
8-
9-
public init(_ start: Int, _ end: Int) {
10-
self.start = start
11-
self.end = end
12-
}
13-
14-
public init(_ array: [Int]) {
15-
self.start = array[0]
16-
self.end = array[1]
17-
}
18-
19-
public func toArray() -> [Int] {
20-
return [self.start, self.end]
21-
}
22-
}
23-
245
class Solution {
25-
func merge(_ intervals: [Interval]) -> [Interval] {
26-
var output: [Interval] = []
27-
var sorted = intervals.sorted(by: { $0.start < $1.start })
6+
func merge(_ intervals: [[Int]]) -> [[Int]] {
7+
guard intervals.count > 0 else { return [] }
8+
var output: [[Int]] = []
9+
var sorted = mergeSort(intervals, 0, intervals.count-1)
2810
var i = 0
2911
while i < sorted.count {
3012
var j = i + 1
31-
while j < sorted.count, sorted[i].end >= sorted[j].start {
32-
sorted[i].end = max(sorted[j].end, sorted[i].end)
13+
while j < sorted.count, sorted[i][1] >= sorted[j][0] {
14+
sorted[i][1] = max(sorted[j][1], sorted[i][1])
3315
j += 1
3416
}
3517
output.append(sorted[i])
3618
i = j
3719
}
3820
return output
3921
}
22+
23+
private func mergeSort(_ intervals: [[Int]], _ start: Int, _ end: Int) -> [[Int]] {
24+
guard end - start > 0 else { return [intervals[start]] }
25+
26+
let mid = start + (end-start)/2
27+
let leftArr = mergeSort(intervals, start, mid)
28+
let rightArr = mergeSort(intervals, mid+1, end)
29+
30+
var output = [[Int]]()
31+
var leftStart = 0
32+
var rightStart = 0
33+
while leftStart < leftArr.count || rightStart < rightArr.count {
34+
if leftStart < leftArr.count, rightStart < rightArr.count {
35+
if leftArr[leftStart][0] < rightArr[rightStart][0] {
36+
output.append(leftArr[leftStart])
37+
leftStart += 1
38+
} else {
39+
output.append(rightArr[rightStart])
40+
rightStart += 1
41+
}
42+
} else if leftStart < leftArr.count {
43+
for i in leftStart..<leftArr.count {
44+
output.append(leftArr[i])
45+
}
46+
leftStart = leftArr.count
47+
} else {
48+
for i in rightStart..<rightArr.count {
49+
output.append(rightArr[i])
50+
}
51+
rightStart = rightArr.count
52+
}
53+
}
54+
return output
55+
}
4056
}
4157

4258
class Tests: XCTestCase {
4359
let s = Solution()
4460

4561
func testSample1() {
46-
let input = [Interval(1,3), Interval(2,6), Interval(8,10), Interval(15,18)]
62+
let input = [[1,3], [2,6], [8,10], [15,18]]
4763
let expected = [[1,6],[8,10],[15,18]]
48-
var output: [[Int]] = []
49-
for i in s.merge(input) {
50-
output.append(i.toArray())
51-
}
52-
XCTAssertEqual(output, expected)
64+
XCTAssertEqual(expected, s.merge(input))
5365
}
5466

5567
func testSample2() {
56-
let input = [Interval(1,4), Interval(4,5)]
68+
let input = [[1,4], [4,5]]
5769
let expected = [[1,5]]
58-
var output: [[Int]] = []
59-
for i in s.merge(input) {
60-
output.append(i.toArray())
61-
}
62-
XCTAssertEqual(output, expected)
70+
XCTAssertEqual(expected, s.merge(input))
6371
}
6472

6573
func testSample3() {
66-
let input = [Interval(1,4), Interval(2,3)]
74+
let input = [[1,4], [2,3]]
6775
let expected = [[1,4]]
68-
var output: [[Int]] = []
69-
for i in s.merge(input) {
70-
output.append(i.toArray())
71-
}
72-
XCTAssertEqual(output, expected)
76+
XCTAssertEqual(expected, s.merge(input))
7377
}
7478

7579
func testSample4() {
76-
let input = [Interval(1,4), Interval(0,2), Interval(3,5)]
80+
let input = [[1,4], [0,2], [3,5]]
7781
let expected = [[0,5]]
78-
var output: [[Int]] = []
79-
for i in s.merge(input) {
80-
output.append(i.toArray())
81-
}
82-
XCTAssertEqual(output, expected)
82+
XCTAssertEqual(expected, s.merge(input))
83+
}
84+
85+
func testSample5() {
86+
let input = [[Int]]()
87+
let expected = [[Int]]()
88+
XCTAssertEqual(expected, s.merge(input))
8389
}
8490
}
8591

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
2+
<playground version='5.0' target-platform='macos'>
33
<timeline fileName='timeline.xctimeline'/>
44
</playground>

Problems/1-100/Medium/0060-PermutationSequence.playground/Contents.swift

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,24 @@ import XCTest
55
class Solution {
66
func getPermutation(_ n: Int, _ k: Int) -> String {
77
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
8+
var ref = [Int]()
9+
var count = 1
1810
for i in 1...n {
19-
current = current * i
20-
factList.append(current)
11+
ref.append(i)
12+
count = count * i
2113
}
2214

2315
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))")
16+
var newN = n
17+
var newK = k
18+
while ref.count > 0, count > 0 {
19+
20+
let idx = Int((Double(ref.count*newK)/Double(count)).rounded(.up)) - 1
21+
output += "\(ref.remove(at: idx))"
22+
count = count/newN
23+
newK = newK - idx*count
24+
newN -= 1
3025
}
31-
output.append("\(arr.removeLast())")
3226
return output
3327
}
3428
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'>
2+
<playground version='5.0' target-platform='ios'>
33
<timeline fileName='timeline.xctimeline'/>
44
</playground>

0 commit comments

Comments
 (0)