Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions 3sum/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
func threeSum(_ nums: [Int]) -> [[Int]] {
let nums = nums.sorted()
var answer: Set<[Int]> = []
for (index, num) in nums.enumerated() {
var leftIndex = index + 1
var rightIndex = nums.endIndex - 1
while leftIndex < rightIndex {
let sum = num + nums[leftIndex] + nums[rightIndex]
if sum == 0 {
answer.insert([num, nums[leftIndex], nums[rightIndex]])
leftIndex += 1
rightIndex -= 1
} else if sum < 0 {
leftIndex += 1
} else if sum > 0 {
rightIndex -= 1
}
}
}

return Array(answer)
}
}

17 changes: 17 additions & 0 deletions climbing-stairs/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
func climbStairs(_ n: Int) -> Int {
var dictionary: [Int: Int] = [:]
for i in 1...n {
if i < 3 {
dictionary[i] = i
} else {
if let value1 = dictionary[i - 1],
let value2 = dictionary[i - 2] {
dictionary[i] = value1 + value2
}
}
}
return dictionary[n] ?? 0
}
}

17 changes: 17 additions & 0 deletions product-of-array-except-self/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
func productExceptSelf(_ nums: [Int]) -> [Int] {
var answer: [Int] = Array(repeating: 1, count: nums.endIndex)
for i in 1..<nums.endIndex {
answer[i] = answer[i - 1] * nums[i - 1]
}

var suffix = 1
for i in (0..<nums.endIndex).reversed() {
answer[i] *= suffix
suffix *= nums[i]
}

return answer
}
}

14 changes: 14 additions & 0 deletions valid-anagram/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
func isAnagram(_ s: String, _ t: String) -> Bool {
var sDictionary: [Character: Int] = [:]
var tDictionary: [Character: Int] = [:]
for char in s {
sDictionary[char] = (sDictionary[char] ?? 0) + 1
}
for char in t {
tDictionary[char] = (tDictionary[char] ?? 0) + 1
}
return sDictionary == tDictionary
}
}

19 changes: 19 additions & 0 deletions validate-binary-search-tree/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
private var prev: Int?

func isValidBST(_ root: TreeNode?) -> Bool {
inorder(root)
}

private func inorder(_ root: TreeNode?) -> Bool {
guard let root = root else { return true }

guard inorder(root.left) else { return false }

if let prev = prev, root.val <= prev { return false }
prev = root.val

return inorder(root.right)
}
}