Skip to content

Commit a9db161

Browse files
authored
Merge pull request #1269 from doitduri/main
[doitduri] WEEK 02 Solutions
2 parents 1c8fa40 + 5a0ffe1 commit a9db161

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

3sum/doitduri.swift

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Foundation
2+
3+
class Solution {
4+
func threeSum(_ nums: [Int]) -> [[Int]] {
5+
var result = [[Int]]()
6+
let sortedNums = nums.sorted()
7+
8+
for i in 0..<sortedNums.count - 2 {
9+
if i > 0 && sortedNums[i] == sortedNums[i - 1] {
10+
continue
11+
}
12+
13+
var leftNumber = i + 1
14+
var rightNumber = sortedNums.count - 1
15+
16+
while leftNumber < rightNumber {
17+
let sum = sortedNums[i] + sortedNums[leftNumber] + sortedNums[rightNumber]
18+
19+
if sum < 0 {
20+
leftNumber += 1
21+
} else if sum > 0 {
22+
rightNumber -= 1
23+
} else {
24+
result.append([sortedNums[i], sortedNums[leftNumber], sortedNums[rightNumber]])
25+
26+
while leftNumber < rightNumber && sortedNums[leftNumber] == sortedNums[leftNumber + 1] {
27+
leftNumber += 1
28+
}
29+
30+
while leftNumber < rightNumber && sortedNums[rightNumber] == sortedNums[rightNumber - 1] {
31+
rightNumber -= 1
32+
}
33+
34+
leftNumber += 1
35+
rightNumber -= 1
36+
}
37+
}
38+
}
39+
40+
return result
41+
}
42+
}
43+

climbing-stairs/doitduri.swift

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
func climbStairs(_ n: Int) -> Int {
3+
var tables = [0, 1, 2]
4+
for i in 3...45 {
5+
let result = tables[i-1] + tables[i-2]
6+
tables.append(result)
7+
}
8+
9+
return tables[n]
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
func productExceptSelf(_ nums: [Int]) -> [Int] {
3+
let length = nums.count
4+
var result = Array(repeating: 1, count: length)
5+
var leftProduct = 1
6+
for i in 0..<length {
7+
result[i] = leftProduct
8+
leftProduct *= nums[i]
9+
}
10+
11+
var rightProduct = 1
12+
for i in (0..<length).reversed() {
13+
result[i] *= rightProduct
14+
rightProduct *= nums[i]
15+
}
16+
17+
return result
18+
}
19+
}

valid-anagram/doitduri.swift

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
func isAnagram(_ s: String, _ t: String) -> Bool {
3+
guard s.count == t.count else {
4+
return false
5+
}
6+
7+
return s.sorted() == t.sorted()
8+
}
9+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
3+
func isValidBST(_ root: TreeNode?) -> Bool {
4+
return isValid(root, nil, nil)
5+
}
6+
7+
private func isValid(_ node: TreeNode?, _ min: Int?, _ max: Int?) -> Bool {
8+
guard let node = node else { return true }
9+
10+
if let min = min, node.val <= min {
11+
return false
12+
}
13+
14+
if let max = max, node.val >= max {
15+
return false
16+
}
17+
return isValid(node.left, min, node.val) && isValid(node.right, node.val, max)
18+
}
19+
}
20+

0 commit comments

Comments
 (0)