Skip to content

Instantly share code, notes, and snippets.

// https://leetcode.com/problems/implement-trie-prefix-tree
type Trie struct {
root *TrieNode
}
type TrieNode struct {
children []*TrieNode
isLeaf bool
}
// https://leetcode.com/problems/minimum-size-subarray-sum/
func minSubArrayLen(s int, nums []int) int {
n := len(nums)
sum, min := 0, n + 1
for i, j := 0, 0; j < n && i < n; j++ {
sum += nums[j]
for i <= j && sum >= s {
// https://leetcode.com/problems/course-schedule-ii
func findOrder(numCourses int, prerequisites [][]int) []int {
graph := make([][]int, numCourses)
indirects := make([]int, numCourses)
for _, pair := range prerequisites {
from, to := pair[0], pair[1]
graph[from] = append(graph[from], to)
indirects[to]++
/*
https://leetcode.com/problems/add-and-search-word-data-structure-design/
This solution based on the key idea of search engines: Inverted Index
Please have a look at this https://nlp.stanford.edu/IR-book/html/htmledition/an-example-information-retrieval-problem-1.html
Or take a look at this open source project https://github.com/suggest-go/suggest
Briefly description:
// https://leetcode.com/problems/house-robber-ii
func rob(nums []int) int {
n := len(nums)
if n == 0 {
return 0
}
if n == 1 {
// https://leetcode.com/problems/kth-largest-element-in-an-array
import "container/heap"
type Heap []int
func (h Heap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
// https://leetcode.com/problems/combination-sum-iii
func combinationSum3(k int, n int) [][]int {
if k == 1 {
return [][]int{{n}}
}
result := [][]int{}
combination(1, k, n, []int{}, &result)
// https://leetcode.com/problems/maximal-square
func maximalSquare(matrix [][]byte) int {
n := len(matrix)
if n == 0 {
return 0
}
m := len(matrix[0])
// https://leetcode.com/problems/basic-calculator-ii
import "strings"
const (
space = rune(' ')
plus = rune('+')
minus = rune('-')
divide = rune('/')
asterisk = rune('*')
// https://leetcode.com/problems/summary-ranges
func summaryRanges(nums []int) []string {
numsLen := len(nums)
if numsLen == 0 {
return []string{}
}
ranges := []string{}