Skip to content

Instantly share code, notes, and snippets.

View davidseek's full-sized avatar
💭
He/Him

David Seek davidseek

💭
He/Him
View GitHub Profile
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 {
func isPowerOfTwo(_ n: Int) -> Bool {
// 1
guard n != 1 && n != 2 else {
return true
}
// 2
var last: Int = 2
func levelOrder(_ root: TreeNode?) -> [[Int]] {
// 1
guard let root = root else {
return []
}
// 2
var queue = Queue()
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
}
func reverseString(_ s: inout [Character]) {
s = s.reversed()
}
func reverseString(_ s: inout [Character]) {
s.reverse()
}
func reverseString(_ s: inout [Character]) {
// Call our recursive helper function
swapCharacters(
atLeftPointer: 0,
andRightPointer: s.count - 1,
in: &s
)
}
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
}
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
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() {