This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func maximumProduct(_ nums: [Int]) -> Int { | |
| if nums.count == 3 { | |
| return nums.reduce(1, *) | |
| } | |
| let sortedNums = nums.sorted(by: >) | |
| let allMaxNum = sortedNums[0] * sortedNums[1] * sortedNums[2] | |
| let minMixedMaxNum = sortedNums.first! * sortedNums[nums.count - 2] * sortedNums.last! | |
| return max(allMaxNum, minMixedMaxNum) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func canPlaceFlowers(_ flowerbed: [Int], _ n: Int) -> Bool { | |
| if n == 0 { return true } | |
| if flowerbed.count == 1 { | |
| return n == 1 && flowerbed.first! == 0 | |
| } | |
| var flowerCount = n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] { | |
| var mapping = [Int: Int]() | |
| var results = [Int]() | |
| nums1.forEach( { mapping.updateValue(0, forKey: $0) } ) | |
| for n2 in nums2 { | |
| if let _ = mapping[n2] { | |
| mapping.removeValue(forKey: n2) | |
| results.append(n2) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] { | |
| let set1 = Set(nums1) | |
| let set2 = Set(nums2) | |
| return Array(set1.intersection(set2)) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool { | |
| if p == nil && q == nil { return true } | |
| guard let tree1 = p, let tree2 = q, tree1.val == tree2.val else { return false } | |
| return isSameTree(tree1.left, tree2.left) && isSameTree(tree1.right, tree2.right) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func convertToTitle(_ n: Int) -> String { | |
| var num = n | |
| var intToStr = [Int: String]() | |
| var result = "" | |
| intToStr.updateValue("Z", forKey: 0) | |
| for index in 1 ... 25 { | |
| let char = String(UnicodeScalar(64 + index)!) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func convertToTitle(_ n: Int) -> String { | |
| var num = n | |
| var intToStr = [Int: String]() | |
| var result = "" | |
| intToStr.updateValue("Z", forKey: 0) | |
| for index in 1 ... 25 { | |
| let char = String(UnicodeScalar(64 + index)!) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func maxDepth(_ root: TreeNode?) -> Int { | |
| guard let root = root else { return 0 } | |
| var maxLeftDepth = 1 | |
| var maxRightDepth = 1 | |
| maxLeftDepth += maxDepth(root.left) | |
| maxRightDepth += maxDepth(root.right) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func maxDepth(_ root: TreeNode?) -> Int { | |
| guard let root = root else { return 0 } | |
| var maxLeftDepth = 1 | |
| var maxRightDepth = 1 | |
| if let leftNood = root.left { | |
| maxLeftDepth += maxDepth(leftNood) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func majorityElement(_ nums: [Int]) -> Int { | |
| return nums.sorted(by: >)[nums.count / 2] | |
| } | |
| } |