Skip to content

Instantly share code, notes, and snippets.

View derekli66's full-sized avatar
🤓
Being.a.nerd()

Chien-Ming (Derek) Lee derekli66

🤓
Being.a.nerd()
View GitHub Profile
@derekli66
derekli66 / NSTaskExample.swift
Created October 23, 2022 06:44
Sample code to use NSTask(Process) and NSPipe and NSRunloop
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)
@derekli66
derekli66 / LRUCache.swift
Created September 5, 2022 07:53
LRU cache implementation in Swift
class ListNode<T> {
var val: T?
var key: Int
var next: ListNode?
var previous: ListNode?
init(key: Int) {
self.key = key
}
}
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])
}
/**
* 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
@derekli66
derekli66 / KMP.swift
Created June 14, 2022 15:46
Implement KMPMatch and KMPSearch function to check if pattern exists and the index of matched pattern
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] {
@derekli66
derekli66 / KMP_String_Matching.swift
Created June 5, 2022 17:07
KMP string matching algorithm. Implement in Swift.
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
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 {
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 }
@derekli66
derekli66 / queue_using_stacks.swift
Created May 13, 2022 21:04
Solution to LeetCode problem, 232 Implement Queue using Stacks. https://leetcode.com/problems/implement-queue-using-stacks/
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
@derekli66
derekli66 / stack_using_queues.swift
Created May 13, 2022 21:02
Solution to LeetCode problem, 225 Implement Stack using Queues. https://leetcode.com/problems/implement-stack-using-queues/
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?