Skip to content

Instantly share code, notes, and snippets.

View EJSohn's full-sized avatar

Harley Son EJSohn

View GitHub Profile
func sumSlice(slice []int) int {
tot := 0
for _, val := range slice {
tot += val
}
return tot
}
def sum_list(lst):
tot = 0
for val in lst:
tot += val
return tot
def sum_lst(lst):
tot = 0
for val in lst:
tot += val
return tot
# or return sum(lst) ¯\_(ツ)_/¯
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
recursiveWalk(t, ch)
close(ch) // close channel when done
}
func recursiveWalk(t *tree.Tree, ch chan int) {
if t != nil {
// in order
func twoSum(nums []int, target int) []int {
// map from int -> int
m := make(map[int]int)
// python like enumerate
for i, num := range(nums) {
// get value and check existence in map
// and in if statement where ok is the boolean comparison value
if index, ok := m[target - num]; ok {
// construct array and return values
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteDuplicates(head *ListNode) *ListNode {
// check head isn't nil (null)
if head == nil {
func searchRange(nums []int, target int) []int {
// empty nums case
if len(nums) == 0 {
return []int{-1, -1}
}
// find starting and ending
return []int{findStarting(nums, target), findEnding(nums, target)}
}
func findStarting(nums []int, target int) int {
type LRUCache struct {
capacity int
cache map[int]*node // map
head, tail *node // linked list
}
type node struct {
prev, next *node
key, val int
}
$ free -m
total used free shared buff/cache available
Mem: 990 78 518 4 392 867
Swap: 0 0 0
$ free -m
total used free shared buff/cache available
Mem: 990 127 453 4 410 818
Swap: 0 0 0