Skip to content

Instantly share code, notes, and snippets.

@NigelJu
NigelJu / LeetCode 628. Maximum Product of Three Numbers ( swift ).swift
Created July 4, 2018 11:49
LeetCode 628. Maximum Product of Three Numbers ( swift ).swift
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)
@NigelJu
NigelJu / LeetCode 605. Can Place Flowers (swift) Perfect .swift
Created July 3, 2018 14:02
LeetCode 605. Can Place Flowers (swift) Perfect .swift
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
@NigelJu
NigelJu / LeetCode 349. Intersection of Two Arrays Mapping( swift ).swift
Created July 2, 2018 12:08
LeetCode 349. Intersection of Two Arrays Mapping( swift ).swift
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)
}
@NigelJu
NigelJu / LeetCode 349. Intersection of Two Arrays Func( swift ).swift
Created July 2, 2018 12:05
LeetCode 349. Intersection of Two Arrays Func( swift )
class Solution {
func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
let set1 = Set(nums1)
let set2 = Set(nums2)
return Array(set1.intersection(set2))
}
}
@NigelJu
NigelJu / LeetCode 100. Same Tree (swift).swift
Created July 1, 2018 03:49
LeetCode 100. Same Tree (swift).swift
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)
}
}
@NigelJu
NigelJu / LeetCode 168. Excel Sheet Column Title Slow.swift
Created June 30, 2018 09:26
LeetCode 168. Excel Sheet Column Title Slow.swift
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)!)
@NigelJu
NigelJu / LeetCode 168. Excel Sheet Column Title Base.swift
Created June 30, 2018 09:11
LeetCode 168. Excel Sheet Column Title Base.swift
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)!)
@NigelJu
NigelJu / LeetCode 104. Maximum Depth of Binary Tree ( Swift )Fast.swift
Created June 30, 2018 06:20
LeetCode 104. Maximum Depth of Binary Tree ( Swift )Fast.swift
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)
@NigelJu
NigelJu / LeetCode 104. Maximum Depth of Binary Tree ( Swift ).swift
Created June 30, 2018 06:14
LeetCode 104. Maximum Depth of Binary Tree ( Swift ).swift
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)
}
@NigelJu
NigelJu / LeetCode169. Majority Element ( swift )sort.swift
Created June 28, 2018 12:31
LeetCode169. Majority Element ( swift )sort.swift
class Solution {
func majorityElement(_ nums: [Int]) -> Int {
return nums.sorted(by: >)[nums.count / 2]
}
}