Skip to content

Instantly share code, notes, and snippets.

View EJSohn's full-sized avatar

Harley Son EJSohn

View GitHub Profile
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 {
/**
* 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 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
// 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
def sum_lst(lst):
tot = 0
for val in lst:
tot += val
return tot
# or return sum(lst) ¯\_(ツ)_/¯
def sum_list(lst):
tot = 0
for val in lst:
tot += val
return tot
func sumSlice(slice []int) int {
tot := 0
for _, val := range slice {
tot += val
}
return tot
}
struct LinkNode {
Value int
Prev, Next *LinkNode
}
// c like struct
type TreeNode struct {
Value int
Left, Right *TreeNode // pointers
}
func DfsInOrder(treeNode *TreeNode) {
if treeNode == nil {
return
@EJSohn
EJSohn / two_sum.go
Created April 19, 2019 15:39
translation code snippet (2019.04.20)
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