This file contains 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
let cast = ["Vivien", "Marlon", "Kim", "Karl"] | |
let lowercaseNames = cast.map { $0.lowercased() } | |
// 'lowercaseNames' == ["vivien", "marlon", "kim", "karl"] | |
let letterCounts = cast.map { $0.count } | |
// 'letterCounts' == [6, 6, 3, 4] |
This file contains 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
extension Collection { | |
/// Returns an array containing the results of mapping the given closure | |
/// over the sequence's elements. | |
/// | |
/// In this example, `map` is used first to convert the names in the array | |
/// to lowercase strings and then to count their characters. | |
/// | |
/// let cast = ["Vivien", "Marlon", "Kim", "Karl"] | |
/// let lowercaseNames = cast.map { $0.lowercased() } | |
/// // 'lowercaseNames' == ["vivien", "marlon", "kim", "karl"] |
This file contains 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
extension Sequence { | |
// T is the output type | |
func myMap<T>(_ transform: (Element) -> T) -> [T] { | |
var result: [T] = [] | |
self.forEach { (item) in | |
result.append(transform(item)) | |
} | |
return result |
This file contains 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 Node { | |
var key: Int? | |
var value: Int? | |
var next: Node? | |
var prev: Node? | |
} | |
class DLinkList { | |
var head: Node = Node() | |
var tail: Node = Node() |
This file contains 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 TrieNode { | |
var children: [Character: TrieNode] = [Character: TrieNode]() | |
var isEOW: Bool = false | |
} | |
class Trie { | |
var root: TrieNode? | |
/** Initialize your data structure here. */ | |
init() { | |
root = TrieNode() |
This file contains 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 Codec { | |
func serializeHelper(_ root: TreeNode?,_ output: inout String) { | |
if root == nil { | |
output = output + "null" + " " | |
return | |
} | |
output = output + String(root!.val) + " " | |
serializeHelper(root?.left, &output) | |
serializeHelper(root?.right, &output) |
This file contains 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
/** | |
* Definition for a binary tree node. | |
* public class TreeNode { | |
* public var val: Int | |
* public var left: TreeNode? | |
* public var right: TreeNode? | |
* public init(_ val: Int) { | |
* self.val = val | |
* self.left = nil | |
* self.right = nil |
This file contains 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
/** | |
* Definition for a binary tree node. | |
* public class TreeNode { | |
* public var val: Int | |
* public var left: TreeNode? | |
* public var right: TreeNode? | |
* public init() { self.val = 0; self.left = nil; self.right = nil; } | |
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } | |
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { | |
* self.val = val |
This file contains 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 singleNumber(_ nums: [Int]) -> Int { | |
var result = 0 | |
nums.forEach { (num) in | |
result = result ^ num | |
} | |
return result | |
} | |
} |
This file contains 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
func lengthOfLIS(_ nums: [Int]) -> Int { | |
if nums.isEmpty { return 0 } | |
let len = nums.count | |
var dp = Array(repeating: 0, count: len) | |
dp[0] = 1 | |
var ans = 1 | |
for i in 1..<dp.count { | |
var maxVal = 0 | |
for j in 0..<i { | |
if nums[i] > nums[j] { |