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
import Foundation | |
let task = Process() | |
// Set the launch tool path | |
task.launchPath = "/bin/sh" | |
// Set the command to run the task | |
let commandStr = "ls -l /Users/derek/PG"// "last | grep reboot" | |
var arguments = ["-c"] | |
arguments.append(commandStr) |
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 ListNode<T> { | |
var val: T? | |
var key: Int | |
var next: ListNode? | |
var previous: ListNode? | |
init(key: Int) { | |
self.key = key | |
} | |
} |
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 findKthLargest(_ nums: [Int], _ k: Int) -> Int { | |
if nums.count < k { return 0 } | |
let minHeap = MinHeap() | |
for idx in 0..<k { | |
minHeap.insert(nums[idx]) | |
} | |
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
/** | |
* 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 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
import Foundation | |
func computeLPS(_ pattern: String) -> [Int] { | |
let array = Array(pattern).map{String($0)} | |
var i = 1 | |
var j = 0 | |
var lps = Array(repeating: 0, count: array.count) | |
while i < array.count { | |
if array[j] == array[i] { |
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 buildKMP(_ pattern: String) -> [Int] { | |
let strs = Array(pattern).map { String($0) } | |
var kmp = Array(repeating: 0, count: strs.count) | |
var i = 1 | |
var j = 0 | |
while i < strs.count { | |
if strs[i] == strs[j] { | |
kmp[i] = j + 1 | |
i += 1 |
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 isPrefixOfWord(_ sentence: String, _ searchWord: String) -> Int { | |
let sentence = " " + sentence | |
let searchWord = " " + searchWord | |
let index = contains(sentence, searchWord) | |
let strs = Array(sentence).map{String($0)} | |
var count = 0 | |
for idx in 0..<strs.count { |
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 stringMatching(_ words: [String]) -> [String] { | |
var matched = Set<String>() | |
for idx in 0..<words.count { | |
let pattern = words[idx] | |
for j in 0..<words.count { | |
if j == idx { continue } | |
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
private class ListNode { | |
var next: ListNode? | |
var prev: ListNode? | |
var key: Int = 0 | |
} | |
class Stack { | |
private var head: ListNode? | |
private var tail: ListNode? | |
private var count: Int = 0 |
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 ListNode { | |
var next: ListNode? | |
var key: Int = 0 | |
} | |
/* | |
push to back, peek/pop from front, size and is empty | |
*/ | |
class LinkedList { | |
private var head: ListNode? | |
private var tail: ListNode? |
NewerOlder