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 RandomizedSet { | |
| private var data: Set<Int> = [] | |
| /** | |
| Inserts a value to the set. | |
| Returns true if the set did not already | |
| contain the specified element. | |
| */ | |
| func insert(_ val: Int) -> Bool { |
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
| func isPowerOfTwo(_ n: Int) -> Bool { | |
| // 1 | |
| guard n != 1 && n != 2 else { | |
| return true | |
| } | |
| // 2 | |
| var last: Int = 2 |
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
| func levelOrder(_ root: TreeNode?) -> [[Int]] { | |
| // 1 | |
| guard let root = root else { | |
| return [] | |
| } | |
| // 2 | |
| var queue = Queue() | |
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
| struct Queue { | |
| typealias NodeAtLevel = (node: TreeNode, level: Int) | |
| private var enqueueStack: [NodeAtLevel] = [] | |
| private var dequeueStack: [NodeAtLevel] = [] | |
| public var isEmpty: Bool { | |
| return enqueueStack.isEmpty && dequeueStack.isEmpty | |
| } |
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
| func reverseString(_ s: inout [Character]) { | |
| s = s.reversed() | |
| } |
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
| func reverseString(_ s: inout [Character]) { | |
| s.reverse() | |
| } |
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
| func reverseString(_ s: inout [Character]) { | |
| // Call our recursive helper function | |
| swapCharacters( | |
| atLeftPointer: 0, | |
| andRightPointer: s.count - 1, | |
| in: &s | |
| ) | |
| } | |
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
| func deleteNode(_ node: ListNode?) { | |
| // First we assign next's .value | |
| node?.val = node?.next?.val ?? Int() | |
| // And then next's .next | |
| node?.next = node?.next?.next | |
| } |
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
| func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { | |
| // Get a mutable reference to each head of l1 and l2 | |
| var currentL1: ListNode? = l1 | |
| var currentL2: ListNode? = l2 | |
| // Create a reference to a node we're going to append on | |
| var sumList: ListNode? = ListNode(-1) | |
| // And a reference to its head |
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
| func twoSum(_ nums: [Int], _ target: Int) -> [Int] { | |
| /** | |
| Init an empty dictionary. | |
| The value will be the array element. | |
| The key will be the index of the element. | |
| */ | |
| var hashMap: [Int: Int] = [:] | |
| // Iterate through every element in numbers | |
| for (i, element) in numbers.enumerated() { | |