diff --git a/3sum/delight010.swift b/3sum/delight010.swift new file mode 100644 index 000000000..ad3128bb4 --- /dev/null +++ b/3sum/delight010.swift @@ -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) + } +} + diff --git a/climbing-stairs/delight010.swift b/climbing-stairs/delight010.swift new file mode 100644 index 000000000..c27fa8c2e --- /dev/null +++ b/climbing-stairs/delight010.swift @@ -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 + } +} + diff --git a/product-of-array-except-self/delight010.swift b/product-of-array-except-self/delight010.swift new file mode 100644 index 000000000..4e71d3bd1 --- /dev/null +++ b/product-of-array-except-self/delight010.swift @@ -0,0 +1,17 @@ +class Solution { + func productExceptSelf(_ nums: [Int]) -> [Int] { + var answer: [Int] = Array(repeating: 1, count: nums.endIndex) + for i in 1.. 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 + } +} + diff --git a/validate-binary-search-tree/delight010.swift b/validate-binary-search-tree/delight010.swift new file mode 100644 index 000000000..6a199cc13 --- /dev/null +++ b/validate-binary-search-tree/delight010.swift @@ -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) + } +} +