repo
stringlengths
6
47
file_url
stringlengths
77
269
file_path
stringlengths
5
186
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-07 08:35:43
2026-01-07 08:55:24
truncated
bool
2 classes
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0874.Walking-Robot-Simulation/874. Walking Robot Simulation_test.go
leetcode/0874.Walking-Robot-Simulation/874. Walking Robot Simulation_test.go
package leetcode import "testing" func Test_robotSim(t *testing.T) { type args struct { commands []int obstacles [][]int } cases := []struct { name string args args want int }{ { "case 1", args{ commands: []int{4, -1, 3}, obstacles: [][]int{{}}, }, 25, }, { "case 2", args{ commands: []int{4, -1, 4, -2, 4}, obstacles: [][]int{{2, 4}}, }, 65, }, } for _, tt := range cases { t.Run(tt.name, func(t *testing.T) { if got := robotSim(tt.args.commands, tt.args.obstacles); got != tt.want { t.Errorf("robotSim() = %v, want %v", got, tt.want) } }) } }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0874.Walking-Robot-Simulation/874. Walking Robot Simulation.go
leetcode/0874.Walking-Robot-Simulation/874. Walking Robot Simulation.go
package leetcode func robotSim(commands []int, obstacles [][]int) int { m := make(map[[2]int]struct{}) for _, v := range obstacles { if len(v) != 0 { m[[2]int{v[0], v[1]}] = struct{}{} } } directX := []int{0, 1, 0, -1} directY := []int{1, 0, -1, 0} direct, x, y := 0, 0, 0 result := 0 for _, c := range commands { if c == -2 { direct = (direct + 3) % 4 continue } if c == -1 { direct = (direct + 1) % 4 continue } for ; c > 0; c-- { nextX := x + directX[direct] nextY := y + directY[direct] if _, ok := m[[2]int{nextX, nextY}]; ok { break } tmpResult := nextX*nextX + nextY*nextY if tmpResult > result { result = tmpResult } x = nextX y = nextY } } return result }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0891.Sum-of-Subsequence-Widths/891. Sum of Subsequence Widths.go
leetcode/0891.Sum-of-Subsequence-Widths/891. Sum of Subsequence Widths.go
package leetcode import ( "sort" ) func sumSubseqWidths(A []int) int { sort.Ints(A) res, mod, n, p := 0, 1000000007, len(A), 1 for i := 0; i < n; i++ { res = (res + (A[i]-A[n-1-i])*p) % mod p = (p << 1) % mod } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0891.Sum-of-Subsequence-Widths/891. Sum of Subsequence Widths_test.go
leetcode/0891.Sum-of-Subsequence-Widths/891. Sum of Subsequence Widths_test.go
package leetcode import ( "fmt" "testing" ) type question891 struct { para891 ans891 } // para 是参数 // one 代表第一个参数 type para891 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans891 struct { one int } func Test_Problem891(t *testing.T) { qs := []question891{ { para891{[]int{2, 1, 3}}, ans891{6}, }, { para891{[]int{3, 7, 2, 3}}, ans891{35}, }, } fmt.Printf("------------------------Leetcode Problem 891------------------------\n") for _, q := range qs { _, p := q.ans891, q.para891 fmt.Printf("【input】:%v 【output】:%v\n", p, sumSubseqWidths(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0204.Count-Primes/204. Count Primes_test.go
leetcode/0204.Count-Primes/204. Count Primes_test.go
package leetcode import ( "fmt" "testing" ) type question204 struct { para204 ans204 } // para 是参数 // one 代表第一个参数 type para204 struct { one int } // ans 是答案 // one 代表第一个答案 type ans204 struct { one int } func Test_Problem204(t *testing.T) { qs := []question204{ { para204{10}, ans204{4}, }, { para204{100}, ans204{25}, }, { para204{1000}, ans204{168}, }, } fmt.Printf("------------------------Leetcode Problem 204------------------------\n") for _, q := range qs { _, p := q.ans204, q.para204 fmt.Printf("【input】:%v 【output】:%v\n", p, countPrimes(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0204.Count-Primes/204. Count Primes.go
leetcode/0204.Count-Primes/204. Count Primes.go
package leetcode func countPrimes(n int) int { isNotPrime := make([]bool, n) for i := 2; i*i < n; i++ { if isNotPrime[i] { continue } for j := i * i; j < n; j = j + i { isNotPrime[j] = true } } count := 0 for i := 2; i < n; i++ { if !isNotPrime[i] { count++ } } return count }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1383.Maximum-Performance-of-a-Team/1383. Maximum Performance of a Team_test.go
leetcode/1383.Maximum-Performance-of-a-Team/1383. Maximum Performance of a Team_test.go
package leetcode import ( "fmt" "testing" ) type question1383 struct { para1383 ans1383 } // para 是参数 // one 代表第一个参数 type para1383 struct { n int speed []int efficiency []int k int } // ans 是答案 // one 代表第一个答案 type ans1383 struct { one int } func Test_Problem1383(t *testing.T) { qs := []question1383{ { para1383{6, []int{2, 10, 3, 1, 5, 8}, []int{5, 4, 3, 9, 7, 2}, 2}, ans1383{60}, }, { para1383{6, []int{2, 10, 3, 1, 5, 8}, []int{5, 4, 3, 9, 7, 2}, 3}, ans1383{68}, }, { para1383{6, []int{2, 10, 3, 1, 5, 8}, []int{5, 4, 3, 9, 7, 2}, 4}, ans1383{72}, }, } fmt.Printf("------------------------Leetcode Problem 1383------------------------\n") for _, q := range qs { _, p := q.ans1383, q.para1383 fmt.Printf("【input】:%v 【output】:%v\n", p, maxPerformance(p.n, p.speed, p.efficiency, p.k)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1383.Maximum-Performance-of-a-Team/1383. Maximum Performance of a Team.go
leetcode/1383.Maximum-Performance-of-a-Team/1383. Maximum Performance of a Team.go
package leetcode import ( "container/heap" "sort" ) func maxPerformance(n int, speed []int, efficiency []int, k int) int { indexes := make([]int, n) for i := range indexes { indexes[i] = i } sort.Slice(indexes, func(i, j int) bool { return efficiency[indexes[i]] > efficiency[indexes[j]] }) ph := speedHeap{} heap.Init(&ph) speedSum := 0 var max int64 for _, index := range indexes { if ph.Len() == k { speedSum -= heap.Pop(&ph).(int) } speedSum += speed[index] heap.Push(&ph, speed[index]) max = Max(max, int64(speedSum)*int64(efficiency[index])) } return int(max % (1e9 + 7)) } type speedHeap []int func (h speedHeap) Less(i, j int) bool { return h[i] < h[j] } func (h speedHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h speedHeap) Len() int { return len(h) } func (h *speedHeap) Push(x interface{}) { *h = append(*h, x.(int)) } func (h *speedHeap) Pop() interface{} { res := (*h)[len(*h)-1] *h = (*h)[:h.Len()-1] return res } func Max(a, b int64) int64 { if a > b { return a } return b }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0904.Fruit-Into-Baskets/904. Fruit Into Baskets_test.go
leetcode/0904.Fruit-Into-Baskets/904. Fruit Into Baskets_test.go
package leetcode import ( "fmt" "testing" ) type question904 struct { para904 ans904 } // para 是参数 // one 代表第一个参数 type para904 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans904 struct { one int } func Test_Problem904(t *testing.T) { qs := []question904{ { para904{[]int{1, 1}}, ans904{2}, }, { para904{[]int{1, 2, 1}}, ans904{3}, }, { para904{[]int{0, 1, 2, 2}}, ans904{3}, }, { para904{[]int{1, 2, 3, 2, 2}}, ans904{4}, }, { para904{[]int{3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4}}, ans904{5}, }, { para904{[]int{4, 5}}, ans904{2}, }, { para904{[]int{1}}, ans904{1}, }, { para904{[]int{}}, ans904{}, }, } fmt.Printf("------------------------Leetcode Problem 904------------------------\n") for _, q := range qs { _, p := q.ans904, q.para904 fmt.Printf("【input】:%v 【output】:%v\n", p, totalFruit(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0904.Fruit-Into-Baskets/904. Fruit Into Baskets.go
leetcode/0904.Fruit-Into-Baskets/904. Fruit Into Baskets.go
package leetcode func totalFruit(tree []int) int { if len(tree) == 0 { return 0 } left, right, counter, res, freq := 0, 0, 1, 1, map[int]int{} freq[tree[0]]++ for left < len(tree) { if right+1 < len(tree) && ((counter > 0 && tree[right+1] != tree[left]) || (tree[right+1] == tree[left] || freq[tree[right+1]] > 0)) { if counter > 0 && tree[right+1] != tree[left] { counter-- } right++ freq[tree[right]]++ } else { if counter == 0 || (counter > 0 && right == len(tree)-1) { res = max(res, right-left+1) } freq[tree[left]]-- if freq[tree[left]] == 0 { counter++ } left++ } } return res } func max(a int, b int) int { if a > b { return a } return b }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0978.Longest-Turbulent-Subarray/978. Longest Turbulent Subarray.go
leetcode/0978.Longest-Turbulent-Subarray/978. Longest Turbulent Subarray.go
package leetcode // 解法一 模拟法 func maxTurbulenceSize(arr []int) int { inc, dec := 1, 1 maxLen := min(1, len(arr)) for i := 1; i < len(arr); i++ { if arr[i-1] < arr[i] { inc = dec + 1 dec = 1 } else if arr[i-1] > arr[i] { dec = inc + 1 inc = 1 } else { inc = 1 dec = 1 } maxLen = max(maxLen, max(inc, dec)) } return maxLen } func max(a int, b int) int { if a > b { return a } return b } func min(a int, b int) int { if a > b { return b } return a } // 解法二 滑动窗口 func maxTurbulenceSize1(arr []int) int { var maxLength int if len(arr) == 2 && arr[0] != arr[1] { maxLength = 2 } else { maxLength = 1 } left := 0 for right := 2; right < len(arr); right++ { if arr[right] == arr[right-1] { left = right } else if (arr[right]-arr[right-1])^(arr[right-1]-arr[right-2]) >= 0 { left = right - 1 } maxLength = max(maxLength, right-left+1) } return maxLength }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0978.Longest-Turbulent-Subarray/978. Longest Turbulent Subarray_test.go
leetcode/0978.Longest-Turbulent-Subarray/978. Longest Turbulent Subarray_test.go
package leetcode import ( "fmt" "testing" ) type question978 struct { para978 ans978 } // para 是参数 // one 代表第一个参数 type para978 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans978 struct { one int } func Test_Problem978(t *testing.T) { qs := []question978{ { para978{[]int{0, 1, 1, 0, 1, 0, 1, 1, 0, 0}}, ans978{5}, }, { para978{[]int{9, 9}}, ans978{1}, }, { para978{[]int{9, 4, 2, 10, 7, 8, 8, 1, 9}}, ans978{5}, }, { para978{[]int{4, 8, 12, 16}}, ans978{2}, }, { para978{[]int{100}}, ans978{1}, }, } fmt.Printf("------------------------Leetcode Problem 978------------------------\n") for _, q := range qs { _, p := q.ans978, q.para978 fmt.Printf("【input】:%v 【output】:%v\n", p, maxTurbulenceSize(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0211.Design-Add-and-Search-Words-Data-Structure/211. Design Add and Search Words Data Structure_test.go
leetcode/0211.Design-Add-and-Search-Words-Data-Structure/211. Design Add and Search Words Data Structure_test.go
package leetcode import ( "fmt" "testing" ) func Test_Problem211(t *testing.T) { obj := Constructor211() fmt.Printf("obj = %v\n", obj) obj.AddWord("bad") fmt.Printf("obj = %v\n", obj) obj.AddWord("dad") fmt.Printf("obj = %v\n", obj) obj.AddWord("mad") fmt.Printf("obj = %v\n", obj) param1 := obj.Search("pad") fmt.Printf("param_1 = %v obj = %v\n", param1, obj) param2 := obj.Search("bad") fmt.Printf("param_2 = %v obj = %v\n", param2, obj) param3 := obj.Search(".ad") fmt.Printf("param_3 = %v obj = %v\n", param3, obj) param4 := obj.Search("b..") fmt.Printf("param_4 = %v obj = %v\n", param4, obj) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0211.Design-Add-and-Search-Words-Data-Structure/211. Design Add and Search Words Data Structure.go
leetcode/0211.Design-Add-and-Search-Words-Data-Structure/211. Design Add and Search Words Data Structure.go
package leetcode type WordDictionary struct { children map[rune]*WordDictionary isWord bool } /** Initialize your data structure here. */ func Constructor211() WordDictionary { return WordDictionary{children: make(map[rune]*WordDictionary)} } /** Adds a word into the data structure. */ func (this *WordDictionary) AddWord(word string) { parent := this for _, ch := range word { if child, ok := parent.children[ch]; ok { parent = child } else { newChild := &WordDictionary{children: make(map[rune]*WordDictionary)} parent.children[ch] = newChild parent = newChild } } parent.isWord = true } /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ func (this *WordDictionary) Search(word string) bool { parent := this for i, ch := range word { if rune(ch) == '.' { isMatched := false for _, v := range parent.children { if v.Search(word[i+1:]) { isMatched = true } } return isMatched } else if _, ok := parent.children[rune(ch)]; !ok { return false } parent = parent.children[rune(ch)] } return len(parent.children) == 0 || parent.isWord } /** * Your WordDictionary object will be instantiated and called as such: * obj := Constructor(); * obj.AddWord(word); * param_2 := obj.Search(word); */
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0433.Minimum-Genetic-Mutation/433. Minimum Genetic Mutation.go
leetcode/0433.Minimum-Genetic-Mutation/433. Minimum Genetic Mutation.go
package leetcode // 解法一 BFS func minMutation(start string, end string, bank []string) int { wordMap, que, depth := getWordMap(bank, start), []string{start}, 0 for len(que) > 0 { depth++ qlen := len(que) for i := 0; i < qlen; i++ { word := que[0] que = que[1:] candidates := getCandidates433(word) for _, candidate := range candidates { if _, ok := wordMap[candidate]; ok { if candidate == end { return depth } delete(wordMap, candidate) que = append(que, candidate) } } } } return -1 } func getWordMap(wordList []string, beginWord string) map[string]int { wordMap := make(map[string]int) for i, word := range wordList { if _, ok := wordMap[word]; !ok { if word != beginWord { wordMap[word] = i } } } return wordMap } func getCandidates433(word string) []string { var res []string for i := 0; i < 26; i++ { for j := 0; j < len(word); j++ { if word[j] != byte(int('A')+i) { res = append(res, word[:j]+string(rune(int('A')+i))+word[j+1:]) } } } return res } // 解法二 DFS func minMutation1(start string, end string, bank []string) int { endGene := convert(end) startGene := convert(start) m := make(map[uint32]struct{}) for _, gene := range bank { m[convert(gene)] = struct{}{} } if _, ok := m[endGene]; !ok { return -1 } if check(startGene ^ endGene) { return 1 } delete(m, startGene) step := make(map[uint32]int) step[endGene] = 0 return dfsMutation(startGene, m, step) } func dfsMutation(start uint32, m map[uint32]struct{}, step map[uint32]int) int { if v, ok := step[start]; ok { return v } c := -1 step[start] = c for k := range m { if check(k ^ start) { next := dfsMutation(k, m, step) if next != -1 { if c == -1 || c > next { c = next + 1 } } } } step[start] = c return c } func check(val uint32) bool { if val == 0 { return false } if val&(val-1) == 0 { return true } for val > 0 { if val == 3 { return true } if val&3 != 0 { return false } val >>= 2 } return false } func convert(gene string) uint32 { var v uint32 for _, c := range gene { v <<= 2 switch c { case 'C': v |= 1 case 'G': v |= 2 case 'T': v |= 3 } } return v }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0433.Minimum-Genetic-Mutation/433. Minimum Genetic Mutation_test.go
leetcode/0433.Minimum-Genetic-Mutation/433. Minimum Genetic Mutation_test.go
package leetcode import ( "fmt" "testing" ) type question433 struct { para433 ans433 } // para 是参数 // one 代表第一个参数 type para433 struct { start string end string bank []string } // ans 是答案 // one 代表第一个答案 type ans433 struct { one int } func Test_Problem433(t *testing.T) { qs := []question433{ { para433{"AACCGGTT", "AACCGGTA", []string{"AACCGGTA"}}, ans433{1}, }, { para433{"AACCGGTT", "AAACGGTA", []string{"AACCGGTA", "AACCGCTA", "AAACGGTA"}}, ans433{2}, }, { para433{"AAAAACCC", "AACCCCCC", []string{"AAAACCCC", "AAACCCCC", "AACCCCCC"}}, ans433{3}, }, { para433{"AACCGGTT", "AACCGGTA", []string{}}, ans433{-1}, }, } fmt.Printf("------------------------Leetcode Problem 433------------------------\n") for _, q := range qs { _, p := q.ans433, q.para433 fmt.Printf("【input】:%v 【output】:%v\n", p, minMutation(p.start, p.end, p.bank)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0077.Combinations/77. Combinations_test.go
leetcode/0077.Combinations/77. Combinations_test.go
package leetcode import ( "fmt" "testing" ) type question77 struct { para77 ans77 } // para 是参数 // one 代表第一个参数 type para77 struct { n int k int } // ans 是答案 // one 代表第一个答案 type ans77 struct { one [][]int } func Test_Problem77(t *testing.T) { qs := []question77{ { para77{4, 2}, ans77{[][]int{{2, 4}, {3, 4}, {2, 3}, {1, 2}, {1, 3}, {1, 4}}}, }, } fmt.Printf("------------------------Leetcode Problem 77------------------------\n") for _, q := range qs { _, p := q.ans77, q.para77 fmt.Printf("【input】:%v 【output】:%v\n", p, combine(p.n, p.k)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0077.Combinations/77. Combinations.go
leetcode/0077.Combinations/77. Combinations.go
package leetcode func combine(n int, k int) [][]int { if n <= 0 || k <= 0 || k > n { return [][]int{} } c, res := []int{}, [][]int{} generateCombinations(n, k, 1, c, &res) return res } func generateCombinations(n, k, start int, c []int, res *[][]int) { if len(c) == k { b := make([]int, len(c)) copy(b, c) *res = append(*res, b) return } // i will at most be n - (k - c.size()) + 1 for i := start; i <= n-(k-len(c))+1; i++ { c = append(c, i) generateCombinations(n, k, i+1, c, res) c = c[:len(c)-1] } return }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0062.Unique-Paths/62. Unique Paths_test.go
leetcode/0062.Unique-Paths/62. Unique Paths_test.go
package leetcode import ( "fmt" "testing" ) type question62 struct { para62 ans62 } // para 是参数 // one 代表第一个参数 type para62 struct { m int n int } // ans 是答案 // one 代表第一个答案 type ans62 struct { one int } func Test_Problem62(t *testing.T) { qs := []question62{ { para62{3, 2}, ans62{3}, }, { para62{7, 3}, ans62{28}, }, { para62{1, 2}, ans62{1}, }, } fmt.Printf("------------------------Leetcode Problem 62------------------------\n") for _, q := range qs { _, p := q.ans62, q.para62 fmt.Printf("【input】:%v 【output】:%v\n", p, uniquePaths(p.m, p.n)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0062.Unique-Paths/62. Unique Paths.go
leetcode/0062.Unique-Paths/62. Unique Paths.go
package leetcode func uniquePaths(m int, n int) int { dp := make([][]int, n) for i := 0; i < n; i++ { dp[i] = make([]int, m) } for i := 0; i < n; i++ { for j := 0; j < m; j++ { if i == 0 || j == 0 { dp[i][j] = 1 continue } dp[i][j] = dp[i-1][j] + dp[i][j-1] } } return dp[n-1][m-1] }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0056.Merge-Intervals/56. Merge Intervals_test.go
leetcode/0056.Merge-Intervals/56. Merge Intervals_test.go
package leetcode import ( "fmt" "testing" ) type question56 struct { para56 ans56 } // para 是参数 // one 代表第一个参数 type para56 struct { one []Interval } // ans 是答案 // one 代表第一个答案 type ans56 struct { one []Interval } func Test_Problem56(t *testing.T) { qs := []question56{ { para56{[]Interval{}}, ans56{[]Interval{}}, }, { para56{[]Interval{{Start: 1, End: 3}, {Start: 2, End: 6}, {Start: 8, End: 10}, {Start: 15, End: 18}}}, ans56{[]Interval{{Start: 1, End: 6}, {Start: 8, End: 10}, {Start: 15, End: 18}}}, }, { para56{[]Interval{{Start: 1, End: 4}, {Start: 4, End: 5}}}, ans56{[]Interval{{Start: 1, End: 5}}}, }, { para56{[]Interval{{Start: 1, End: 3}, {Start: 3, End: 6}, {Start: 5, End: 10}, {Start: 9, End: 18}}}, ans56{[]Interval{{Start: 1, End: 18}}}, }, { para56{[]Interval{{Start: 15, End: 18}, {Start: 8, End: 10}, {Start: 2, End: 6}, {Start: 1, End: 3}}}, ans56{[]Interval{{Start: 1, End: 6}, {Start: 8, End: 10}, {Start: 15, End: 18}}}, }, { para56{[]Interval{{Start: 1, End: 3}, {Start: 2, End: 6}, {Start: 8, End: 10}, {Start: 15, End: 18}}}, ans56{[]Interval{{Start: 1, End: 6}, {Start: 8, End: 10}, {Start: 15, End: 18}}}, }, { para56{[]Interval{{Start: 1, End: 4}, {Start: 1, End: 5}}}, ans56{[]Interval{{Start: 1, End: 5}}}, }, } fmt.Printf("------------------------Leetcode Problem 56------------------------\n") for _, q := range qs { _, p := q.ans56, q.para56 fmt.Printf("【input】:%v 【output】:%v\n", p, merge56(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0056.Merge-Intervals/56. Merge Intervals.go
leetcode/0056.Merge-Intervals/56. Merge Intervals.go
package leetcode import ( "github.com/halfrost/LeetCode-Go/structures" ) // Interval define type Interval = structures.Interval /** * Definition for an interval. * type Interval struct { * Start int * End int * } */ func merge56(intervals []Interval) []Interval { if len(intervals) == 0 { return intervals } quickSort(intervals, 0, len(intervals)-1) res := make([]Interval, 0) res = append(res, intervals[0]) curIndex := 0 for i := 1; i < len(intervals); i++ { if intervals[i].Start > res[curIndex].End { curIndex++ res = append(res, intervals[i]) } else { res[curIndex].End = max(intervals[i].End, res[curIndex].End) } } return res } func max(a int, b int) int { if a > b { return a } return b } func min(a int, b int) int { if a > b { return b } return a } func partitionSort(a []Interval, lo, hi int) int { pivot := a[hi] i := lo - 1 for j := lo; j < hi; j++ { if (a[j].Start < pivot.Start) || (a[j].Start == pivot.Start && a[j].End < pivot.End) { i++ a[j], a[i] = a[i], a[j] } } a[i+1], a[hi] = a[hi], a[i+1] return i + 1 } func quickSort(a []Interval, lo, hi int) { if lo >= hi { return } p := partitionSort(a, lo, hi) quickSort(a, lo, p-1) quickSort(a, p+1, hi) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0138.Copy-List-With-Random-Pointer/138. Copy List With Random Pointer.go
leetcode/0138.Copy-List-With-Random-Pointer/138. Copy List With Random Pointer.go
package leetcode // Node define type Node struct { Val int Next *Node Random *Node } func copyRandomList(head *Node) *Node { if head == nil { return nil } tempHead := copyNodeToLinkedList(head) return splitLinkedList(tempHead) } func splitLinkedList(head *Node) *Node { cur := head head = head.Next for cur != nil && cur.Next != nil { cur.Next, cur = cur.Next.Next, cur.Next } return head } func copyNodeToLinkedList(head *Node) *Node { cur := head for cur != nil { node := &Node{ Val: cur.Val, Next: cur.Next, } cur.Next, cur = node, cur.Next } cur = head for cur != nil { if cur.Random != nil { cur.Next.Random = cur.Random.Next } cur = cur.Next.Next } return head }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0138.Copy-List-With-Random-Pointer/138. Copy List With Random Pointer_test.go
leetcode/0138.Copy-List-With-Random-Pointer/138. Copy List With Random Pointer_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question138 struct { para138 ans138 } // para 是参数 // one 代表第一个参数 type para138 struct { head [][]int } // ans 是答案 // one 代表第一个答案 type ans138 struct { one [][]int } func Test_Problem138(t *testing.T) { qs := []question138{ { para138{[][]int{{7, structures.NULL}, {13, 0}, {11, 4}, {10, 2}, {1, 0}}}, ans138{[][]int{{7, structures.NULL}, {13, 0}, {11, 4}, {10, 2}, {1, 0}}}, }, { para138{[][]int{{1, 1}, {2, 1}}}, ans138{[][]int{{1, 1}, {2, 1}}}, }, { para138{[][]int{{3, structures.NULL}, {3, 0}, {3, structures.NULL}}}, ans138{[][]int{{3, structures.NULL}, {3, 0}, {3, structures.NULL}}}, }, { para138{[][]int{}}, ans138{[][]int{}}, }, } fmt.Printf("------------------------Leetcode Problem 138------------------------\n") for _, q := range qs { _, p := q.ans138, q.para138 fmt.Printf("【input】:%v 【output】:%v\n", p, node2Ints(copyRandomList(ints2Node(p.head)))) } fmt.Printf("\n\n\n") } func ints2Node(one [][]int) *Node { nodesMap := map[int]*Node{} for index, nodes := range one { tmp := &Node{Val: nodes[0]} nodesMap[index] = tmp } for index, node := range nodesMap { if index != len(one)-1 { node.Next = nodesMap[index+1] } else { node.Next = nil } } for index, nodes := range one { if nodes[1] == structures.NULL { nodesMap[index].Random = nil } else { nodesMap[index].Random = nodesMap[nodes[1]] } } return nodesMap[0] } func node2Ints(head *Node) (res [][]int) { nodesMap, count, cur := map[*Node]int{}, 0, head if head == nil { return [][]int{} } for cur.Next != nil { nodesMap[cur] = count count++ cur = cur.Next } nodesMap[cur] = count tmp := []int{} cur = head for cur.Next != nil { tmp := append(tmp, cur.Val) if cur.Random == nil { tmp = append(tmp, structures.NULL) } else { tmp = append(tmp, nodesMap[cur.Random]) } res = append(res, tmp) cur = cur.Next } tmp = append(tmp, cur.Val) if cur.Random == nil { tmp = append(tmp, structures.NULL) } else { tmp = append(tmp, nodesMap[cur.Random]) } res = append(res, tmp) return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1689.Partitioning-Into-Minimum-Number-Of-Deci-Binary-Numbers/1689. Partitioning Into Minimum Number Of Deci-Binary Numbers.go
leetcode/1689.Partitioning-Into-Minimum-Number-Of-Deci-Binary-Numbers/1689. Partitioning Into Minimum Number Of Deci-Binary Numbers.go
package leetcode func minPartitions(n string) int { res := 0 for i := 0; i < len(n); i++ { if int(n[i]-'0') > res { res = int(n[i] - '0') } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1689.Partitioning-Into-Minimum-Number-Of-Deci-Binary-Numbers/1689. Partitioning Into Minimum Number Of Deci-Binary Numbers_test.go
leetcode/1689.Partitioning-Into-Minimum-Number-Of-Deci-Binary-Numbers/1689. Partitioning Into Minimum Number Of Deci-Binary Numbers_test.go
package leetcode import ( "fmt" "testing" ) type question1689 struct { para1689 ans1689 } // para 是参数 // one 代表第一个参数 type para1689 struct { n string } // ans 是答案 // one 代表第一个答案 type ans1689 struct { one int } func Test_Problem1689(t *testing.T) { qs := []question1689{ { para1689{"32"}, ans1689{3}, }, { para1689{"82734"}, ans1689{8}, }, } fmt.Printf("------------------------Leetcode Problem 1689------------------------\n") for _, q := range qs { _, p := q.ans1689, q.para1689 fmt.Printf("【input】:%v 【output】:%v\n", p, minPartitions(p.n)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0725.Split-Linked-List-in-Parts/725. Split Linked List in Parts.go
leetcode/0725.Split-Linked-List-in-Parts/725. Split Linked List in Parts.go
package leetcode import ( "fmt" "github.com/halfrost/LeetCode-Go/structures" ) // ListNode define type ListNode = structures.ListNode /** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ func splitListToParts(root *ListNode, k int) []*ListNode { res := make([]*ListNode, 0) if root == nil { for i := 0; i < k; i++ { res = append(res, nil) } return res } length := getLength(root) splitNum := length / k lengNum := length % k cur, head := root, root var pre *ListNode fmt.Printf("总长度 %v, 分 %v 组, 前面 %v 组长度为 %v, 剩余 %v 组,每组 %v\n", length, k, lengNum, splitNum+1, k-lengNum, splitNum) if splitNum == 0 { for i := 0; i < k; i++ { if cur != nil { pre = cur.Next cur.Next = nil res = append(res, cur) cur = pre } else { res = append(res, nil) } } return res } for i := 0; i < lengNum; i++ { for j := 0; j < splitNum; j++ { cur = cur.Next } fmt.Printf("0 刚刚出来 head = %v cur = %v pre = %v\n", head, cur, head) pre = cur.Next cur.Next = nil res = append(res, head) head = pre cur = pre fmt.Printf("0 head = %v cur = %v pre = %v\n", head, cur, head) } for i := 0; i < k-lengNum; i++ { for j := 0; j < splitNum-1; j++ { cur = cur.Next } fmt.Printf("1 刚刚出来 head = %v cur = %v pre = %v\n", head, cur, head) pre = cur.Next cur.Next = nil res = append(res, head) head = pre cur = pre } return res } func getLength(l *ListNode) int { count := 0 cur := l for cur != nil { count++ cur = cur.Next } return count }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0725.Split-Linked-List-in-Parts/725. Split Linked List in Parts_test.go
leetcode/0725.Split-Linked-List-in-Parts/725. Split Linked List in Parts_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question725 struct { para725 ans725 } // para 是参数 // one 代表第一个参数 type para725 struct { one []int n int } // ans 是答案 // one 代表第一个答案 type ans725 struct { one []int } func Test_Problem725(t *testing.T) { qs := []question725{ { para725{[]int{1, 2, 3, 4, 5}, 7}, ans725{[]int{1, 2, 3, 4, 5, 0, 0}}, }, { para725{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 3}, ans725{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, }, { para725{[]int{1, 1, 1, 1, 1}, 1}, ans725{[]int{1, 1, 1, 1, 1}}, }, { para725{[]int{}, 3}, ans725{[]int{}}, }, { para725{[]int{1, 2, 3, 4, 5}, 5}, ans725{[]int{1, 2, 3, 4}}, }, { para725{[]int{}, 5}, ans725{[]int{}}, }, { para725{[]int{1, 2, 3, 4, 5}, 10}, ans725{[]int{1, 2, 3, 4, 5}}, }, { para725{[]int{1}, 1}, ans725{[]int{}}, }, } fmt.Printf("------------------------Leetcode Problem 725------------------------\n") for _, q := range qs { _, p := q.ans725, q.para725 res := splitListToParts(structures.Ints2List(p.one), p.n) for _, value := range res { fmt.Printf("【input】:%v length:%v 【output】:%v\n", p, len(res), structures.List2Ints(value)) } fmt.Printf("\n\n\n") } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0089.Gray-Code/89. Gray Code_test.go
leetcode/0089.Gray-Code/89. Gray Code_test.go
package leetcode import ( "fmt" "testing" ) type question89 struct { para89 ans89 } // para 是参数 // one 代表第一个参数 type para89 struct { one int } // ans 是答案 // one 代表第一个答案 type ans89 struct { one []int } func Test_Problem89(t *testing.T) { qs := []question89{ { para89{2}, ans89{[]int{0, 1, 3, 2}}, }, { para89{0}, ans89{[]int{0}}, }, { para89{3}, ans89{[]int{0, 1, 3, 2, 6, 7, 5, 4}}, }, } fmt.Printf("------------------------Leetcode Problem 89------------------------\n") for _, q := range qs { _, p := q.ans89, q.para89 fmt.Printf("【input】:%v 【output】:%v\n", p, grayCode(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0089.Gray-Code/89. Gray Code.go
leetcode/0089.Gray-Code/89. Gray Code.go
package leetcode // 解法一 递归方法,时间复杂度和空间复杂度都较优 func grayCode(n int) []int { if n == 0 { return []int{0} } res := []int{} num := make([]int, n) generateGrayCode(int(1<<uint(n)), 0, &num, &res) return res } func generateGrayCode(n, step int, num *[]int, res *[]int) { if n == 0 { return } *res = append(*res, convertBinary(*num)) if step%2 == 0 { (*num)[len(*num)-1] = flipGrayCode((*num)[len(*num)-1]) } else { index := len(*num) - 1 for ; index >= 0; index-- { if (*num)[index] == 1 { break } } if index == 0 { (*num)[len(*num)-1] = flipGrayCode((*num)[len(*num)-1]) } else { (*num)[index-1] = flipGrayCode((*num)[index-1]) } } generateGrayCode(n-1, step+1, num, res) return } func convertBinary(num []int) int { res, rad := 0, 1 for i := len(num) - 1; i >= 0; i-- { res += num[i] * rad rad *= 2 } return res } func flipGrayCode(num int) int { if num == 0 { return 1 } return 0 } // 解法二 直译 func grayCode1(n int) []int { var l uint = 1 << uint(n) out := make([]int, l) for i := uint(0); i < l; i++ { out[i] = int((i >> 1) ^ i) } return out }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0456.132-Pattern/456. 132 Pattern_test.go
leetcode/0456.132-Pattern/456. 132 Pattern_test.go
package leetcode import ( "fmt" "testing" ) type question456 struct { para456 ans456 } // para 是参数 // one 代表第一个参数 type para456 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans456 struct { one bool } func Test_Problem456(t *testing.T) { qs := []question456{ { para456{[]int{}}, ans456{false}, }, { para456{[]int{1, 2, 3, 4}}, ans456{false}, }, { para456{[]int{3, 1, 4, 2}}, ans456{true}, }, { para456{[]int{-1, 3, 2, 0}}, ans456{true}, }, { para456{[]int{3, 5, 0, 3, 4}}, ans456{true}, }, // 如需多个测试,可以复制上方元素。 } fmt.Printf("------------------------Leetcode Problem 456------------------------\n") for _, q := range qs { _, p := q.ans456, q.para456 fmt.Printf("【input】:%v 【output】:%v\n", p, find132pattern(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0456.132-Pattern/456. 132 Pattern.go
leetcode/0456.132-Pattern/456. 132 Pattern.go
package leetcode import ( "math" ) // 解法一 单调栈 func find132pattern(nums []int) bool { if len(nums) < 3 { return false } num3, stack := math.MinInt64, []int{} for i := len(nums) - 1; i >= 0; i-- { if nums[i] < num3 { return true } for len(stack) != 0 && nums[i] > stack[len(stack)-1] { num3 = stack[len(stack)-1] stack = stack[:len(stack)-1] } stack = append(stack, nums[i]) } return false } // 解法二 暴力解法,超时! func find132pattern1(nums []int) bool { if len(nums) < 3 { return false } for j := 0; j < len(nums); j++ { stack := []int{} for i := j; i < len(nums); i++ { if len(stack) == 0 || (len(stack) > 0 && nums[i] > nums[stack[len(stack)-1]]) { stack = append(stack, i) } else if nums[i] < nums[stack[len(stack)-1]] { index := len(stack) - 1 for ; index >= 0; index-- { if nums[stack[index]] < nums[i] { return true } } } } } return false }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0079.Word-Search/79. Word Search_test.go
leetcode/0079.Word-Search/79. Word Search_test.go
package leetcode import ( "fmt" "testing" ) type question79 struct { para79 ans79 } // para 是参数 // one 代表第一个参数 type para79 struct { b [][]byte word string } // ans 是答案 // one 代表第一个答案 type ans79 struct { one bool } func Test_Problem79(t *testing.T) { qs := []question79{ { para79{[][]byte{ {'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}, }, "ABCCED"}, ans79{true}, }, { para79{[][]byte{ {'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}, }, "SEE"}, ans79{true}, }, { para79{[][]byte{ {'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}, }, "ABCB"}, ans79{false}, }, { para79{[][]byte{ {'o', 'a', 'a', 'n'}, {'e', 't', 'a', 'e'}, {'i', 'h', 'k', 'r'}, {'i', 'f', 'l', 'v'}, }, "oath"}, ans79{true}, }, { para79{[][]byte{ {'o', 'a', 'a', 'n'}, {'e', 't', 'a', 'e'}, {'i', 'h', 'k', 'r'}, {'i', 'f', 'l', 'v'}, }, "pea"}, ans79{false}, }, { para79{[][]byte{ {'o', 'a', 'a', 'n'}, {'e', 't', 'a', 'e'}, {'i', 'h', 'k', 'r'}, {'i', 'f', 'l', 'v'}, }, "eat"}, ans79{true}, }, { para79{[][]byte{ {'o', 'a', 'a', 'n'}, {'e', 't', 'a', 'e'}, {'i', 'h', 'k', 'r'}, {'i', 'f', 'l', 'v'}, }, "rain"}, ans79{false}, }, } fmt.Printf("------------------------Leetcode Problem 79------------------------\n") for _, q := range qs { _, p := q.ans79, q.para79 fmt.Printf("【input】:%v 【output】:%v\n\n\n", p, exist(p.b, p.word)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0079.Word-Search/79. Word Search.go
leetcode/0079.Word-Search/79. Word Search.go
package leetcode var dir = [][]int{ {-1, 0}, {0, 1}, {1, 0}, {0, -1}, } func exist(board [][]byte, word string) bool { visited := make([][]bool, len(board)) for i := 0; i < len(visited); i++ { visited[i] = make([]bool, len(board[0])) } for i, v := range board { for j := range v { if searchWord(board, visited, word, 0, i, j) { return true } } } return false } func isInBoard(board [][]byte, x, y int) bool { return x >= 0 && x < len(board) && y >= 0 && y < len(board[0]) } func searchWord(board [][]byte, visited [][]bool, word string, index, x, y int) bool { if index == len(word)-1 { return board[x][y] == word[index] } if board[x][y] == word[index] { visited[x][y] = true for i := 0; i < 4; i++ { nx := x + dir[i][0] ny := y + dir[i][1] if isInBoard(board, nx, ny) && !visited[nx][ny] && searchWord(board, visited, word, index+1, nx, ny) { return true } } visited[x][y] = false } return false }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0645.Set-Mismatch/645. Set Mismatch_test.go
leetcode/0645.Set-Mismatch/645. Set Mismatch_test.go
package leetcode import ( "fmt" "testing" ) type question645 struct { para645 ans645 } // para 是参数 // one 代表第一个参数 type para645 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans645 struct { one []int } func Test_Problem645(t *testing.T) { qs := []question645{ { para645{[]int{1, 2, 2, 4}}, ans645{[]int{2, 3}}, }, } fmt.Printf("------------------------Leetcode Problem 645------------------------\n") for _, q := range qs { _, p := q.ans645, q.para645 fmt.Printf("【input】:%v 【output】:%v\n", p, findErrorNums(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0645.Set-Mismatch/645. Set Mismatch.go
leetcode/0645.Set-Mismatch/645. Set Mismatch.go
package leetcode func findErrorNums(nums []int) []int { m, res := make([]int, len(nums)), make([]int, 2) for _, n := range nums { if m[n-1] == 0 { m[n-1] = 1 } else { res[0] = n } } for i := range m { if m[i] == 0 { res[1] = i + 1 break } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0470.Implement-Rand10-Using-Rand7/470. Implement Rand10() Using Rand7().go
leetcode/0470.Implement-Rand10-Using-Rand7/470. Implement Rand10() Using Rand7().go
package leetcode import "math/rand" func rand10() int { rand10 := 10 for rand10 >= 10 { rand10 = (rand7() - 1) + rand7() } return rand10%10 + 1 } func rand7() int { return rand.Intn(7) } func rand101() int { rand40 := 40 for rand40 >= 40 { rand40 = (rand7()-1)*7 + rand7() - 1 } return rand40%10 + 1 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0470.Implement-Rand10-Using-Rand7/470. Implement Rand10() Using Rand7()_test.go
leetcode/0470.Implement-Rand10-Using-Rand7/470. Implement Rand10() Using Rand7()_test.go
package leetcode import ( "fmt" "testing" ) type question470 struct { para470 ans470 } // para 是参数 // one 代表第一个参数 type para470 struct { } // ans 是答案 // one 代表第一个答案 type ans470 struct { one int } func Test_Problem470(t *testing.T) { qs := []question470{ { para470{}, ans470{2}, }, { para470{}, ans470{0}, }, { para470{}, ans470{1}, }, } fmt.Printf("------------------------Leetcode Problem 470------------------------\n") for _, q := range qs { _, p := q.ans470, q.para470 fmt.Printf("【input】:%v 【output】:%v\n", p, rand10()) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0303.Range-Sum-Query-Immutable/303. Range Sum Query - Immutable_test.go
leetcode/0303.Range-Sum-Query-Immutable/303. Range Sum Query - Immutable_test.go
package leetcode import ( "fmt" "testing" ) func Test_Problem303(t *testing.T) { obj := Constructor303([]int{-2, 0, 3, -5, 2, -1}) fmt.Printf("obj = %v\n", obj) fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 2)) // return 1 fmt.Printf("SumRange(2,5) = %v\n", obj.SumRange(2, 5)) // return -1 fmt.Printf("SumRange(0,5) = %v\n", obj.SumRange(0, 5)) // return -3 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0303.Range-Sum-Query-Immutable/303. Range Sum Query - Immutable.go
leetcode/0303.Range-Sum-Query-Immutable/303. Range Sum Query - Immutable.go
package leetcode import ( "github.com/halfrost/LeetCode-Go/template" ) //解法一 线段树,sumRange 时间复杂度 O(1) // NumArray define type NumArray struct { st *template.SegmentTree } // Constructor303 define func Constructor303(nums []int) NumArray { st := template.SegmentTree{} st.Init(nums, func(i, j int) int { return i + j }) return NumArray{st: &st} } // SumRange define func (ma *NumArray) SumRange(i int, j int) int { return ma.st.Query(i, j) } //解法二 prefixSum,sumRange 时间复杂度 O(1) // // NumArray define // type NumArray struct { // prefixSum []int // } // // Constructor303 define // func Constructor303(nums []int) NumArray { // for i := 1; i < len(nums); i++ { // nums[i] += nums[i-1] // } // return NumArray{prefixSum: nums} // } // // SumRange define // func (this *NumArray) SumRange(i int, j int) int { // if i > 0 { // return this.prefixSum[j] - this.prefixSum[i-1] // } // return this.prefixSum[j] // } /** * Your NumArray object will be instantiated and called as such: * obj := Constructor(nums); * param_1 := obj.SumRange(i,j); */
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0013.Roman-to-Integer/13. Roman to Integer_test.go
leetcode/0013.Roman-to-Integer/13. Roman to Integer_test.go
package leetcode import ( "fmt" "testing" ) type question13 struct { para13 ans13 } // para 是参数 // one 代表第一个参数 type para13 struct { one string } // ans 是答案 // one 代表第一个答案 type ans13 struct { one int } func Test_Problem13(t *testing.T) { qs := []question13{ { para13{"III"}, ans13{3}, }, { para13{"IV"}, ans13{4}, }, { para13{"IX"}, ans13{9}, }, { para13{"LVIII"}, ans13{58}, }, { para13{"MCMXCIV"}, ans13{1994}, }, { para13{"MCMXICIVI"}, ans13{2014}, }, } fmt.Printf("------------------------Leetcode Problem 13------------------------\n") for _, q := range qs { _, p := q.ans13, q.para13 fmt.Printf("【input】:%v 【output】:%v\n", p.one, romanToInt(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0013.Roman-to-Integer/13. Roman to Integer.go
leetcode/0013.Roman-to-Integer/13. Roman to Integer.go
package leetcode var roman = map[string]int{ "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, } func romanToInt(s string) int { if s == "" { return 0 } num, lastint, total := 0, 0, 0 for i := 0; i < len(s); i++ { char := s[len(s)-(i+1) : len(s)-i] num = roman[char] if num < lastint { total = total - num } else { total = total + num } lastint = num } return total }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0461.Hamming-Distance/461. Hamming Distance_test.go
leetcode/0461.Hamming-Distance/461. Hamming Distance_test.go
package leetcode import ( "fmt" "testing" ) type question461 struct { para461 ans461 } // para 是参数 // one 代表第一个参数 type para461 struct { x int y int } // ans 是答案 // one 代表第一个答案 type ans461 struct { one int } func Test_Problem461(t *testing.T) { qs := []question461{ { para461{1, 4}, ans461{2}, }, { para461{1, 1}, ans461{0}, }, { para461{1, 3}, ans461{1}, }, } fmt.Printf("------------------------Leetcode Problem 461------------------------\n") for _, q := range qs { _, p := q.ans461, q.para461 fmt.Printf("【input】:%v 【output】:%v\n", p, hammingDistance(p.x, p.y)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0461.Hamming-Distance/461. Hamming Distance.go
leetcode/0461.Hamming-Distance/461. Hamming Distance.go
package leetcode func hammingDistance(x int, y int) int { distance := 0 for xor := x ^ y; xor != 0; xor &= (xor - 1) { distance++ } return distance }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0559.Maximum-Depth-of-N-ary-Tree/559.Maximum Depth of N-ary Tree_test.go
leetcode/0559.Maximum-Depth-of-N-ary-Tree/559.Maximum Depth of N-ary Tree_test.go
package leetcode import ( "fmt" "testing" ) type question559 struct { para559 ans559 } // para 是参数 type para559 struct { root *Node } // ans 是答案 type ans559 struct { ans int } func Test_Problem559(t *testing.T) { qs := []question559{ { para559{&Node{ Val: 1, Children: []*Node{ {Val: 3, Children: []*Node{{Val: 5, Children: nil}, {Val: 6, Children: nil}}}, {Val: 2, Children: nil}, {Val: 4, Children: nil}, }, }}, ans559{3}, }, { para559{&Node{ Val: 1, Children: []*Node{ {Val: 2, Children: nil}, {Val: 3, Children: []*Node{{Val: 6, Children: nil}, {Val: 7, Children: []*Node{{Val: 11, Children: []*Node{{Val: 14, Children: nil}}}}}}}, {Val: 4, Children: []*Node{{Val: 8, Children: []*Node{{Val: 12, Children: nil}}}}}, {Val: 5, Children: []*Node{{Val: 9, Children: []*Node{{Val: 13, Children: nil}}}, {Val: 10, Children: nil}}}, }, }}, ans559{5}, }, } fmt.Printf("------------------------Leetcode Problem 559------------------------\n") for _, q := range qs { _, p := q.ans559, q.para559 fmt.Printf("【input】:%v 【output】:%v\n", p, maxDepth(p.root)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0559.Maximum-Depth-of-N-ary-Tree/559.Maximum Depth of N-ary Tree.go
leetcode/0559.Maximum-Depth-of-N-ary-Tree/559.Maximum Depth of N-ary Tree.go
package leetcode type Node struct { Val int Children []*Node } func maxDepth(root *Node) int { if root == nil { return 0 } return 1 + bfs(root) } func bfs(root *Node) int { var q []*Node var depth int q = append(q, root.Children...) for len(q) != 0 { depth++ length := len(q) for length != 0 { ele := q[0] q = q[1:] length-- if ele != nil && len(ele.Children) != 0 { q = append(q, ele.Children...) } } } return depth }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0763.Partition-Labels/763. Partition Labels_test.go
leetcode/0763.Partition-Labels/763. Partition Labels_test.go
package leetcode import ( "fmt" "testing" ) type question763 struct { para763 ans763 } // para 是参数 // one 代表第一个参数 type para763 struct { one string } // ans 是答案 // one 代表第一个答案 type ans763 struct { one []int } func Test_Problem763(t *testing.T) { qs := []question763{ { para763{"ababcbacadefegdehijhklij"}, ans763{[]int{9, 7, 8}}, }, } fmt.Printf("------------------------Leetcode Problem 763------------------------\n") for _, q := range qs { _, p := q.ans763, q.para763 fmt.Printf("【input】:%v 【output】:%v\n", p, partitionLabels(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0763.Partition-Labels/763. Partition Labels.go
leetcode/0763.Partition-Labels/763. Partition Labels.go
package leetcode // 解法一 func partitionLabels(S string) []int { var lastIndexOf [26]int for i, v := range S { lastIndexOf[v-'a'] = i } var arr []int for start, end := 0, 0; start < len(S); start = end + 1 { end = lastIndexOf[S[start]-'a'] for i := start; i < end; i++ { if end < lastIndexOf[S[i]-'a'] { end = lastIndexOf[S[i]-'a'] } } arr = append(arr, end-start+1) } return arr } // 解法二 func partitionLabels1(S string) []int { visit, counter, res, sum, lastLength := make([]int, 26), map[byte]int{}, []int{}, 0, 0 for i := 0; i < len(S); i++ { counter[S[i]]++ } for i := 0; i < len(S); i++ { counter[S[i]]-- visit[S[i]-'a'] = 1 sum = 0 for j := 0; j < 26; j++ { if visit[j] == 1 { sum += counter[byte('a'+j)] } } if sum == 0 { res = append(res, i+1-lastLength) lastLength += i + 1 - lastLength } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array/1685. Sum of Absolute Differences in a Sorted Array.go
leetcode/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array/1685. Sum of Absolute Differences in a Sorted Array.go
package leetcode // 解法一 优化版 prefixSum + sufixSum func getSumAbsoluteDifferences(nums []int) []int { size := len(nums) sufixSum := make([]int, size) sufixSum[size-1] = nums[size-1] for i := size - 2; i >= 0; i-- { sufixSum[i] = sufixSum[i+1] + nums[i] } ans, preSum := make([]int, size), 0 for i := 0; i < size; i++ { // 后面可以加到的值 res, sum := 0, sufixSum[i]-nums[i] res += (sum - (size-i-1)*nums[i]) // 前面可以加到的值 res += (i*nums[i] - preSum) ans[i] = res preSum += nums[i] } return ans } // 解法二 prefixSum func getSumAbsoluteDifferences1(nums []int) []int { preSum, res, sum := []int{}, []int{}, nums[0] preSum = append(preSum, nums[0]) for i := 1; i < len(nums); i++ { sum += nums[i] preSum = append(preSum, sum) } for i := 0; i < len(nums); i++ { if i == 0 { res = append(res, preSum[len(nums)-1]-preSum[0]-nums[i]*(len(nums)-1)) } else if i > 0 && i < len(nums)-1 { res = append(res, preSum[len(nums)-1]-preSum[i]-preSum[i-1]+nums[i]*i-nums[i]*(len(nums)-1-i)) } else { res = append(res, nums[i]*len(nums)-preSum[len(nums)-1]) } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array/1685. Sum of Absolute Differences in a Sorted Array_test.go
leetcode/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array/1685. Sum of Absolute Differences in a Sorted Array_test.go
package leetcode import ( "fmt" "testing" ) type question1685 struct { para1685 ans1685 } // para 是参数 // one 代表第一个参数 type para1685 struct { nums []int } // ans 是答案 // one 代表第一个答案 type ans1685 struct { one []int } func Test_Problem1685(t *testing.T) { qs := []question1685{ { para1685{[]int{2, 3, 5}}, ans1685{[]int{4, 3, 5}}, }, { para1685{[]int{1, 4, 6, 8, 10}}, ans1685{[]int{24, 15, 13, 15, 21}}, }, } fmt.Printf("------------------------Leetcode Problem 1685------------------------\n") for _, q := range qs { _, p := q.ans1685, q.para1685 fmt.Printf("【input】:%v 【output】:%v\n", p, getSumAbsoluteDifferences(p.nums)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II_test.go
leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II_test.go
package leetcode import ( "fmt" "testing" ) type question132 struct { para132 ans132 } // para 是参数 // one 代表第一个参数 type para132 struct { s string } // ans 是答案 // one 代表第一个答案 type ans132 struct { one int } func Test_Problem132(t *testing.T) { qs := []question132{ { para132{"aab"}, ans132{1}, }, } fmt.Printf("------------------------Leetcode Problem 132------------------------\n") for _, q := range qs { _, p := q.ans132, q.para132 fmt.Printf("【input】:%v 【output】:%v\n", p, minCut(p.s)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II.go
leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II.go
package leetcode func minCut(s string) int { if s == "" { return 0 } result := len(s) current := make([]string, 0, len(s)) dfs132(s, 0, current, &result) return result } func dfs132(s string, idx int, cur []string, result *int) { start, end := idx, len(s) if start == end { *result = min(*result, len(cur)-1) return } for i := start; i < end; i++ { if isPal(s, start, i) { dfs132(s, i+1, append(cur, s[start:i+1]), result) } } } func min(a int, b int) int { if a > b { return b } return a } func isPal(str string, s, e int) bool { for s < e { if str[s] != str[e] { return false } s++ e-- } return true }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0099.Recover-Binary-Search-Tree/99. Recover Binary Search Tree.go
leetcode/0099.Recover-Binary-Search-Tree/99. Recover Binary Search Tree.go
package leetcode import ( "github.com/halfrost/LeetCode-Go/structures" ) // TreeNode define type TreeNode = structures.TreeNode /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func recoverTree(root *TreeNode) { var prev, target1, target2 *TreeNode _, target1, target2 = inOrderTraverse(root, prev, target1, target2) if target1 != nil && target2 != nil { target1.Val, target2.Val = target2.Val, target1.Val } } func inOrderTraverse(root, prev, target1, target2 *TreeNode) (*TreeNode, *TreeNode, *TreeNode) { if root == nil { return prev, target1, target2 } prev, target1, target2 = inOrderTraverse(root.Left, prev, target1, target2) if prev != nil && prev.Val > root.Val { if target1 == nil { target1 = prev } target2 = root } prev = root prev, target1, target2 = inOrderTraverse(root.Right, prev, target1, target2) return prev, target1, target2 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0099.Recover-Binary-Search-Tree/99. Recover Binary Search Tree_test.go
leetcode/0099.Recover-Binary-Search-Tree/99. Recover Binary Search Tree_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question99 struct { para99 ans99 } // para 是参数 // one 代表第一个参数 type para99 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans99 struct { one []int } func Test_Problem99(t *testing.T) { qs := []question99{ { para99{[]int{1, 3, structures.NULL, structures.NULL, 2}}, ans99{[]int{3, 1, structures.NULL, structures.NULL, 2}}, }, { para99{[]int{3, 1, 4, structures.NULL, structures.NULL, 2}}, ans99{[]int{2, 1, 4, structures.NULL, structures.NULL, 3}}, }, } fmt.Printf("------------------------Leetcode Problem 99------------------------\n") for _, q := range qs { _, p := q.ans99, q.para99 fmt.Printf("【input】:%v ", p) rootOne := structures.Ints2TreeNode(p.one) recoverTree(rootOne) fmt.Printf("【output】:%v \n", p) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0264.Ugly-Number-II/264. Ugly Number II_test.go
leetcode/0264.Ugly-Number-II/264. Ugly Number II_test.go
package leetcode import ( "fmt" "testing" ) type question264 struct { para264 ans264 } // para 是参数 // one 代表第一个参数 type para264 struct { one int } // ans 是答案 // one 代表第一个答案 type ans264 struct { one int } func Test_Problem264(t *testing.T) { qs := []question264{ { para264{10}, ans264{12}, }, { para264{1}, ans264{1}, }, { para264{6}, ans264{6}, }, { para264{8}, ans264{9}, }, { para264{14}, ans264{20}, }, } fmt.Printf("------------------------Leetcode Problem 264------------------------\n") for _, q := range qs { _, p := q.ans264, q.para264 fmt.Printf("【input】:%v 【output】:%v\n", p, nthUglyNumber(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0264.Ugly-Number-II/264. Ugly Number II.go
leetcode/0264.Ugly-Number-II/264. Ugly Number II.go
package leetcode func nthUglyNumber(n int) int { dp, p2, p3, p5 := make([]int, n+1), 1, 1, 1 dp[0], dp[1] = 0, 1 for i := 2; i <= n; i++ { x2, x3, x5 := dp[p2]*2, dp[p3]*3, dp[p5]*5 dp[i] = min(min(x2, x3), x5) if dp[i] == x2 { p2++ } if dp[i] == x3 { p3++ } if dp[i] == x5 { p5++ } } return dp[n] } func min(a, b int) int { if a < b { return a } return b }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1690.Stone-Game-VII/1690. Stone Game VII_test.go
leetcode/1690.Stone-Game-VII/1690. Stone Game VII_test.go
package leetcode import ( "fmt" "testing" ) type question1690 struct { para1690 ans1690 } // para 是参数 // one 代表第一个参数 type para1690 struct { stones []int } // ans 是答案 // one 代表第一个答案 type ans1690 struct { one int } func Test_Problem1690(t *testing.T) { qs := []question1690{ { para1690{[]int{5, 3, 1, 4, 2}}, ans1690{6}, }, { para1690{[]int{7, 90, 5, 1, 100, 10, 10, 2}}, ans1690{122}, }, } fmt.Printf("------------------------Leetcode Problem 1690------------------------\n") for _, q := range qs { _, p := q.ans1690, q.para1690 fmt.Printf("【input】:%v 【output】:%v\n", p, stoneGameVII(p.stones)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1690.Stone-Game-VII/1690. Stone Game VII.go
leetcode/1690.Stone-Game-VII/1690. Stone Game VII.go
package leetcode // 解法一 优化空间版 DP func stoneGameVII(stones []int) int { n := len(stones) sum := make([]int, n) dp := make([]int, n) for i, d := range stones { sum[i] = d } for i := 1; i < n; i++ { for j := 0; j+i < n; j++ { if (n-i)%2 == 1 { d0 := dp[j] + sum[j] d1 := dp[j+1] + sum[j+1] if d0 > d1 { dp[j] = d0 } else { dp[j] = d1 } } else { d0 := dp[j] - sum[j] d1 := dp[j+1] - sum[j+1] if d0 < d1 { dp[j] = d0 } else { dp[j] = d1 } } sum[j] = sum[j] + stones[i+j] } } return dp[0] } // 解法二 常规 DP func stoneGameVII1(stones []int) int { prefixSum := make([]int, len(stones)) for i := 0; i < len(stones); i++ { if i == 0 { prefixSum[i] = stones[i] } else { prefixSum[i] = prefixSum[i-1] + stones[i] } } dp := make([][]int, len(stones)) for i := range dp { dp[i] = make([]int, len(stones)) dp[i][i] = 0 } n := len(stones) for l := 2; l <= n; l++ { for i := 0; i+l <= n; i++ { dp[i][i+l-1] = max(prefixSum[i+l-1]-prefixSum[i+1]+stones[i+1]-dp[i+1][i+l-1], prefixSum[i+l-2]-prefixSum[i]+stones[i]-dp[i][i+l-2]) } } return dp[0][n-1] } func max(a, b int) int { if a > b { return a } return b }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0104.Maximum-Depth-of-Binary-Tree/104. Maximum Depth of Binary Tree_test.go
leetcode/0104.Maximum-Depth-of-Binary-Tree/104. Maximum Depth of Binary Tree_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question104 struct { para104 ans104 } // para 是参数 // one 代表第一个参数 type para104 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans104 struct { one int } func Test_Problem104(t *testing.T) { qs := []question104{ { para104{[]int{}}, ans104{0}, }, { para104{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}}, ans104{3}, }, } fmt.Printf("------------------------Leetcode Problem 104------------------------\n") for _, q := range qs { _, p := q.ans104, q.para104 fmt.Printf("【input】:%v ", p) root := structures.Ints2TreeNode(p.one) fmt.Printf("【output】:%v \n", maxDepth(root)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0104.Maximum-Depth-of-Binary-Tree/104. Maximum Depth of Binary Tree.go
leetcode/0104.Maximum-Depth-of-Binary-Tree/104. Maximum Depth of Binary Tree.go
package leetcode import ( "github.com/halfrost/LeetCode-Go/structures" ) // TreeNode define type TreeNode = structures.TreeNode /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func maxDepth(root *TreeNode) int { if root == nil { return 0 } return max(maxDepth(root.Left), maxDepth(root.Right)) + 1 } func max(a int, b int) int { if a > b { return a } return b }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0168.Excel-Sheet-Column-Title/168. Excel Sheet Column Title_test.go
leetcode/0168.Excel-Sheet-Column-Title/168. Excel Sheet Column Title_test.go
package leetcode import ( "fmt" "testing" ) type question168 struct { para168 ans168 } // para 是参数 // one 代表第一个参数 type para168 struct { n int } // ans 是答案 // one 代表第一个答案 type ans168 struct { one string } func Test_Problem168(t *testing.T) { qs := []question168{ { para168{1}, ans168{"A"}, }, { para168{28}, ans168{"AB"}, }, { para168{701}, ans168{"ZY"}, }, { para168{10011}, ans168{"NUA"}, }, { para168{999}, ans168{"ALK"}, }, { para168{681}, ans168{"ZE"}, }, } fmt.Printf("------------------------Leetcode Problem 168------------------------\n") for _, q := range qs { _, p := q.ans168, q.para168 fmt.Printf("【input】:%v 【output】:%v\n", p, convertToTitle(p.n)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0168.Excel-Sheet-Column-Title/168. Excel Sheet Column Title.go
leetcode/0168.Excel-Sheet-Column-Title/168. Excel Sheet Column Title.go
package leetcode func convertToTitle(n int) string { result := []byte{} for n > 0 { result = append(result, 'A'+byte((n-1)%26)) n = (n - 1) / 26 } for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 { result[i], result[j] = result[j], result[i] } return string(result) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0981.Time-Based-Key-Value-Store/981. Time Based Key-Value Store.go
leetcode/0981.Time-Based-Key-Value-Store/981. Time Based Key-Value Store.go
package leetcode import "sort" type data struct { time int value string } // TimeMap is a timebased key-value store // TimeMap define type TimeMap map[string][]data // Constructor981 define func Constructor981() TimeMap { return make(map[string][]data, 1024) } // Set define func (t TimeMap) Set(key string, value string, timestamp int) { if _, ok := t[key]; !ok { t[key] = make([]data, 1, 1024) } t[key] = append(t[key], data{ time: timestamp, value: value, }) } // Get define func (t TimeMap) Get(key string, timestamp int) string { d := t[key] i := sort.Search(len(d), func(i int) bool { return timestamp < d[i].time }) i-- return t[key][i].value } /** * Your TimeMap object will be instantiated and called as such: * obj := Constructor(); * obj.Set(key,value,timestamp); * param_2 := obj.Get(key,timestamp); */
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0981.Time-Based-Key-Value-Store/981. Time Based Key-Value Store_test.go
leetcode/0981.Time-Based-Key-Value-Store/981. Time Based Key-Value Store_test.go
package leetcode import ( "fmt" "testing" ) func Test_Problem981(t *testing.T) { obj := Constructor981() obj.Set("foo", "bar", 1) fmt.Printf("Get = %v\n", obj.Get("foo", 1)) fmt.Printf("Get = %v\n", obj.Get("foo", 3)) obj.Set("foo", "bar2", 4) fmt.Printf("Get = %v\n", obj.Get("foo", 4)) fmt.Printf("Get = %v\n", obj.Get("foo", 5)) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0191.Number-of-1-Bits/191. Number of 1 Bits.go
leetcode/0191.Number-of-1-Bits/191. Number of 1 Bits.go
package leetcode import "math/bits" // 解法一 func hammingWeight(num uint32) int { return bits.OnesCount(uint(num)) } // 解法二 func hammingWeight1(num uint32) int { count := 0 for num != 0 { num = num & (num - 1) count++ } return count }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0191.Number-of-1-Bits/191. Number of 1 Bits_test.go
leetcode/0191.Number-of-1-Bits/191. Number of 1 Bits_test.go
package leetcode import ( "fmt" "strconv" "testing" ) type question191 struct { para191 ans191 } // para 是参数 // one 代表第一个参数 type para191 struct { one uint32 } // ans 是答案 // one 代表第一个答案 type ans191 struct { one int } func Test_Problem191(t *testing.T) { qs := []question191{ { para191{5}, ans191{1}, }, { para191{13}, ans191{2}, }, } fmt.Printf("------------------------Leetcode Problem 191------------------------\n") for _, q := range qs { _, p := q.ans191, q.para191 input := strconv.FormatUint(uint64(p.one), 2) // 32位无符号整数转换为二进制字符串 input = fmt.Sprintf("%0*v", 32, input) // 格式化输出32位,保留前置0 fmt.Printf("【input】:%v 【output】:%v\n", input, hammingWeight(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0977.Squares-of-a-Sorted-Array/977. Squares of a Sorted Array.go
leetcode/0977.Squares-of-a-Sorted-Array/977. Squares of a Sorted Array.go
package leetcode import "sort" // 解法一 func sortedSquares(A []int) []int { ans := make([]int, len(A)) for i, k, j := 0, len(A)-1, len(ans)-1; i <= j; k-- { if A[i]*A[i] > A[j]*A[j] { ans[k] = A[i] * A[i] i++ } else { ans[k] = A[j] * A[j] j-- } } return ans } // 解法二 func sortedSquares1(A []int) []int { for i, value := range A { A[i] = value * value } sort.Ints(A) return A }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0977.Squares-of-a-Sorted-Array/977. Squares of a Sorted Array_test.go
leetcode/0977.Squares-of-a-Sorted-Array/977. Squares of a Sorted Array_test.go
package leetcode import ( "fmt" "testing" ) type question977 struct { para977 ans977 } // para 是参数 // one 代表第一个参数 type para977 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans977 struct { one []int } func Test_Problem977(t *testing.T) { qs := []question977{ { para977{[]int{-4, -1, 0, 3, 10}}, ans977{[]int{0, 1, 9, 16, 100}}, }, { para977{[]int{1}}, ans977{[]int{1}}, }, { para977{[]int{-7, -3, 2, 3, 11}}, ans977{[]int{4, 9, 9, 49, 121}}, }, } fmt.Printf("------------------------Leetcode Problem 977------------------------\n") for _, q := range qs { _, p := q.ans977, q.para977 fmt.Printf("【input】:%v 【output】:%v\n", p, sortedSquares(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0338.Counting-Bits/338. Counting Bits_test.go
leetcode/0338.Counting-Bits/338. Counting Bits_test.go
package leetcode import ( "fmt" "testing" ) type question338 struct { para338 ans338 } // para 是参数 // one 代表第一个参数 type para338 struct { one int } // ans 是答案 // one 代表第一个答案 type ans338 struct { one []int } func Test_Problem338(t *testing.T) { qs := []question338{ { para338{2}, ans338{[]int{0, 1, 1}}, }, { para338{5}, ans338{[]int{0, 1, 1, 2, 1, 2}}, }, } fmt.Printf("------------------------Leetcode Problem 338------------------------\n") for _, q := range qs { _, p := q.ans338, q.para338 fmt.Printf("【input】:%v 【output】:%v\n", p, countBits(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0338.Counting-Bits/338. Counting Bits.go
leetcode/0338.Counting-Bits/338. Counting Bits.go
package leetcode func countBits(num int) []int { bits := make([]int, num+1) for i := 1; i <= num; i++ { bits[i] += bits[i&(i-1)] + 1 } return bits }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0880.Decoded-String-at-Index/880. Decoded String at Index_test.go
leetcode/0880.Decoded-String-at-Index/880. Decoded String at Index_test.go
package leetcode import ( "fmt" "testing" ) type question880 struct { para880 ans880 } // para 是参数 // one 代表第一个参数 type para880 struct { s string k int } // ans 是答案 // one 代表第一个答案 type ans880 struct { one string } func Test_Problem880(t *testing.T) { qs := []question880{ { para880{"aw4eguc6cs", 41}, ans880{"a"}, }, { para880{"vk6u5xhq9v", 554}, ans880{"h"}, }, { para880{"leet2code3", 10}, ans880{"o"}, }, { para880{"ha22", 5}, ans880{"h"}, }, { para880{"a2345678999999999999999", 1}, ans880{"a"}, }, { para880{"y959q969u3hb22odq595", 222280369}, ans880{"q"}, }, } fmt.Printf("------------------------Leetcode Problem 880------------------------\n") for _, q := range qs { _, p := q.ans880, q.para880 fmt.Printf("【input】:%v 【output】:%v\n", p, decodeAtIndex(p.s, p.k)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0880.Decoded-String-at-Index/880. Decoded String at Index.go
leetcode/0880.Decoded-String-at-Index/880. Decoded String at Index.go
package leetcode func isLetter(char byte) bool { if char >= 'a' && char <= 'z' { return true } return false } func decodeAtIndex(S string, K int) string { length := 0 for i := 0; i < len(S); i++ { if isLetter(S[i]) { length++ if length == K { return string(S[i]) } } else { if length*int(S[i]-'0') >= K { if K%length != 0 { return decodeAtIndex(S[:i], K%length) } return decodeAtIndex(S[:i], length) } length *= int(S[i] - '0') } } return "" }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0210.Course-Schedule-II/210. Course Schedule II.go
leetcode/0210.Course-Schedule-II/210. Course Schedule II.go
package leetcode func findOrder(numCourses int, prerequisites [][]int) []int { in := make([]int, numCourses) frees := make([][]int, numCourses) next := make([]int, 0, numCourses) for _, v := range prerequisites { in[v[0]]++ frees[v[1]] = append(frees[v[1]], v[0]) } for i := 0; i < numCourses; i++ { if in[i] == 0 { next = append(next, i) } } for i := 0; i != len(next); i++ { c := next[i] v := frees[c] for _, vv := range v { in[vv]-- if in[vv] == 0 { next = append(next, vv) } } } if len(next) == numCourses { return next } return []int{} }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0210.Course-Schedule-II/210. Course Schedule II_test.go
leetcode/0210.Course-Schedule-II/210. Course Schedule II_test.go
package leetcode import ( "fmt" "testing" ) type question210 struct { para210 ans210 } // para 是参数 // one 代表第一个参数 type para210 struct { one int pre [][]int } // ans 是答案 // one 代表第一个答案 type ans210 struct { one []int } func Test_Problem210(t *testing.T) { qs := []question210{ { para210{2, [][]int{{1, 0}}}, ans210{[]int{0, 1}}, }, { para210{2, [][]int{{1, 0}, {0, 1}}}, ans210{[]int{0, 1, 2, 3}}, }, { para210{4, [][]int{{1, 0}, {2, 0}, {3, 1}, {3, 2}}}, ans210{[]int{0, 1, 2, 3}}, }, { para210{3, [][]int{{1, 0}, {1, 2}, {0, 1}}}, ans210{[]int{}}, }, } fmt.Printf("------------------------Leetcode Problem 210------------------------\n") for _, q := range qs { _, p := q.ans210, q.para210 fmt.Printf("【input】:%v 【output】:%v\n", p, findOrder(p.one, p.pre)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1018.Binary-Prefix-Divisible-By-5/1018. Binary Prefix Divisible By 5.go
leetcode/1018.Binary-Prefix-Divisible-By-5/1018. Binary Prefix Divisible By 5.go
package leetcode func prefixesDivBy5(a []int) []bool { res, num := make([]bool, len(a)), 0 for i, v := range a { num = (num<<1 | v) % 5 res[i] = num == 0 } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1018.Binary-Prefix-Divisible-By-5/1018. Binary Prefix Divisible By 5_test.go
leetcode/1018.Binary-Prefix-Divisible-By-5/1018. Binary Prefix Divisible By 5_test.go
package leetcode import ( "fmt" "testing" ) type question1018 struct { para1018 ans1018 } // para 是参数 // one 代表第一个参数 type para1018 struct { a []int } // ans 是答案 // one 代表第一个答案 type ans1018 struct { one []bool } func Test_Problem1018(t *testing.T) { qs := []question1018{ { para1018{[]int{0, 1, 1}}, ans1018{[]bool{true, false, false}}, }, { para1018{[]int{1, 1, 1}}, ans1018{[]bool{false, false, false}}, }, { para1018{[]int{0, 1, 1, 1, 1, 1}}, ans1018{[]bool{true, false, false, false, true, false}}, }, { para1018{[]int{1, 1, 1, 0, 1}}, ans1018{[]bool{false, false, false, false, false}}, }, } fmt.Printf("------------------------Leetcode Problem 1018------------------------\n") for _, q := range qs { _, p := q.ans1018, q.para1018 fmt.Printf("【input】:%v 【output】:%v\n", p, prefixesDivBy5(p.a)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0793.Preimage-Size-of-Factorial-Zeroes-Function/793. Preimage Size of Factorial Zeroes Function.go
leetcode/0793.Preimage-Size-of-Factorial-Zeroes-Function/793. Preimage Size of Factorial Zeroes Function.go
package leetcode // 解法一 二分搜索 func preimageSizeFZF(K int) int { low, high := 0, 5*K for low <= high { mid := low + (high-low)>>1 k := trailingZeroes(mid) if k == K { return 5 } else if k > K { high = mid - 1 } else { low = mid + 1 } } return 0 } func trailingZeroes(n int) int { if n/5 == 0 { return 0 } return n/5 + trailingZeroes(n/5) } // 解法二 数学方法 func preimageSizeFZF1(K int) int { base := 0 for base < K { base = base*5 + 1 } for K > 0 { base = (base - 1) / 5 if K/base == 5 { return 0 } K %= base } return 5 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0793.Preimage-Size-of-Factorial-Zeroes-Function/793. Preimage Size of Factorial Zeroes Function_test.go
leetcode/0793.Preimage-Size-of-Factorial-Zeroes-Function/793. Preimage Size of Factorial Zeroes Function_test.go
package leetcode import ( "fmt" "testing" ) type question793 struct { para793 ans793 } // para 是参数 // one 代表第一个参数 type para793 struct { one int } // ans 是答案 // one 代表第一个答案 type ans793 struct { one int } func Test_Problem793(t *testing.T) { qs := []question793{ { para793{0}, ans793{5}, }, { para793{5}, ans793{0}, }, } fmt.Printf("------------------------Leetcode Problem 793------------------------\n") for _, q := range qs { _, p := q.ans793, q.para793 fmt.Printf("【input】:%v 【output】:%v\n", p, preimageSizeFZF(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0215.Kth-Largest-Element-in-an-Array/215. Kth Largest Element in an Array.go
leetcode/0215.Kth-Largest-Element-in-an-Array/215. Kth Largest Element in an Array.go
package leetcode import ( "math/rand" "sort" ) // 解法一 排序,排序的方法反而速度是最快的 func findKthLargest1(nums []int, k int) int { sort.Ints(nums) return nums[len(nums)-k] } // 解法二 这个方法的理论依据是 partition 得到的点的下标就是最终排序之后的下标,根据这个下标,我们可以判断第 K 大的数在哪里 // 时间复杂度 O(n),空间复杂度 O(log n),最坏时间复杂度为 O(n^2),空间复杂度 O(n) func findKthLargest(nums []int, k int) int { m := len(nums) - k + 1 // mth smallest, from 1..len(nums) return selectSmallest(nums, 0, len(nums)-1, m) } func selectSmallest(nums []int, l, r, i int) int { if l >= r { return nums[l] } q := partition(nums, l, r) k := q - l + 1 if k == i { return nums[q] } if i < k { return selectSmallest(nums, l, q-1, i) } else { return selectSmallest(nums, q+1, r, i-k) } } func partition(nums []int, l, r int) int { k := l + rand.Intn(r-l+1) // 此处为优化,使得时间复杂度期望降为 O(n),最坏时间复杂度为 O(n^2) nums[k], nums[r] = nums[r], nums[k] i := l - 1 // nums[l..i] <= nums[r] // nums[i+1..j-1] > nums[r] for j := l; j < r; j++ { if nums[j] <= nums[r] { i++ nums[i], nums[j] = nums[j], nums[i] } } nums[i+1], nums[r] = nums[r], nums[i+1] return i + 1 } // 扩展题 剑指 Offer 40. 最小的 k 个数 func getLeastNumbers(arr []int, k int) []int { return selectSmallest1(arr, 0, len(arr)-1, k)[:k] } // 和 selectSmallest 实现完全一致,只是返回值不用再截取了,直接返回 nums 即可 func selectSmallest1(nums []int, l, r, i int) []int { if l >= r { return nums } q := partition(nums, l, r) k := q - l + 1 if k == i { return nums } if i < k { return selectSmallest1(nums, l, q-1, i) } else { return selectSmallest1(nums, q+1, r, i-k) } }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0215.Kth-Largest-Element-in-an-Array/215. Kth Largest Element in an Array_test.go
leetcode/0215.Kth-Largest-Element-in-an-Array/215. Kth Largest Element in an Array_test.go
package leetcode import ( "fmt" "testing" ) type question215 struct { para215 ans215 } // para 是参数 // one 代表第一个参数 type para215 struct { one []int two int } // ans 是答案 // one 代表第一个答案 type ans215 struct { one int } func Test_Problem215(t *testing.T) { qs := []question215{ { para215{[]int{3, 2, 1}, 2}, ans215{2}, }, { para215{[]int{3, 2, 1, 5, 6, 4}, 2}, ans215{5}, }, { para215{[]int{3, 2, 3, 1, 2, 4, 5, 5, 6}, 4}, ans215{4}, }, { para215{[]int{0, 0, 0, 0, 0}, 2}, ans215{0}, }, { para215{[]int{1}, 1}, ans215{1}, }, { para215{[]int{3, 2, 3, 1, 2, 4, 5, 5, 6, 7, 7, 8, 2, 3, 1, 1, 1, 10, 11, 5, 6, 2, 4, 7, 8, 5, 6}, 20}, ans215{2}, }, } fmt.Printf("------------------------Leetcode Problem 215------------------------\n") for _, q := range qs { _, p := q.ans215, q.para215 fmt.Printf("【input】:%v 【output】:%v\n", p.one, findKthLargest(p.one, p.two)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring.go
leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring.go
package leetcode // 解法一 二分搜索 + Rabin-Karp func longestDupSubstring(S string) string { // low, high := 0, len(S) // for low < high { // mid := (low + high + 1) >> 1 // if hasRepeated("", B, mid) { // low = mid // } else { // high = mid - 1 // } // } return "这个解法还有问题!" } // func hashSlice(arr []int, length int) []int { // // hash 数组里面记录 arr 比 length 长出去部分的 hash 值 // hash, pl, h := make([]int, len(arr)-length+1), 1, 0 // for i := 0; i < length-1; i++ { // pl *= primeRK // } // for i, v := range arr { // h = h*primeRK + v // if i >= length-1 { // hash[i-length+1] = h // h -= pl * arr[i-length+1] // } // } // return hash // } // func hasSamePrefix(A, B []int, length int) bool { // for i := 0; i < length; i++ { // if A[i] != B[i] { // return false // } // } // return true // } // func hasRepeated(A, B []int, length int) bool { // hs := hashSlice(A, length) // hashToOffset := make(map[int][]int, len(hs)) // for i, h := range hs { // hashToOffset[h] = append(hashToOffset[h], i) // } // for i, h := range hashSlice(B, length) { // if offsets, ok := hashToOffset[h]; ok { // for _, offset := range offsets { // if hasSamePrefix(A[offset:], B[i:], length) { // return true // } // } // } // } // return false // } // 解法二 二分搜索 + 暴力匹配 func longestDupSubstring1(S string) string { res := "" low, high := 0, len(S) for low < high { mid := low + (high-low)>>1 if isDuplicate(mid, S, &res) { low = mid + 1 } else { high = mid } } return res } func isDuplicate(length int, str string, res *string) bool { visited := map[string]bool{} for i := 0; i+length <= len(str); i++ { subStr := str[i : i+length] if visited[subStr] { *res = subStr return true } visited[subStr] = true } return false }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring_test.go
leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring_test.go
package leetcode import ( "fmt" "testing" ) type question1044 struct { para1044 ans1044 } // para 是参数 // one 代表第一个参数 type para1044 struct { one string } // ans 是答案 // one 代表第一个答案 type ans1044 struct { one string } func Test_Problem1044(t *testing.T) { qs := []question1044{ { para1044{"banana"}, ans1044{"ana"}, }, { para1044{"abcd"}, ans1044{""}, }, } fmt.Printf("------------------------Leetcode Problem 1044------------------------\n") for _, q := range qs { _, p := q.ans1044, q.para1044 fmt.Printf("【input】:%v 【output】:%v\n", p, longestDupSubstring(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1658.Minimum-Operations-to-Reduce-X-to-Zero/1658. Minimum Operations to Reduce X to Zero.go
leetcode/1658.Minimum-Operations-to-Reduce-X-to-Zero/1658. Minimum Operations to Reduce X to Zero.go
package leetcode func minOperations(nums []int, x int) int { total := 0 for _, n := range nums { total += n } target := total - x if target < 0 { return -1 } if target == 0 { return len(nums) } left, right, sum, res := 0, 0, 0, -1 for right < len(nums) { if sum < target { sum += nums[right] right++ } for sum >= target { if sum == target { res = max(res, right-left) } sum -= nums[left] left++ } } if res == -1 { return -1 } return len(nums) - res } func max(a, b int) int { if a > b { return a } return b }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1658.Minimum-Operations-to-Reduce-X-to-Zero/1658. Minimum Operations to Reduce X to Zero_test.go
leetcode/1658.Minimum-Operations-to-Reduce-X-to-Zero/1658. Minimum Operations to Reduce X to Zero_test.go
package leetcode import ( "fmt" "testing" ) type question1658 struct { para1658 ans1658 } // para 是参数 // one 代表第一个参数 type para1658 struct { nums []int x int } // ans 是答案 // one 代表第一个答案 type ans1658 struct { one int } func Test_Problem1658(t *testing.T) { qs := []question1658{ { para1658{[]int{1, 1, 4, 2, 3}, 5}, ans1658{2}, }, { para1658{[]int{5, 6, 7, 8, 9}, 4}, ans1658{-1}, }, { para1658{[]int{3, 2, 20, 1, 1, 3}, 10}, ans1658{5}, }, } fmt.Printf("------------------------Leetcode Problem 1658------------------------\n") for _, q := range qs { _, p := q.ans1658, q.para1658 fmt.Printf("【input】:%v 【output】:%v \n", p, minOperations(p.nums, p.x)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0227.Basic-Calculator-II/227. Basic Calculator II_test.go
leetcode/0227.Basic-Calculator-II/227. Basic Calculator II_test.go
package leetcode import ( "fmt" "testing" ) type question227 struct { para227 ans227 } // para 是参数 // one 代表第一个参数 type para227 struct { one string } // ans 是答案 // one 代表第一个答案 type ans227 struct { one int } func Test_Problem227(t *testing.T) { qs := []question227{ { para227{"3+2*2"}, ans227{7}, }, { para227{"3/2"}, ans227{1}, }, { para227{" 3+5 / 2 "}, ans227{5}, }, { para227{"1 + 1"}, ans227{2}, }, { para227{" 2-1 + 2 "}, ans227{3}, }, { para227{"2-5/6"}, ans227{2}, }, } fmt.Printf("------------------------Leetcode Problem 227------------------------\n") for _, q := range qs { _, p := q.ans227, q.para227 fmt.Printf("【input】:%v 【output】:%v\n", p, calculate(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0227.Basic-Calculator-II/227. Basic Calculator II.go
leetcode/0227.Basic-Calculator-II/227. Basic Calculator II.go
package leetcode func calculate(s string) int { stack, preSign, num, res := []int{}, '+', 0, 0 for i, ch := range s { isDigit := '0' <= ch && ch <= '9' if isDigit { num = num*10 + int(ch-'0') } if !isDigit && ch != ' ' || i == len(s)-1 { switch preSign { case '+': stack = append(stack, num) case '-': stack = append(stack, -num) case '*': stack[len(stack)-1] *= num default: stack[len(stack)-1] /= num } preSign = ch num = 0 } } for _, v := range stack { res += v } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1680.Concatenation-of-Consecutive-Binary-Numbers/1680. Concatenation of Consecutive Binary Numbers_test.go
leetcode/1680.Concatenation-of-Consecutive-Binary-Numbers/1680. Concatenation of Consecutive Binary Numbers_test.go
package leetcode import ( "fmt" "testing" ) type question1680 struct { para1680 ans1680 } // para 是参数 // one 代表第一个参数 type para1680 struct { n int } // ans 是答案 // one 代表第一个答案 type ans1680 struct { one int } func Test_Problem1680(t *testing.T) { qs := []question1680{ { para1680{1}, ans1680{1}, }, { para1680{3}, ans1680{27}, }, { para1680{12}, ans1680{505379714}, }, { para1680{42}, ans1680{727837408}, }, { para1680{24}, ans1680{385951001}, }, { para1680{81}, ans1680{819357292}, }, { para1680{66}, ans1680{627730462}, }, } fmt.Printf("------------------------Leetcode Problem 1680------------------------\n") for _, q := range qs { _, p := q.ans1680, q.para1680 fmt.Printf("【input】:%v 【output】:%v\n", p, concatenatedBinary(p.n)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1680.Concatenation-of-Consecutive-Binary-Numbers/1680. Concatenation of Consecutive Binary Numbers.go
leetcode/1680.Concatenation-of-Consecutive-Binary-Numbers/1680. Concatenation of Consecutive Binary Numbers.go
package leetcode import ( "math/bits" ) // 解法一 模拟 func concatenatedBinary(n int) int { res, mod, shift := 0, 1000000007, 0 for i := 1; i <= n; i++ { if (i & (i - 1)) == 0 { shift++ } res = ((res << shift) + i) % mod } return res } // 解法二 位运算 func concatenatedBinary1(n int) int { res := 0 for i := 1; i <= n; i++ { res = (res<<bits.Len(uint(i)) | i) % (1e9 + 7) } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0119.Pascals-Triangle-II/119. Pascals Triangle II_test.go
leetcode/0119.Pascals-Triangle-II/119. Pascals Triangle II_test.go
package leetcode import ( "fmt" "testing" ) type question119 struct { para119 ans119 } // para 是参数 // one 代表第一个参数 type para119 struct { rowIndex int } // ans 是答案 // one 代表第一个答案 type ans119 struct { one []int } func Test_Problem119(t *testing.T) { qs := []question119{ { para119{3}, ans119{[]int{1, 3, 3, 1}}, }, { para119{0}, ans119{[]int{1}}, }, } fmt.Printf("------------------------Leetcode Problem 119------------------------\n") for _, q := range qs { _, p := q.ans119, q.para119 fmt.Printf("【input】:%v 【output】:%v\n", p, getRow(p.rowIndex)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0119.Pascals-Triangle-II/119. Pascals Triangle II.go
leetcode/0119.Pascals-Triangle-II/119. Pascals Triangle II.go
package leetcode func getRow(rowIndex int) []int { row := make([]int, rowIndex+1) row[0] = 1 for i := 1; i <= rowIndex; i++ { row[i] = row[i-1] * (rowIndex - i + 1) / i } return row }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0526.Beautiful-Arrangement/526. Beautiful Arrangement.go
leetcode/0526.Beautiful-Arrangement/526. Beautiful Arrangement.go
package leetcode // 解法一 暴力打表法 func countArrangement1(N int) int { res := []int{0, 1, 2, 3, 8, 10, 36, 41, 132, 250, 700, 750, 4010, 4237, 10680, 24679, 87328, 90478, 435812} return res[N] } // 解法二 DFS 回溯 func countArrangement(N int) int { if N == 0 { return 0 } nums, used, p, res := make([]int, N), make([]bool, N), []int{}, [][]int{} for i := range nums { nums[i] = i + 1 } generatePermutation526(nums, 0, p, &res, &used) return len(res) } func generatePermutation526(nums []int, index int, p []int, res *[][]int, used *[]bool) { if index == len(nums) { temp := make([]int, len(p)) copy(temp, p) *res = append(*res, temp) return } for i := 0; i < len(nums); i++ { if !(*used)[i] { if !(checkDivisible(nums[i], len(p)+1) || checkDivisible(len(p)+1, nums[i])) { // 关键的剪枝条件 continue } (*used)[i] = true p = append(p, nums[i]) generatePermutation526(nums, index+1, p, res, used) p = p[:len(p)-1] (*used)[i] = false } } return } func checkDivisible(num, d int) bool { tmp := num / d if int(tmp)*int(d) == num { return true } return false }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0526.Beautiful-Arrangement/526. Beautiful Arrangement_test.go
leetcode/0526.Beautiful-Arrangement/526. Beautiful Arrangement_test.go
package leetcode import ( "fmt" "testing" ) type question526 struct { para526 ans526 } // para 是参数 // one 代表第一个参数 type para526 struct { one int } // ans 是答案 // one 代表第一个答案 type ans526 struct { one int } func Test_Problem526(t *testing.T) { qs := []question526{ { para526{1}, ans526{1}, }, { para526{2}, ans526{2}, }, { para526{3}, ans526{3}, }, { para526{4}, ans526{8}, }, { para526{5}, ans526{10}, }, { para526{6}, ans526{36}, }, { para526{7}, ans526{41}, }, { para526{8}, ans526{132}, }, { para526{9}, ans526{250}, }, { para526{10}, ans526{700}, }, { para526{11}, ans526{750}, }, { para526{12}, ans526{4010}, }, { para526{13}, ans526{4237}, }, { para526{14}, ans526{10680}, }, { para526{15}, ans526{24679}, }, { para526{16}, ans526{87328}, }, { para526{17}, ans526{90478}, }, { para526{18}, ans526{435812}, }, // 如需多个测试,可以复制上方元素。 } fmt.Printf("------------------------Leetcode Problem 526------------------------\n") for _, q := range qs { _, p := q.ans526, q.para526 fmt.Printf("【input】:%v 【output】:%v\n", p, countArrangement(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0300.Longest-Increasing-Subsequence/300. Longest Increasing Subsequence_test.go
leetcode/0300.Longest-Increasing-Subsequence/300. Longest Increasing Subsequence_test.go
package leetcode import ( "fmt" "testing" ) type question300 struct { para300 ans300 } // para 是参数 // one 代表第一个参数 type para300 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans300 struct { one int } func Test_Problem300(t *testing.T) { qs := []question300{ { para300{[]int{10, 9, 2, 5, 3, 7, 101, 18}}, ans300{4}, }, } fmt.Printf("------------------------Leetcode Problem 300------------------------\n") for _, q := range qs { _, p := q.ans300, q.para300 fmt.Printf("【input】:%v 【output】:%v\n", p, lengthOfLIS(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0300.Longest-Increasing-Subsequence/300. Longest Increasing Subsequence.go
leetcode/0300.Longest-Increasing-Subsequence/300. Longest Increasing Subsequence.go
package leetcode import "sort" // 解法一 O(n^2) DP func lengthOfLIS(nums []int) int { dp, res := make([]int, len(nums)+1), 0 dp[0] = 0 for i := 1; i <= len(nums); i++ { for j := 1; j < i; j++ { if nums[j-1] < nums[i-1] { dp[i] = max(dp[i], dp[j]) } } dp[i] = dp[i] + 1 res = max(res, dp[i]) } return res } func max(a int, b int) int { if a > b { return a } return b } // 解法二 O(n log n) DP func lengthOfLIS1(nums []int) int { dp := []int{} for _, num := range nums { i := sort.SearchInts(dp, num) if i == len(dp) { dp = append(dp, num) } else { dp[i] = num } } return len(dp) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0816.Ambiguous-Coordinates/816. Ambiguous Coordinates_test.go
leetcode/0816.Ambiguous-Coordinates/816. Ambiguous Coordinates_test.go
package leetcode import ( "fmt" "testing" ) type question816 struct { para816 ans816 } // para 是参数 // one 代表第一个参数 type para816 struct { one string } // ans 是答案 // one 代表第一个答案 type ans816 struct { one []string } func Test_Problem816(t *testing.T) { qs := []question816{ { para816{"(120123)"}, ans816{[]string{"(1, 20123)", " (1, 2.0123)", " (1, 20.123)", " (1, 201.23)", " (1, 2012.3)", " (12, 0.123)", " (1.2, 0.123)", " (120, 123)", " (120, 1.23)", " (120, 12.3)", " (1201, 23) ", "(1201, 2.3)", " (1.201, 23)", " (1.201, 2.3) ", "(12.01, 23)", " (12.01, 2.3) ", "(120.1, 23)", " (120.1, 2.3) ", "(12012, 3)", " (1.2012, 3)", " (12.012, 3)", " (120.12, 3)", " (1201.2, 3)"}}, }, } fmt.Printf("------------------------Leetcode Problem 816------------------------\n") for _, q := range qs { _, p := q.ans816, q.para816 fmt.Printf("【input】:%v 【output】:%v\n", p, ambiguousCoordinates(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0816.Ambiguous-Coordinates/816. Ambiguous Coordinates.go
leetcode/0816.Ambiguous-Coordinates/816. Ambiguous Coordinates.go
package leetcode func ambiguousCoordinates(s string) []string { res := []string{} s = s[1 : len(s)-1] for i := range s[:len(s)-1] { a := build(s[:i+1]) b := build(s[i+1:]) for _, ta := range a { for _, tb := range b { res = append(res, "("+ta+", "+tb+")") } } } return res } func build(s string) []string { res := []string{} if len(s) == 1 || s[0] != '0' { res = append(res, s) } // 结尾带 0 的情况 if s[len(s)-1] == '0' { return res } // 切分长度大于一位且带前导 0 的情况 if s[0] == '0' { res = append(res, "0."+s[1:]) return res } for i := range s[:len(s)-1] { res = append(res, s[:i+1]+"."+s[i+1:]) } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0767.Reorganize-String/767. Reorganize String.go
leetcode/0767.Reorganize-String/767. Reorganize String.go
package leetcode import ( "sort" ) func reorganizeString(S string) string { fs := frequencySort767(S) if fs == "" { return "" } bs := []byte(fs) ans := "" j := (len(bs)-1)/2 + 1 for i := 0; i <= (len(bs)-1)/2; i++ { ans += string(bs[i]) if j < len(bs) { ans += string(bs[j]) } j++ } return ans } func frequencySort767(s string) string { if s == "" { return "" } sMap := map[byte]int{} cMap := map[int][]byte{} sb := []byte(s) for _, b := range sb { sMap[b]++ if sMap[b] > (len(sb)+1)/2 { return "" } } for key, value := range sMap { cMap[value] = append(cMap[value], key) } var keys []int for k := range cMap { keys = append(keys, k) } sort.Sort(sort.Reverse(sort.IntSlice(keys))) res := make([]byte, 0) for _, k := range keys { for i := 0; i < len(cMap[k]); i++ { for j := 0; j < k; j++ { res = append(res, cMap[k][i]) } } } return string(res) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0767.Reorganize-String/767. Reorganize String_test.go
leetcode/0767.Reorganize-String/767. Reorganize String_test.go
package leetcode import ( "fmt" "testing" ) type question767 struct { para767 ans767 } // para 是参数 // one 代表第一个参数 type para767 struct { one string } // ans 是答案 // one 代表第一个答案 type ans767 struct { one string } func Test_Problem767(t *testing.T) { qs := []question767{ { para767{"aab"}, ans767{"aba"}, }, { para767{"aaab"}, ans767{""}, }, } fmt.Printf("------------------------Leetcode Problem 767------------------------\n") for _, q := range qs { _, p := q.ans767, q.para767 fmt.Printf("【input】:%v 【output】:%v\n", p, reorganizeString(p.one)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0515.Find-Largest-Value-in-Each-Tree-Row/515. Find Largest Value in Each Tree Row.go
leetcode/0515.Find-Largest-Value-in-Each-Tree-Row/515. Find Largest Value in Each Tree Row.go
package leetcode import ( "math" "sort" "github.com/halfrost/LeetCode-Go/structures" ) // TreeNode define type TreeNode = structures.TreeNode /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ // 解法一 层序遍历二叉树,再将每层排序取出最大值 func largestValues(root *TreeNode) []int { tmp := levelOrder(root) res := []int{} for i := 0; i < len(tmp); i++ { sort.Ints(tmp[i]) res = append(res, tmp[i][len(tmp[i])-1]) } return res } // this is 102 solution func levelOrder(root *TreeNode) [][]int { if root == nil { return [][]int{} } queue := []*TreeNode{} queue = append(queue, root) curNum, nextLevelNum, res, tmp := 1, 0, [][]int{}, []int{} for len(queue) != 0 { if curNum > 0 { node := queue[0] if node.Left != nil { queue = append(queue, node.Left) nextLevelNum++ } if node.Right != nil { queue = append(queue, node.Right) nextLevelNum++ } curNum-- tmp = append(tmp, node.Val) queue = queue[1:] } if curNum == 0 { res = append(res, tmp) curNum = nextLevelNum nextLevelNum = 0 tmp = []int{} } } return res } // 解法二 层序遍历二叉树,遍历过程中不断更新最大值 func largestValues1(root *TreeNode) []int { if root == nil { return []int{} } q := []*TreeNode{root} var res []int for len(q) > 0 { qlen := len(q) max := math.MinInt32 for i := 0; i < qlen; i++ { node := q[0] q = q[1:] if node.Val > max { max = node.Val } if node.Left != nil { q = append(q, node.Left) } if node.Right != nil { q = append(q, node.Right) } } res = append(res, max) } return res } // 解法三 深度遍历二叉树 func largestValues3(root *TreeNode) []int { var res []int var dfs func(root *TreeNode, level int) dfs = func(root *TreeNode, level int) { if root == nil { return } if len(res) == level { res = append(res, root.Val) } if res[level] < root.Val { res[level] = root.Val } dfs(root.Right, level+1) dfs(root.Left, level+1) } dfs(root, 0) return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0515.Find-Largest-Value-in-Each-Tree-Row/515. Find Largest Value in Each Tree Row_test.go
leetcode/0515.Find-Largest-Value-in-Each-Tree-Row/515. Find Largest Value in Each Tree Row_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question515 struct { para515 ans515 } // para 是参数 // one 代表第一个参数 type para515 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans515 struct { one []int } func Test_Problem515(t *testing.T) { qs := []question515{ { para515{[]int{}}, ans515{[]int{}}, }, { para515{[]int{1}}, ans515{[]int{1}}, }, { para515{[]int{1, 3, 2, 5, 3, structures.NULL, 9}}, ans515{[]int{1, 3, 9}}, }, { para515{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}}, ans515{[]int{3, 20, 15}}, }, } fmt.Printf("------------------------Leetcode Problem 515------------------------\n") for _, q := range qs { _, p := q.ans515, q.para515 fmt.Printf("【input】:%v ", p) root := structures.Ints2TreeNode(p.one) fmt.Printf("【output】:%v \n", largestValues(root)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false