Skip to content

Instantly share code, notes, and snippets.

// https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree
/**
* Definition for TreeNode.
* type TreeNode struct {
* Val int
* Left *ListNode
* Right *ListNode
* }
*/
// https://leetcode.com/problems/kth-smallest-element-in-a-bst
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// https://leetcode.com/problems/majority-element-ii
import "sort"
func majorityElement(nums []int) []int {
if len(nums) < 2 {
return nums
}
sort.Ints(nums)
// https://leetcode.com/problems/summary-ranges
func summaryRanges(nums []int) []string {
numsLen := len(nums)
if numsLen == 0 {
return []string{}
}
ranges := []string{}
// https://leetcode.com/problems/basic-calculator-ii
import "strings"
const (
space = rune(' ')
plus = rune('+')
minus = rune('-')
divide = rune('/')
asterisk = rune('*')
// 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/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/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/house-robber-ii
func rob(nums []int) int {
n := len(nums)
if n == 0 {
return 0
}
if n == 1 {
/*
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: