Skip to content

Instantly share code, notes, and snippets.

func uniquePaths(m int, n int) int {
// we are going to memorize all previous steps
moves := make([][]int, n)
for i, _ := range moves {
moves[i] = make([]int, m)
}
// we are already located at the top-left corner.
// There is only one way to be at this cell.
// https://leetcode.com/problems/network-delay-time
const inf = (1 << 31) - 1
type Edge struct {
vertex int
time int
}
func networkDelayTime(times [][]int, N int, K int) int {
// https://leetcode.com/problems/linked-list-cycle/
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func hasCycle(head *ListNode) bool {
hare := head
// https://leetcode.com/problems/binary-tree-paths
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// https://leetcode.com/problems/reconstruct-itinerary
// MUC -> [LHR]
// JFK -> [MUC]
// SFO -> [SJC]
// LHR -> [SFO]
//
// JFK -> [SFO, ATL]
// SFO -> [ATL]
// ATL -> [JFK,SFO]
// https://leetcode.com/problems/reverse-words-in-a-string
func reverseWords(s string) string {
words := ""
chars := []rune(s)
anchor := 0
for i := 0; i < len(chars); i++ {
if chars[i] == rune(' ') {
if anchor < i {
// https://leetcode.com/problems/symmetric-tree/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// https://leetcode.com/problems/intersection-of-two-arrays-ii
import "sort"
func intersect(nums1 []int, nums2 []int) []int {
sort.Ints(nums1)
sort.Ints(nums2)
if len(nums1) > len(nums2) {
nums1, nums2 = nums2, nums1
// https://leetcode.com/problems/insert-delete-getrandom-o1
import "math/rand"
type RandomizedSet struct {
table map[int]int
dict []int
}
// https://leetcode.com/problems/implement-queue-using-stacks
// -> [][][][] ->
// <-> [][][][]
// <-> [][][][]
type MyQueue struct {
front int
ordered []int
reversed []int
}