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/1816.Truncate-Sentence/1816.Truncate Sentence_test.go
leetcode/1816.Truncate-Sentence/1816.Truncate Sentence_test.go
package leetcode import ( "fmt" "testing" ) type question1816 struct { para1816 ans1816 } // para 是参数 type para1816 struct { s string k int } // ans 是答案 type ans1816 struct { ans string } func Test_Problem1816(t *testing.T) { qs := []question1816{ { para1816{"Hello how are you Contestant", 4}, ans1816{"Hello how are you"}, }, { para1816{"What is the solution to this problem", 4}, ans1816{"What is the solution"}, }, { para1816{"chopper is not a tanuki", 5}, ans1816{"chopper is not a tanuki"}, }, } fmt.Printf("------------------------Leetcode Problem 1816------------------------\n") for _, q := range qs { _, p := q.ans1816, q.para1816 fmt.Printf("【input】:%v 【output】:%v\n", p, truncateSentence(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/1816.Truncate-Sentence/1816.Truncate Sentence.go
leetcode/1816.Truncate-Sentence/1816.Truncate Sentence.go
package leetcode func truncateSentence(s string, k int) string { end := 0 for i := range s { if k > 0 && s[i] == ' ' { k-- } if k == 0 { end = i break } } if end == 0 { return s } return s[:end] }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1486.XOR-Operation-in-an-Array/1486. XOR Operation in an Array.go
leetcode/1486.XOR-Operation-in-an-Array/1486. XOR Operation in an Array.go
package leetcode func xorOperation(n int, start int) int { res := 0 for i := 0; i < n; i++ { res ^= start + 2*i } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1486.XOR-Operation-in-an-Array/1486. XOR Operation in an Array_test.go
leetcode/1486.XOR-Operation-in-an-Array/1486. XOR Operation in an Array_test.go
package leetcode import ( "fmt" "testing" ) type question1486 struct { para1486 ans1486 } // para 是参数 // one 代表第一个参数 type para1486 struct { n int start int } // ans 是答案 // one 代表第一个答案 type ans1486 struct { one int } func Test_Problem1486(t *testing.T) { qs := []question1486{ { para1486{5, 0}, ans1486{8}, }, { para1486{4, 3}, ans1486{8}, }, { para1486{1, 7}, ans1486{7}, }, { para1486{10, 5}, ans1486{2}, }, } fmt.Printf("------------------------Leetcode Problem 1486------------------------\n") for _, q := range qs { _, p := q.ans1486, q.para1486 fmt.Printf("【input】:%v 【output】:%v \n", p, xorOperation(p.n, p.start)) } 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/0713.Subarray-Product-Less-Than-K/713. Subarray Product Less Than K.go
leetcode/0713.Subarray-Product-Less-Than-K/713. Subarray Product Less Than K.go
package leetcode func numSubarrayProductLessThanK(nums []int, k int) int { if len(nums) == 0 { return 0 } res, left, right, prod := 0, 0, 0, 1 for left < len(nums) { if right < len(nums) && prod*nums[right] < k { prod = prod * nums[right] right++ } else if left == right { left++ right++ } else { res += right - left prod = prod / nums[left] left++ } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0713.Subarray-Product-Less-Than-K/713. Subarray Product Less Than K_test.go
leetcode/0713.Subarray-Product-Less-Than-K/713. Subarray Product Less Than K_test.go
package leetcode import ( "fmt" "testing" ) type question713 struct { para713 ans713 } // para 是参数 // one 代表第一个参数 type para713 struct { s []int k int } // ans 是答案 // one 代表第一个答案 type ans713 struct { one int } func Test_Problem713(t *testing.T) { qs := []question713{ { para713{[]int{10, 5, 2, 6}, 100}, ans713{8}, }, { para713{[]int{10, 9, 10, 4, 3, 8, 3, 3, 6, 2, 10, 10, 9, 3}, 19}, ans713{18}, }, } fmt.Printf("------------------------Leetcode Problem 713------------------------\n") for _, q := range qs { _, p := q.ans713, q.para713 fmt.Printf("【input】:%v 【output】:%v\n", p, numSubarrayProductLessThanK(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/0367.Valid-Perfect-Square/367. Valid Perfect Square.go
leetcode/0367.Valid-Perfect-Square/367. Valid Perfect Square.go
package leetcode func isPerfectSquare(num int) bool { low, high := 1, num for low <= high { mid := low + (high-low)>>1 if mid*mid == num { return true } else if mid*mid < num { low = mid + 1 } else { high = mid - 1 } } return false }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0367.Valid-Perfect-Square/367. Valid Perfect Square_test.go
leetcode/0367.Valid-Perfect-Square/367. Valid Perfect Square_test.go
package leetcode import ( "fmt" "testing" ) type question367 struct { para367 ans367 } // para 是参数 // one 代表第一个参数 type para367 struct { one int } // ans 是答案 // one 代表第一个答案 type ans367 struct { one bool } func Test_Problem367(t *testing.T) { qs := []question367{ { para367{1}, ans367{true}, }, { para367{2}, ans367{false}, }, { para367{3}, ans367{false}, }, { para367{4}, ans367{true}, }, { para367{5}, ans367{false}, }, { para367{6}, ans367{false}, }, { para367{104976}, ans367{true}, }, // 如需多个测试,可以复制上方元素。 } fmt.Printf("------------------------Leetcode Problem 367------------------------\n") for _, q := range qs { _, p := q.ans367, q.para367 fmt.Printf("【input】:%v 【output】:%v\n", p, isPerfectSquare(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/0933.Number-of-Recent-Calls/933. Number of Recent Calls_test.go
leetcode/0933.Number-of-Recent-Calls/933. Number of Recent Calls_test.go
package leetcode import ( "fmt" "testing" ) func Test_Problem933(t *testing.T) { obj := Constructor933() fmt.Printf("obj = %v\n", obj) param1 := obj.Ping(1) fmt.Printf("param = %v\n", param1) param1 = obj.Ping(100) fmt.Printf("param = %v\n", param1) param1 = obj.Ping(3001) fmt.Printf("param = %v\n", param1) param1 = obj.Ping(3002) fmt.Printf("param = %v\n", param1) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0933.Number-of-Recent-Calls/933. Number of Recent Calls.go
leetcode/0933.Number-of-Recent-Calls/933. Number of Recent Calls.go
package leetcode import "sort" type RecentCounter struct { list []int } func Constructor933() RecentCounter { return RecentCounter{ list: []int{}, } } func (this *RecentCounter) Ping(t int) int { this.list = append(this.list, t) index := sort.Search(len(this.list), func(i int) bool { return this.list[i] >= t-3000 }) if index < 0 { index = -index - 1 } return len(this.list) - index } /** * Your RecentCounter object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.Ping(t); */
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0990.Satisfiability-of-Equality-Equations/990. Satisfiability of Equality Equations_test.go
leetcode/0990.Satisfiability-of-Equality-Equations/990. Satisfiability of Equality Equations_test.go
package leetcode import ( "fmt" "testing" ) type question990 struct { para990 ans990 } // para 是参数 // one 代表第一个参数 type para990 struct { a []string } // ans 是答案 // one 代表第一个答案 type ans990 struct { one bool } func Test_Problem990(t *testing.T) { qs := []question990{ { para990{[]string{"a==b", "b!=a"}}, ans990{false}, }, { para990{[]string{"b==a", "a==b"}}, ans990{true}, }, { para990{[]string{"a==b", "b==c", "a==c"}}, ans990{true}, }, { para990{[]string{"a==b", "b!=c", "c==a"}}, ans990{false}, }, { para990{[]string{"c==c", "b==d", "x!=z"}}, ans990{true}, }, } fmt.Printf("------------------------Leetcode Problem 990------------------------\n") for _, q := range qs { _, p := q.ans990, q.para990 fmt.Printf("【input】:%v 【output】:%v\n", p, equationsPossible(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/0990.Satisfiability-of-Equality-Equations/990. Satisfiability of Equality Equations.go
leetcode/0990.Satisfiability-of-Equality-Equations/990. Satisfiability of Equality Equations.go
package leetcode import ( "github.com/halfrost/LeetCode-Go/template" ) func equationsPossible(equations []string) bool { if len(equations) == 0 { return false } uf := template.UnionFind{} uf.Init(26) for _, equ := range equations { if equ[1] == '=' && equ[2] == '=' { uf.Union(int(equ[0]-'a'), int(equ[3]-'a')) } } for _, equ := range equations { if equ[1] == '!' && equ[2] == '=' { if uf.Find(int(equ[0]-'a')) == uf.Find(int(equ[3]-'a')) { return false } } } return true }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0217.Contains-Duplicate/217. Contains Duplicate.go
leetcode/0217.Contains-Duplicate/217. Contains Duplicate.go
package leetcode func containsDuplicate(nums []int) bool { record := make(map[int]bool, len(nums)) for _, n := range nums { if _, found := record[n]; found { return true } record[n] = 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/0217.Contains-Duplicate/217. Contains Duplicate_test.go
leetcode/0217.Contains-Duplicate/217. Contains Duplicate_test.go
package leetcode import ( "fmt" "testing" ) type question217 struct { para217 ans217 } // para 是参数 // one 代表第一个参数 type para217 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans217 struct { one bool } func Test_Problem217(t *testing.T) { qs := []question217{ { para217{[]int{1, 2, 3, 1}}, ans217{true}, }, { para217{[]int{1, 2, 3, 4}}, ans217{false}, }, { para217{[]int{1, 1, 1, 3, 3, 4, 3, 2, 4, 2}}, ans217{true}, }, } fmt.Printf("------------------------Leetcode Problem 217------------------------\n") for _, q := range qs { _, p := q.ans217, q.para217 fmt.Printf("【input】:%v 【output】:%v\n", p, containsDuplicate(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/0807.Max-Increase-to-Keep-City-Skyline/807.Max Increase to Keep City Skyline_test.go
leetcode/0807.Max-Increase-to-Keep-City-Skyline/807.Max Increase to Keep City Skyline_test.go
package leetcode import ( "fmt" "testing" ) type question807 struct { para807 ans807 } // para 是参数 type para807 struct { grid [][]int } // ans 是答案 type ans807 struct { ans int } func Test_Problem807(t *testing.T) { qs := []question807{ { para807{[][]int{{3, 0, 8, 4}, {2, 4, 5, 7}, {9, 2, 6, 3}, {0, 3, 1, 0}}}, ans807{35}, }, { para807{[][]int{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}}, ans807{0}, }, } fmt.Printf("------------------------Leetcode Problem 807------------------------\n") for _, q := range qs { _, p := q.ans807, q.para807 fmt.Printf("【input】:%v 【output】:%v\n", p.grid, maxIncreaseKeepingSkyline(p.grid)) } 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/0807.Max-Increase-to-Keep-City-Skyline/807.Max Increase to Keep City Skyline.go
leetcode/0807.Max-Increase-to-Keep-City-Skyline/807.Max Increase to Keep City Skyline.go
package leetcode func maxIncreaseKeepingSkyline(grid [][]int) int { n := len(grid) topBottomSkyline := make([]int, 0, n) leftRightSkyline := make([]int, 0, n) for i := range grid { cur := 0 for _, v := range grid[i] { if cur < v { cur = v } } leftRightSkyline = append(leftRightSkyline, cur) } for j := range grid { cur := 0 for i := 0; i < len(grid[0]); i++ { if cur < grid[i][j] { cur = grid[i][j] } } topBottomSkyline = append(topBottomSkyline, cur) } var ans int for i := range grid { for j := 0; j < len(grid[0]); j++ { ans += min(topBottomSkyline[j], leftRightSkyline[i]) - grid[i][j] } } return ans } 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/0653.Two-Sum-IV-Input-is-a-BST/653. Two Sum IV - Input is a BST.go
leetcode/0653.Two-Sum-IV-Input-is-a-BST/653. Two Sum IV - Input is a BST.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 findTarget(root *TreeNode, k int) bool { m := make(map[int]int, 0) return findTargetDFS(root, k, m) } func findTargetDFS(root *TreeNode, k int, m map[int]int) bool { if root == nil { return false } if _, ok := m[k-root.Val]; ok { return ok } m[root.Val]++ return findTargetDFS(root.Left, k, m) || findTargetDFS(root.Right, k, m) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0653.Two-Sum-IV-Input-is-a-BST/653. Two Sum IV - Input is a BST_test.go
leetcode/0653.Two-Sum-IV-Input-is-a-BST/653. Two Sum IV - Input is a BST_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question653 struct { para653 ans653 } // para 是参数 // one 代表第一个参数 type para653 struct { one []int k int } // ans 是答案 // one 代表第一个答案 type ans653 struct { one bool } func Test_Problem653(t *testing.T) { qs := []question653{ { para653{[]int{}, 0}, ans653{false}, }, { para653{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}, 29}, ans653{true}, }, { para653{[]int{1, 2, 3, 4, structures.NULL, structures.NULL, 5}, 9}, ans653{true}, }, { para653{[]int{1, 2, 3, 4, structures.NULL, 5}, 4}, ans653{true}, }, } fmt.Printf("------------------------Leetcode Problem 653------------------------\n") for _, q := range qs { _, p := q.ans653, q.para653 fmt.Printf("【input】:%v ", p) root := structures.Ints2TreeNode(p.one) fmt.Printf("【output】:%v \n", findTarget(root, 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/1695.Maximum-Erasure-Value/1695. Maximum Erasure Value.go
leetcode/1695.Maximum-Erasure-Value/1695. Maximum Erasure Value.go
package leetcode func maximumUniqueSubarray(nums []int) int { if len(nums) == 0 { return 0 } result, left, right, freq := 0, 0, -1, map[int]int{} for left < len(nums) { if right+1 < len(nums) && freq[nums[right+1]] == 0 { freq[nums[right+1]]++ right++ } else { freq[nums[left]]-- left++ } sum := 0 for i := left; i <= right; i++ { sum += nums[i] } result = max(result, sum) } return result } 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/1695.Maximum-Erasure-Value/1695. Maximum Erasure Value_test.go
leetcode/1695.Maximum-Erasure-Value/1695. Maximum Erasure Value_test.go
package leetcode import ( "fmt" "testing" ) type question1695 struct { para1695 ans1695 } // para 是参数 // one 代表第一个参数 type para1695 struct { nums []int } // ans 是答案 // one 代表第一个答案 type ans1695 struct { one int } func Test_Problem1695(t *testing.T) { qs := []question1695{ { para1695{[]int{4, 2, 4, 5, 6}}, ans1695{17}, }, { para1695{[]int{5, 2, 1, 2, 5, 2, 1, 2, 5}}, ans1695{8}, }, } fmt.Printf("------------------------Leetcode Problem 1695------------------------\n") for _, q := range qs { _, p := q.ans1695, q.para1695 fmt.Printf("【input】:%v 【output】:%v\n", p, maximumUniqueSubarray(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/2180.Count-Integers-With-Even-Digit-Sum/2180. Count Integers With Even Digit Sum.go
leetcode/2180.Count-Integers-With-Even-Digit-Sum/2180. Count Integers With Even Digit Sum.go
package leetcode func countEven(num int) int { count := 0 for i := 1; i <= num; i++ { if addSum(i)%2 == 0 { count++ } } return count } func addSum(num int) int { sum := 0 tmp := num for tmp != 0 { sum += tmp % 10 tmp = tmp / 10 } return sum }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/2180.Count-Integers-With-Even-Digit-Sum/2180. Count Integers With Even Digit Sum_test.go
leetcode/2180.Count-Integers-With-Even-Digit-Sum/2180. Count Integers With Even Digit Sum_test.go
package leetcode import ( "fmt" "testing" ) type question2180 struct { para2180 ans2180 } // para 是参数 // one 代表第一个参数 type para2180 struct { target int } // ans 是答案 // one 代表第一个答案 type ans2180 struct { one int } func Test_Problem1(t *testing.T) { qs := []question2180{ { para2180{4}, ans2180{2}, }, { para2180{30}, ans2180{14}, }, } fmt.Printf("------------------------Leetcode Problem 2180------------------------\n") for _, q := range qs { _, p := q.ans2180, q.para2180 fmt.Printf("【input】:%v 【output】:%v\n", p, countEven(p.target)) } 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/1738.Find-Kth-Largest-XOR-Coordinate-Value/1738. Find Kth Largest XOR Coordinate Value.go
leetcode/1738.Find-Kth-Largest-XOR-Coordinate-Value/1738. Find Kth Largest XOR Coordinate Value.go
package leetcode import "sort" // 解法一 压缩版的前缀和 func kthLargestValue(matrix [][]int, k int) int { if len(matrix) == 0 || len(matrix[0]) == 0 { return 0 } res, prefixSum := make([]int, 0, len(matrix)*len(matrix[0])), make([]int, len(matrix[0])) for i := range matrix { line := 0 for j, v := range matrix[i] { line ^= v prefixSum[j] ^= line res = append(res, prefixSum[j]) } } sort.Ints(res) return res[len(res)-k] } // 解法二 前缀和 func kthLargestValue1(matrix [][]int, k int) int { nums, prefixSum := []int{}, make([][]int, len(matrix)+1) prefixSum[0] = make([]int, len(matrix[0])+1) for i, row := range matrix { prefixSum[i+1] = make([]int, len(matrix[0])+1) for j, val := range row { prefixSum[i+1][j+1] = prefixSum[i+1][j] ^ prefixSum[i][j+1] ^ prefixSum[i][j] ^ val nums = append(nums, prefixSum[i+1][j+1]) } } sort.Ints(nums) return nums[len(nums)-k] }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1738.Find-Kth-Largest-XOR-Coordinate-Value/1738. Find Kth Largest XOR Coordinate Value_test.go
leetcode/1738.Find-Kth-Largest-XOR-Coordinate-Value/1738. Find Kth Largest XOR Coordinate Value_test.go
package leetcode import ( "fmt" "testing" ) type question1738 struct { para1738 ans1738 } // para 是参数 // one 代表第一个参数 type para1738 struct { matrix [][]int k int } // ans 是答案 // one 代表第一个答案 type ans1738 struct { one int } func Test_Problem1738(t *testing.T) { qs := []question1738{ { para1738{[][]int{{5, 2}, {1, 6}}, 1}, ans1738{7}, }, { para1738{[][]int{{5, 2}, {1, 6}}, 2}, ans1738{5}, }, { para1738{[][]int{{5, 2}, {1, 6}}, 3}, ans1738{4}, }, { para1738{[][]int{{5, 2}, {1, 6}}, 4}, ans1738{0}, }, } fmt.Printf("------------------------Leetcode Problem 1738------------------------\n") for _, q := range qs { _, p := q.ans1738, q.para1738 fmt.Printf("【input】:%v 【output】:%v\n", p, kthLargestValue(p.matrix, 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/0496.Next-Greater-Element-I/496. Next Greater Element I.go
leetcode/0496.Next-Greater-Element-I/496. Next Greater Element I.go
package leetcode func nextGreaterElement(nums1 []int, nums2 []int) []int { if len(nums1) == 0 || len(nums2) == 0 { return []int{} } res, reocrd := []int{}, map[int]int{} for i, v := range nums2 { reocrd[v] = i } for i := 0; i < len(nums1); i++ { flag := false for j := reocrd[nums1[i]]; j < len(nums2); j++ { if nums2[j] > nums1[i] { res = append(res, nums2[j]) flag = true break } } if flag == false { res = append(res, -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/0496.Next-Greater-Element-I/496. Next Greater Element I_test.go
leetcode/0496.Next-Greater-Element-I/496. Next Greater Element I_test.go
package leetcode import ( "fmt" "testing" ) type question496 struct { para496 ans496 } // para 是参数 // one 代表第一个参数 type para496 struct { one []int another []int } // ans 是答案 // one 代表第一个答案 type ans496 struct { one []int } func Test_Problem496(t *testing.T) { qs := []question496{ { para496{[]int{4, 1, 2}, []int{1, 3, 4, 2}}, ans496{[]int{-1, 3, -1}}, }, { para496{[]int{2, 4}, []int{1, 2, 3, 4}}, ans496{[]int{3, -1}}, }, // 如需多个测试,可以复制上方元素。 } fmt.Printf("------------------------Leetcode Problem 496------------------------\n") for _, q := range qs { _, p := q.ans496, q.para496 fmt.Printf("【input】:%v 【output】:%v\n", p, nextGreaterElement(p.one, p.another)) } 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/0173.Binary-Search-Tree-Iterator/173. Binary Search Tree Iterator.go
leetcode/0173.Binary-Search-Tree-Iterator/173. Binary Search Tree Iterator.go
package leetcode import ( "container/heap" "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 * } */ // BSTIterator define type BSTIterator struct { pq PriorityQueueOfInt count int } // Constructor173 define func Constructor173(root *TreeNode) BSTIterator { result, pq := []int{}, PriorityQueueOfInt{} postorder(root, &result) for _, v := range result { heap.Push(&pq, v) } bs := BSTIterator{pq: pq, count: len(result)} return bs } func postorder(root *TreeNode, output *[]int) { if root != nil { postorder(root.Left, output) postorder(root.Right, output) *output = append(*output, root.Val) } } /** @return the next smallest number */ func (this *BSTIterator) Next() int { this.count-- return heap.Pop(&this.pq).(int) } /** @return whether we have a next smallest number */ func (this *BSTIterator) HasNext() bool { return this.count != 0 } /** * Your BSTIterator object will be instantiated and called as such: * obj := Constructor(root); * param_1 := obj.Next(); * param_2 := obj.HasNext(); */ type PriorityQueueOfInt []int func (pq PriorityQueueOfInt) Len() int { return len(pq) } func (pq PriorityQueueOfInt) Less(i, j int) bool { return pq[i] < pq[j] } func (pq PriorityQueueOfInt) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] } func (pq *PriorityQueueOfInt) Push(x interface{}) { item := x.(int) *pq = append(*pq, item) } func (pq *PriorityQueueOfInt) Pop() interface{} { n := len(*pq) item := (*pq)[n-1] *pq = (*pq)[:n-1] return item }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0173.Binary-Search-Tree-Iterator/173. Binary Search Tree Iterator_test.go
leetcode/0173.Binary-Search-Tree-Iterator/173. Binary Search Tree Iterator_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) func Test_Problem173(t *testing.T) { root := structures.Ints2TreeNode([]int{9, 7, 15, 3, structures.NULL, structures.NULL, 20}) obj := Constructor173(root) fmt.Printf("obj = %v\n", obj) param1 := obj.Next() fmt.Printf("param_1 = %v\n", param1) param2 := obj.HasNext() fmt.Printf("param_2 = %v\n", param2) param1 = obj.Next() fmt.Printf("param_1 = %v\n", param1) param1 = obj.Next() fmt.Printf("param_1 = %v\n", param1) param1 = obj.Next() fmt.Printf("param_1 = %v\n", param1) param1 = obj.Next() fmt.Printf("param_1 = %v\n", param1) param2 = obj.HasNext() fmt.Printf("param_2 = %v\n", param2) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1283.Find-the-Smallest-Divisor-Given-a-Threshold/1283. Find the Smallest Divisor Given a Threshold_test.go
leetcode/1283.Find-the-Smallest-Divisor-Given-a-Threshold/1283. Find the Smallest Divisor Given a Threshold_test.go
package leetcode import ( "fmt" "testing" ) type question1283 struct { para1283 ans1283 } // para 是参数 // one 代表第一个参数 type para1283 struct { nums []int threshold int } // ans 是答案 // one 代表第一个答案 type ans1283 struct { one int } func Test_Problem1283(t *testing.T) { qs := []question1283{ { para1283{[]int{1, 2, 5, 9}, 6}, ans1283{5}, }, { para1283{[]int{2, 3, 5, 7, 11}, 11}, ans1283{3}, }, { para1283{[]int{19}, 5}, ans1283{4}, }, } fmt.Printf("------------------------Leetcode Problem 1283------------------------\n") for _, q := range qs { _, p := q.ans1283, q.para1283 fmt.Printf("【input】:%v 【output】:%v\n", p, smallestDivisor(p.nums, p.threshold)) } 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/1283.Find-the-Smallest-Divisor-Given-a-Threshold/1283. Find the Smallest Divisor Given a Threshold.go
leetcode/1283.Find-the-Smallest-Divisor-Given-a-Threshold/1283. Find the Smallest Divisor Given a Threshold.go
package leetcode func smallestDivisor(nums []int, threshold int) int { low, high := 1, 1000000 for low < high { mid := low + (high-low)>>1 if calDivisor(nums, mid, threshold) { high = mid } else { low = mid + 1 } } return low } func calDivisor(nums []int, mid, threshold int) bool { sum := 0 for i := range nums { if nums[i]%mid != 0 { sum += nums[i]/mid + 1 } else { sum += nums[i] / mid } } if sum <= threshold { 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/0690.Employee-Importance/690. Employee Importance.go
leetcode/0690.Employee-Importance/690. Employee Importance.go
package leetcode type Employee struct { Id int Importance int Subordinates []int } func getImportance(employees []*Employee, id int) int { m, queue, res := map[int]*Employee{}, []int{id}, 0 for _, e := range employees { m[e.Id] = e } for len(queue) > 0 { e := m[queue[0]] queue = queue[1:] if e == nil { continue } res += e.Importance for _, i := range e.Subordinates { queue = append(queue, i) } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0690.Employee-Importance/690. Employee Importance_test.go
leetcode/0690.Employee-Importance/690. Employee Importance_test.go
package leetcode import ( "fmt" "testing" ) type question690 struct { para690 ans690 } // para 是参数 // one 代表第一个参数 type para690 struct { employees []*Employee id int } // ans 是答案 // one 代表第一个答案 type ans690 struct { one int } func Test_Problem690(t *testing.T) { qs := []question690{ { para690{[]*Employee{{1, 5, []int{2, 3}}, {2, 3, []int{}}, {3, 3, []int{}}}, 1}, ans690{11}, }, } fmt.Printf("------------------------Leetcode Problem 690------------------------\n") for _, q := range qs { _, p := q.ans690, q.para690 fmt.Printf("【input】:%v 【output】:%v\n", p, getImportance(p.employees, p.id)) } 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/0075.Sort-Colors/75. Sort Colors_test.go
leetcode/0075.Sort-Colors/75. Sort Colors_test.go
package leetcode import ( "fmt" "testing" ) type question75 struct { para75 ans75 } // para 是参数 // one 代表第一个参数 type para75 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans75 struct { one []int } func Test_Problem75(t *testing.T) { qs := []question75{ { para75{[]int{}}, ans75{[]int{}}, }, { para75{[]int{1}}, ans75{[]int{1}}, }, { para75{[]int{2, 0, 2, 1, 1, 0}}, ans75{[]int{0, 0, 1, 1, 2, 2}}, }, { para75{[]int{2, 0, 1, 1, 2, 0, 2, 1, 2, 0, 0, 0, 1, 2, 2, 2, 0, 1, 1}}, ans75{[]int{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2}}, }, } fmt.Printf("------------------------Leetcode Problem 75------------------------\n") for _, q := range qs { _, p := q.ans75, q.para75 fmt.Printf("【input】:%v ", p) sortColors(p.one) 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/0075.Sort-Colors/75. Sort Colors.go
leetcode/0075.Sort-Colors/75. Sort Colors.go
package leetcode func sortColors(nums []int) { zero, one := 0, 0 for i, n := range nums { nums[i] = 2 if n <= 1 { nums[one] = 1 one++ } if n == 0 { nums[zero] = 0 zero++ } } }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0328.Odd-Even-Linked-List/328. Odd Even Linked List.go
leetcode/0328.Odd-Even-Linked-List/328. Odd Even Linked List.go
package leetcode import ( "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 oddEvenList(head *ListNode) *ListNode { oddHead := &ListNode{Val: 0, Next: nil} odd := oddHead evenHead := &ListNode{Val: 0, Next: nil} even := evenHead count := 1 for head != nil { if count%2 == 1 { odd.Next = head odd = odd.Next } else { even.Next = head even = even.Next } head = head.Next count++ } even.Next = nil odd.Next = evenHead.Next return oddHead.Next }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0328.Odd-Even-Linked-List/328. Odd Even Linked List_test.go
leetcode/0328.Odd-Even-Linked-List/328. Odd Even Linked List_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question328 struct { para328 ans328 } // para 是参数 // one 代表第一个参数 type para328 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans328 struct { one []int } func Test_Problem328(t *testing.T) { qs := []question328{ { para328{[]int{1, 4, 3, 2, 5, 2}}, ans328{[]int{1, 3, 5, 4, 2, 2}}, }, { para328{[]int{1, 1, 2, 2, 3, 3, 3}}, ans328{[]int{1, 2, 3, 3, 1, 2, 3}}, }, { para328{[]int{4, 3, 2, 5, 2}}, ans328{[]int{4, 2, 2, 3, 5}}, }, { para328{[]int{1, 1, 1, 1, 1, 1}}, ans328{[]int{1, 1, 1, 1, 1, 1}}, }, { para328{[]int{3, 1}}, ans328{[]int{3, 1}}, }, { para328{[]int{1, 2, 3, 4, 5}}, ans328{[]int{1, 3, 5, 2, 4}}, }, { para328{[]int{}}, ans328{[]int{}}, }, } fmt.Printf("------------------------Leetcode Problem 328------------------------\n") for _, q := range qs { _, p := q.ans328, q.para328 fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(oddEvenList(structures.Ints2List(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/0677.Map-Sum-Pairs/677. Map Sum Pairs.go
leetcode/0677.Map-Sum-Pairs/677. Map Sum Pairs.go
package leetcode type MapSum struct { keys map[string]int } /** Initialize your data structure here. */ func Constructor() MapSum { return MapSum{make(map[string]int)} } func (this *MapSum) Insert(key string, val int) { this.keys[key] = val } func (this *MapSum) Sum(prefix string) int { prefixAsRunes, res := []rune(prefix), 0 for key, val := range this.keys { if len(key) >= len(prefix) { shouldSum := true for i, char := range key { if i >= len(prefixAsRunes) { break } if prefixAsRunes[i] != char { shouldSum = false break } } if shouldSum { res += val } } } return res } /** * Your MapSum object will be instantiated and called as such: * obj := Constructor(); * obj.Insert(key,val); * param_2 := obj.Sum(prefix); */
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0677.Map-Sum-Pairs/677. Map Sum Pairs_test.go
leetcode/0677.Map-Sum-Pairs/677. Map Sum Pairs_test.go
package leetcode import ( "fmt" "testing" ) func Test_Problem677(t *testing.T) { obj := Constructor() fmt.Printf("obj = %v\n", obj) obj.Insert("apple", 3) fmt.Printf("obj = %v\n", obj) fmt.Printf("obj.sum = %v\n", obj.Sum("ap")) obj.Insert("app", 2) fmt.Printf("obj = %v\n", obj) fmt.Printf("obj.sum = %v\n", obj.Sum("ap")) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0478.Generate-Random-Point-in-a-Circle/478. Generate Random Point in a Circle_test.go
leetcode/0478.Generate-Random-Point-in-a-Circle/478. Generate Random Point in a Circle_test.go
package leetcode import ( "fmt" "testing" ) func Test_Problem478(t *testing.T) { obj := Constructor(1, 0, 0) fmt.Printf("RandPoint() = %v\n", obj.RandPoint()) fmt.Printf("RandPoint() = %v\n", obj.RandPoint()) fmt.Printf("RandPoint() = %v\n", obj.RandPoint()) obj = Constructor(10, 5, -7.5) fmt.Printf("RandPoint() = %v\n", obj.RandPoint()) fmt.Printf("RandPoint() = %v\n", obj.RandPoint()) fmt.Printf("RandPoint() = %v\n", obj.RandPoint()) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0478.Generate-Random-Point-in-a-Circle/478. Generate Random Point in a Circle.go
leetcode/0478.Generate-Random-Point-in-a-Circle/478. Generate Random Point in a Circle.go
package leetcode import ( "math" "math/rand" "time" ) type Solution struct { r float64 x float64 y float64 } func Constructor(radius float64, x_center float64, y_center float64) Solution { rand.Seed(time.Now().UnixNano()) return Solution{radius, x_center, y_center} } func (this *Solution) RandPoint() []float64 { /* a := angle() r := this.r * math.Sqrt(rand.Float64()) x := r * math.Cos(a) + this.x y := r * math.Sin(a) + this.y return []float64{x, y}*/ for { rx := 2*rand.Float64() - 1.0 ry := 2*rand.Float64() - 1.0 x := this.r * rx y := this.r * ry if x*x+y*y <= this.r*this.r { return []float64{x + this.x, y + this.y} } } } func angle() float64 { return rand.Float64() * 2 * math.Pi } /** * Your Solution object will be instantiated and called as such: * obj := Constructor(radius, x_center, y_center); * param_1 := obj.RandPoint(); */
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0373.Find-K-Pairs-with-Smallest-Sums/373. Find K Pairs with Smallest Sums_test.go
leetcode/0373.Find-K-Pairs-with-Smallest-Sums/373. Find K Pairs with Smallest Sums_test.go
package leetcode import ( "fmt" "testing" ) type question373 struct { para373 ans373 } // para 是参数 // one 代表第一个参数 type para373 struct { nums1 []int nums2 []int k int } // ans 是答案 // one 代表第一个答案 type ans373 struct { one [][]int } func Test_Problem373(t *testing.T) { qs := []question373{ { para373{[]int{1, 7, 11}, []int{2, 4, 6}, 3}, ans373{[][]int{{1, 2}, {1, 4}, {1, 6}}}, }, { para373{[]int{1, 1, 2}, []int{1, 2, 3}, 2}, ans373{[][]int{{1, 1}, {1, 1}}}, }, { para373{[]int{1, 2}, []int{3}, 3}, ans373{[][]int{{1, 3}, {2, 3}}}, }, { para373{[]int{1, 2, 4, 5, 6}, []int{3, 5, 7, 9}, 3}, ans373{[][]int{{1, 3}, {2, 3}, {1, 5}}}, }, { para373{[]int{1, 1, 2}, []int{1, 2, 3}, 10}, ans373{[][]int{{1, 1}, {1, 1}, {2, 1}, {1, 2}, {1, 2}, {2, 2}, {1, 3}, {1, 3}, {2, 3}}}, }, { para373{[]int{-10, -4, 0, 0, 6}, []int{3, 5, 6, 7, 8, 100}, 10}, ans373{[][]int{{-10, 3}, {-10, 5}, {-10, 6}, {-10, 7}, {-10, 8}, {-4, 3}, {-4, 5}, {-4, 6}, {0, 3}, {0, 3}}}, }, { para373{[]int{0, 0, 0, 0, 0}, []int{-3, 22, 35, 56, 76}, 22}, ans373{[][]int{{0, -3}, {0, -3}, {0, -3}, {0, -3}, {0, -3}, {0, 22}, {0, 22}, {0, 22}, {0, 22}, {0, 22}, {0, 35}, {0, 35}, {0, 35}, {0, 35}, {0, 35}, {0, 56}, {0, 56}, {0, 56}, {0, 56}, {0, 56}, {0, 76}, {0, 76}}}, }, } fmt.Printf("------------------------Leetcode Problem 373------------------------\n") for _, q := range qs { _, p := q.ans373, q.para373 fmt.Printf("【input】:%v 【output】:%v\n", p, kSmallestPairs(p.nums1, p.nums2, 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/0373.Find-K-Pairs-with-Smallest-Sums/373. Find K Pairs with Smallest Sums.go
leetcode/0373.Find-K-Pairs-with-Smallest-Sums/373. Find K Pairs with Smallest Sums.go
package leetcode import ( "container/heap" "sort" ) // 解法一 优先队列 func kSmallestPairs(nums1 []int, nums2 []int, k int) [][]int { result, h := [][]int{}, &minHeap{} if len(nums1) == 0 || len(nums2) == 0 || k == 0 { return result } if len(nums1)*len(nums2) < k { k = len(nums1) * len(nums2) } heap.Init(h) for _, num := range nums1 { heap.Push(h, []int{num, nums2[0], 0}) } for len(result) < k { min := heap.Pop(h).([]int) result = append(result, min[:2]) if min[2] < len(nums2)-1 { heap.Push(h, []int{min[0], nums2[min[2]+1], min[2] + 1}) } } return result } type minHeap [][]int func (h minHeap) Len() int { return len(h) } func (h minHeap) Less(i, j int) bool { return h[i][0]+h[i][1] < h[j][0]+h[j][1] } func (h minHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *minHeap) Push(x interface{}) { *h = append(*h, x.([]int)) } func (h *minHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } // 解法二 暴力解法 func kSmallestPairs1(nums1 []int, nums2 []int, k int) [][]int { size1, size2, res := len(nums1), len(nums2), [][]int{} if size1 == 0 || size2 == 0 || k < 0 { return nil } for i := 0; i < size1; i++ { for j := 0; j < size2; j++ { res = append(res, []int{nums1[i], nums2[j]}) } } sort.Slice(res, func(i, j int) bool { return res[i][0]+res[i][1] < res[j][0]+res[j][1] }) if len(res) >= k { return res[:k] } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0733.Flood-Fill/733. Flood Fill_test.go
leetcode/0733.Flood-Fill/733. Flood Fill_test.go
package leetcode import ( "fmt" "testing" ) type question733 struct { para733 ans733 } // para 是参数 // one 代表第一个参数 type para733 struct { one [][]int sr int sc int c int } // ans 是答案 // one 代表第一个答案 type ans733 struct { one [][]int } func Test_Problem733(t *testing.T) { qs := []question733{ { para733{[][]int{ {1, 1, 1}, {1, 1, 0}, {1, 0, 1}, }, 1, 1, 2}, ans733{[][]int{ {2, 2, 2}, {2, 2, 0}, {2, 0, 1}, }}, }, } fmt.Printf("------------------------Leetcode Problem 733------------------------\n") for _, q := range qs { _, p := q.ans733, q.para733 fmt.Printf("【input】:%v 【output】:%v\n", p, floodFill(p.one, p.sr, p.sc, p.c)) } 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/0733.Flood-Fill/733. Flood Fill.go
leetcode/0733.Flood-Fill/733. Flood Fill.go
package leetcode var dir = [][]int{ {-1, 0}, {0, 1}, {1, 0}, {0, -1}, } func floodFill(image [][]int, sr int, sc int, newColor int) [][]int { color := image[sr][sc] if newColor == color { return image } dfs733(image, sr, sc, newColor) return image } func dfs733(image [][]int, x, y int, newColor int) { if image[x][y] == newColor { return } oldColor := image[x][y] image[x][y] = newColor for i := 0; i < 4; i++ { if (x+dir[i][0] >= 0 && x+dir[i][0] < len(image)) && (y+dir[i][1] >= 0 && y+dir[i][1] < len(image[0])) && image[x+dir[i][0]][y+dir[i][1]] == oldColor { dfs733(image, x+dir[i][0], y+dir[i][1], newColor) } } }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1054.Distant-Barcodes/1054. Distant Barcodes.go
leetcode/1054.Distant-Barcodes/1054. Distant Barcodes.go
package leetcode import "sort" func rearrangeBarcodes(barcodes []int) []int { bfs := barcodesFrequencySort(barcodes) if len(bfs) == 0 { return []int{} } res := []int{} j := (len(bfs)-1)/2 + 1 for i := 0; i <= (len(bfs)-1)/2; i++ { res = append(res, bfs[i]) if j < len(bfs) { res = append(res, bfs[j]) } j++ } return res } func barcodesFrequencySort(s []int) []int { if len(s) == 0 { return []int{} } sMap := map[int]int{} // 统计每个数字出现的频次 cMap := map[int][]int{} // 按照频次作为 key 排序 for _, b := range s { sMap[b]++ } 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([]int, 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 res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1054.Distant-Barcodes/1054. Distant Barcodes_test.go
leetcode/1054.Distant-Barcodes/1054. Distant Barcodes_test.go
package leetcode import ( "fmt" "testing" ) type question1054 struct { para1054 ans1054 } // para 是参数 // one 代表第一个参数 type para1054 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans1054 struct { one []int } func Test_Problem1054(t *testing.T) { qs := []question1054{ { para1054{[]int{1, 1, 1, 2, 2, 2}}, ans1054{[]int{2, 1, 2, 1, 2, 1}}, }, { para1054{[]int{1, 1, 1, 1, 2, 2, 3, 3}}, ans1054{[]int{1, 3, 1, 3, 2, 1, 2, 1}}, }, } fmt.Printf("------------------------Leetcode Problem 1054------------------------\n") for _, q := range qs { _, p := q.ans1054, q.para1054 fmt.Printf("【input】:%v 【output】:%v\n", p, rearrangeBarcodes(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/1306.Jump-Game-III/1306. Jump Game III.go
leetcode/1306.Jump-Game-III/1306. Jump Game III.go
package leetcode func canReach(arr []int, start int) bool { if start >= 0 && start < len(arr) && arr[start] < len(arr) { jump := arr[start] arr[start] += len(arr) return jump == 0 || canReach(arr, start+jump) || canReach(arr, start-jump) } return false }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1306.Jump-Game-III/1306. Jump Game III_test.go
leetcode/1306.Jump-Game-III/1306. Jump Game III_test.go
package leetcode import ( "fmt" "testing" ) type question1306 struct { para1306 ans1306 } // para 是参数 // one 代表第一个参数 type para1306 struct { arr []int start int } // ans 是答案 // one 代表第一个答案 type ans1306 struct { one bool } func Test_Problem1306(t *testing.T) { qs := []question1306{ { para1306{[]int{4, 2, 3, 0, 3, 1, 2}, 5}, ans1306{true}, }, { para1306{[]int{4, 2, 3, 0, 3, 1, 2}, 0}, ans1306{true}, }, { para1306{[]int{3, 0, 2, 1, 2}, 2}, ans1306{false}, }, } fmt.Printf("------------------------Leetcode Problem 1306------------------------\n") for _, q := range qs { _, p := q.ans1306, q.para1306 fmt.Printf("【input】:%v 【output】:%v\n", p, canReach(p.arr, p.start)) } 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/0368.Largest-Divisible-Subset/368. Largest Divisible Subset.go
leetcode/0368.Largest-Divisible-Subset/368. Largest Divisible Subset.go
package leetcode import "sort" func largestDivisibleSubset(nums []int) []int { sort.Ints(nums) dp, res := make([]int, len(nums)), []int{} for i := range dp { dp[i] = 1 } maxSize, maxVal := 1, 1 for i := 1; i < len(nums); i++ { for j, v := range nums[:i] { if nums[i]%v == 0 && dp[j]+1 > dp[i] { dp[i] = dp[j] + 1 } } if dp[i] > maxSize { maxSize, maxVal = dp[i], nums[i] } } if maxSize == 1 { return []int{nums[0]} } for i := len(nums) - 1; i >= 0 && maxSize > 0; i-- { if dp[i] == maxSize && maxVal%nums[i] == 0 { res = append(res, nums[i]) maxVal = nums[i] maxSize-- } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0368.Largest-Divisible-Subset/368. Largest Divisible Subset_test.go
leetcode/0368.Largest-Divisible-Subset/368. Largest Divisible Subset_test.go
package leetcode import ( "fmt" "testing" ) type question368 struct { para368 ans368 } // para 是参数 // one 代表第一个参数 type para368 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans368 struct { one []int } func Test_Problem368(t *testing.T) { qs := []question368{ { para368{[]int{1, 2, 3}}, ans368{[]int{1, 2}}, }, { para368{[]int{1, 2, 4, 8}}, ans368{[]int{1, 2, 4, 8}}, }, } fmt.Printf("------------------------Leetcode Problem 368------------------------\n") for _, q := range qs { _, p := q.ans368, q.para368 fmt.Printf("【input】:%v 【output】:%v\n", p, largestDivisibleSubset(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/1009.Complement-of-Base-10-Integer/1009. Complement of Base 10 Integer.go
leetcode/1009.Complement-of-Base-10-Integer/1009. Complement of Base 10 Integer.go
package leetcode func bitwiseComplement(n int) int { mask := 1 for mask < n { mask = (mask << 1) + 1 } return mask ^ n }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1009.Complement-of-Base-10-Integer/1009. Complement of Base 10 Integer_test.go
leetcode/1009.Complement-of-Base-10-Integer/1009. Complement of Base 10 Integer_test.go
package leetcode import ( "fmt" "testing" ) type question1009 struct { para1009 ans1009 } // para 是参数 // one 代表第一个参数 type para1009 struct { n int } // ans 是答案 // one 代表第一个答案 type ans1009 struct { one int } func Test_Problem1009(t *testing.T) { qs := []question1009{ { para1009{5}, ans1009{2}, }, { para1009{7}, ans1009{0}, }, { para1009{10}, ans1009{5}, }, } fmt.Printf("------------------------Leetcode Problem 1009------------------------\n") for _, q := range qs { _, p := q.ans1009, q.para1009 fmt.Printf("【input】:%v 【output】:%v\n", p, bitwiseComplement(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/1302.Deepest-Leaves-Sum/1302. Deepest Leaves Sum_test.go
leetcode/1302.Deepest-Leaves-Sum/1302. Deepest Leaves Sum_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question1302 struct { para1302 ans1302 } // para 是参数 // one 代表第一个参数 type para1302 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans1302 struct { one int } func Test_Problem1302(t *testing.T) { qs := []question1302{ { para1302{[]int{1, 2, 3, 4, 5, structures.NULL, 6, 7, structures.NULL, structures.NULL, structures.NULL, structures.NULL, 8}}, ans1302{15}, }, { para1302{[]int{}}, ans1302{0}, }, } fmt.Printf("------------------------Leetcode Problem 1302------------------------\n") for _, q := range qs { _, p := q.ans1302, q.para1302 fmt.Printf("【input】:%v 【output】:%v\n", p, deepestLeavesSum(structures.Ints2TreeNode(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/1302.Deepest-Leaves-Sum/1302. Deepest Leaves Sum.go
leetcode/1302.Deepest-Leaves-Sum/1302. Deepest Leaves Sum.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 deepestLeavesSum(root *TreeNode) int { maxLevel, sum := 0, 0 dfsDeepestLeavesSum(root, 0, &maxLevel, &sum) return sum } func dfsDeepestLeavesSum(root *TreeNode, level int, maxLevel, sum *int) { if root == nil { return } if level > *maxLevel { *maxLevel, *sum = level, root.Val } else if level == *maxLevel { *sum += root.Val } dfsDeepestLeavesSum(root.Left, level+1, maxLevel, sum) dfsDeepestLeavesSum(root.Right, level+1, maxLevel, sum) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0623.Add-One-Row-to-Tree/623. Add One Row to Tree_test.go
leetcode/0623.Add-One-Row-to-Tree/623. Add One Row to Tree_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question623 struct { para623 ans623 } // para 是参数 // one 代表第一个参数 type para623 struct { one []int v int d int } // ans 是答案 // one 代表第一个答案 type ans623 struct { one []int } func Test_Problem623(t *testing.T) { qs := []question623{ { para623{[]int{4, 2, 6, 3, 1, 5, structures.NULL}, 1, 2}, ans623{[]int{4, 1, 1, 2, structures.NULL, structures.NULL, 6, 3, 1, 5, structures.NULL}}, }, { para623{[]int{4, 2, structures.NULL, 3, 1}, 1, 3}, ans623{[]int{4, 2, structures.NULL, 1, 1, 3, structures.NULL, structures.NULL, 1}}, }, { para623{[]int{1, 2, 3, 4}, 5, 4}, ans623{[]int{1, 2, 3, 4, structures.NULL, structures.NULL, structures.NULL, 5, 5}}, }, { para623{[]int{4, 2, 6, 3, 1, 5}, 1, 3}, ans623{[]int{4, 2, 6, 1, 1, 1, 1, 3, structures.NULL, structures.NULL, 1, 5}}, }, { para623{[]int{4, 2, 6, 3, 1, 5}, 1, 1}, ans623{[]int{1, 4, structures.NULL, 2, 6, 3, 1, 5}}, }, } fmt.Printf("------------------------Leetcode Problem 623------------------------\n") for _, q := range qs { _, p := q.ans623, q.para623 fmt.Printf("【input】:%v ", p) root := structures.Ints2TreeNode(p.one) fmt.Printf("【output】:%v \n", structures.Tree2Preorder(addOneRow(root, p.v, p.d))) } 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/0623.Add-One-Row-to-Tree/623. Add One Row to Tree.go
leetcode/0623.Add-One-Row-to-Tree/623. Add One Row to 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 addOneRow(root *TreeNode, v int, d int) *TreeNode { if d == 1 { tmp := &TreeNode{Val: v, Left: root, Right: nil} return tmp } level := 1 addTreeRow(root, v, d, &level) return root } func addTreeRow(root *TreeNode, v, d int, currLevel *int) { if *currLevel == d-1 { root.Left = &TreeNode{Val: v, Left: root.Left, Right: nil} root.Right = &TreeNode{Val: v, Left: nil, Right: root.Right} return } *currLevel++ if root.Left != nil { addTreeRow(root.Left, v, d, currLevel) } if root.Right != nil { addTreeRow(root.Right, v, d, currLevel) } *currLevel-- }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0475.Heaters/475. Heaters.go
leetcode/0475.Heaters/475. Heaters.go
package leetcode import ( "math" "sort" ) func findRadius(houses []int, heaters []int) int { minRad := 0 sort.Ints(heaters) for _, house := range houses { // 遍历房子的坐标,维护 heaters 的最小半径 heater := findClosestHeater(house, heaters) rad := heater - house if rad < 0 { rad = -rad } if rad > minRad { minRad = rad } } return minRad } // 二分搜索 func findClosestHeater(pos int, heaters []int) int { low, high := 0, len(heaters)-1 if pos < heaters[low] { return heaters[low] } if pos > heaters[high] { return heaters[high] } for low <= high { mid := low + (high-low)>>1 if pos == heaters[mid] { return heaters[mid] } else if pos < heaters[mid] { high = mid - 1 } else { low = mid + 1 } } // 判断距离两边的 heaters 哪个更近 if pos-heaters[high] < heaters[low]-pos { return heaters[high] } return heaters[low] } // 解法二 暴力搜索 func findRadius1(houses []int, heaters []int) int { res := 0 for i := 0; i < len(houses); i++ { dis := math.MaxInt64 for j := 0; j < len(heaters); j++ { dis = min(dis, abs(houses[i]-heaters[j])) } res = max(res, dis) } 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 abs(a int) int { if a > 0 { return 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/0475.Heaters/475. Heaters_test.go
leetcode/0475.Heaters/475. Heaters_test.go
package leetcode import ( "fmt" "testing" ) type question475 struct { para475 ans475 } // para 是参数 // one 代表第一个参数 type para475 struct { houses []int heaters []int } // ans 是答案 // one 代表第一个答案 type ans475 struct { one int } func Test_Problem475(t *testing.T) { qs := []question475{ { para475{[]int{1, 2, 3}, []int{2}}, ans475{1}, }, { para475{[]int{1, 2, 3, 4}, []int{1, 4}}, ans475{1}, }, } fmt.Printf("------------------------Leetcode Problem 475------------------------\n") for _, q := range qs { _, p := q.ans475, q.para475 fmt.Printf("【input】:%v 【output】:%v\n", p, findRadius(p.houses, p.heaters)) } 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/1681.Minimum-Incompatibility/1681. Minimum Incompatibility.go
leetcode/1681.Minimum-Incompatibility/1681. Minimum Incompatibility.go
package leetcode import ( "math" "sort" ) func minimumIncompatibility(nums []int, k int) int { sort.Ints(nums) eachSize, counts := len(nums)/k, make([]int, len(nums)+1) for i := range nums { counts[nums[i]]++ if counts[nums[i]] > k { return -1 } } orders := []int{} for i := range counts { orders = append(orders, i) } sort.Ints(orders) res := math.MaxInt32 generatePermutation1681(nums, counts, orders, 0, 0, eachSize, &res, []int{}) if res == math.MaxInt32 { return -1 } return res } func generatePermutation1681(nums, counts, order []int, index, sum, eachSize int, res *int, current []int) { if len(current) > 0 && len(current)%eachSize == 0 { sum += current[len(current)-1] - current[len(current)-eachSize] index = 0 } if sum >= *res { return } if len(current) == len(nums) { if sum < *res { *res = sum } return } for i := index; i < len(counts); i++ { if counts[order[i]] == 0 { continue } counts[order[i]]-- current = append(current, order[i]) generatePermutation1681(nums, counts, order, i+1, sum, eachSize, res, current) current = current[:len(current)-1] counts[order[i]]++ // 这里是关键的剪枝 if index == 0 { break } } }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1681.Minimum-Incompatibility/1681. Minimum Incompatibility_test.go
leetcode/1681.Minimum-Incompatibility/1681. Minimum Incompatibility_test.go
package leetcode import ( "fmt" "testing" ) type question1681 struct { para1681 ans1681 } // para 是参数 // one 代表第一个参数 type para1681 struct { nums []int k int } // ans 是答案 // one 代表第一个答案 type ans1681 struct { one int } func Test_Problem1681(t *testing.T) { qs := []question1681{ { para1681{[]int{1, 2, 1, 4}, 2}, ans1681{4}, }, { para1681{[]int{6, 3, 8, 1, 3, 1, 2, 2}, 4}, ans1681{6}, }, { para1681{[]int{5, 3, 3, 6, 3, 3}, 3}, ans1681{-1}, }, } fmt.Printf("------------------------Leetcode Problem 1681------------------------\n") for _, q := range qs { _, p := q.ans1681, q.para1681 fmt.Printf("【input】:%v 【output】:%v\n", p, minimumIncompatibility(p.nums, 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/2169.Count-Operations-to-Obtain-Zero/2169. Count Operations to Obtain Zero.go
leetcode/2169.Count-Operations-to-Obtain-Zero/2169. Count Operations to Obtain Zero.go
package leetcode func countOperations(num1 int, num2 int) int { res := 0 for num1 != 0 && num2 != 0 { if num1 >= num2 { num1 -= num2 } else { num2 -= num1 } res++ } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/2169.Count-Operations-to-Obtain-Zero/2169. Count Operations to Obtain Zero_test.go
leetcode/2169.Count-Operations-to-Obtain-Zero/2169. Count Operations to Obtain Zero_test.go
package leetcode import ( "fmt" "testing" ) type question2169 struct { para2169 ans2169 } // para 是参数 // one 代表第一个参数 type para2169 struct { num1 int num2 int } // ans 是答案 // one 代表第一个答案 type ans2169 struct { one int } func Test_Problem2169(t *testing.T) { qs := []question2169{ { para2169{2, 3}, ans2169{3}, }, { para2169{10, 10}, ans2169{1}, }, } fmt.Printf("------------------------Leetcode Problem 2169------------------------\n") for _, q := range qs { _, p := q.ans2169, q.para2169 fmt.Printf("【input】:%v 【output】:%v\n", p, countOperations(p.num1, p.num2)) } 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/1608.Special-Array-With-X-Elements-Greater-Than-or-Equal-X/1608. Special Array With X Elements Greater Than or Equal X_test.go
leetcode/1608.Special-Array-With-X-Elements-Greater-Than-or-Equal-X/1608. Special Array With X Elements Greater Than or Equal X_test.go
package leetcode import ( "fmt" "testing" ) type question1608 struct { para1608 ans1608 } // para 是参数 // one 代表第一个参数 type para1608 struct { nums []int } // ans 是答案 // one 代表第一个答案 type ans1608 struct { one int } func Test_Problem1608(t *testing.T) { qs := []question1608{ { para1608{[]int{3, 5}}, ans1608{2}, }, { para1608{[]int{0, 0}}, ans1608{-1}, }, { para1608{[]int{0, 4, 3, 0, 4}}, ans1608{3}, }, { para1608{[]int{3, 6, 7, 7, 0}}, ans1608{-1}, }, } fmt.Printf("------------------------Leetcode Problem 1608------------------------\n") for _, q := range qs { _, p := q.ans1608, q.para1608 fmt.Printf("【input】:%v 【output】:%v \n", p, specialArray(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/1608.Special-Array-With-X-Elements-Greater-Than-or-Equal-X/1608. Special Array With X Elements Greater Than or Equal X.go
leetcode/1608.Special-Array-With-X-Elements-Greater-Than-or-Equal-X/1608. Special Array With X Elements Greater Than or Equal X.go
package leetcode import "sort" func specialArray(nums []int) int { sort.Ints(nums) x := len(nums) for _, num := range nums { if num >= x { return x } x-- if num >= x { return -1 } } return -1 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0206.Reverse-Linked-List/206. Reverse Linked List_test.go
leetcode/0206.Reverse-Linked-List/206. Reverse Linked List_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question206 struct { para206 ans206 } // para 是参数 // one 代表第一个参数 type para206 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans206 struct { one []int } func Test_Problem206(t *testing.T) { qs := []question206{ { para206{[]int{1, 2, 3, 4, 5}}, ans206{[]int{5, 4, 3, 2, 1}}, }, } fmt.Printf("------------------------Leetcode Problem 206------------------------\n") for _, q := range qs { _, p := q.ans206, q.para206 fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(reverseList(structures.Ints2List(p.one)))) } }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0206.Reverse-Linked-List/206. Reverse Linked List.go
leetcode/0206.Reverse-Linked-List/206. Reverse Linked List.go
package leetcode import ( "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 reverseList(head *ListNode) *ListNode { var behind *ListNode for head != nil { next := head.Next head.Next = behind behind = head head = next } return behind }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0309.Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/309. Best Time to Buy and Sell Stock with Cooldown_test.go
leetcode/0309.Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/309. Best Time to Buy and Sell Stock with Cooldown_test.go
package leetcode import ( "fmt" "testing" ) type question309 struct { para309 ans309 } // para 是参数 // one 代表第一个参数 type para309 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans309 struct { one int } func Test_Problem309(t *testing.T) { qs := []question309{ { para309{[]int{}}, ans309{0}, }, { para309{[]int{2, 1, 4, 5, 2, 9, 7}}, ans309{10}, }, { para309{[]int{6, 1, 3, 2, 4, 7}}, ans309{6}, }, { para309{[]int{1, 2, 3, 0, 2}}, ans309{3}, }, { para309{[]int{7, 1, 5, 3, 6, 4}}, ans309{5}, }, { para309{[]int{7, 6, 4, 3, 1}}, ans309{0}, }, } fmt.Printf("------------------------Leetcode Problem 309------------------------\n") for _, q := range qs { _, p := q.ans309, q.para309 fmt.Printf("【input】:%v 【output】:%v\n", p, maxProfit309(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/0309.Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/309. Best Time to Buy and Sell Stock with Cooldown.go
leetcode/0309.Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/309. Best Time to Buy and Sell Stock with Cooldown.go
package leetcode import ( "math" ) // 解法一 DP func maxProfit309(prices []int) int { if len(prices) <= 1 { return 0 } buy, sell := make([]int, len(prices)), make([]int, len(prices)) for i := range buy { buy[i] = math.MinInt64 } buy[0] = -prices[0] buy[1] = max(buy[0], -prices[1]) sell[1] = max(sell[0], buy[0]+prices[1]) for i := 2; i < len(prices); i++ { sell[i] = max(sell[i-1], buy[i-1]+prices[i]) buy[i] = max(buy[i-1], sell[i-2]-prices[i]) } return sell[len(sell)-1] } func max(a int, b int) int { if a > b { return a } return b } // 解法二 优化辅助空间的 DP func maxProfit309_1(prices []int) int { if len(prices) <= 1 { return 0 } buy := []int{-prices[0], max(-prices[0], -prices[1]), math.MinInt64} sell := []int{0, max(0, -prices[0]+prices[1]), 0} for i := 2; i < len(prices); i++ { sell[i%3] = max(sell[(i-1)%3], buy[(i-1)%3]+prices[i]) buy[i%3] = max(buy[(i-1)%3], sell[(i-2)%3]-prices[i]) } return sell[(len(prices)-1)%3] }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0228.Summary-Ranges/228. Summary Ranges.go
leetcode/0228.Summary-Ranges/228. Summary Ranges.go
package leetcode import ( "strconv" ) func summaryRanges(nums []int) (ans []string) { for i, n := 0, len(nums); i < n; { left := i for i++; i < n && nums[i-1]+1 == nums[i]; i++ { } s := strconv.Itoa(nums[left]) if left != i-1 { s += "->" + strconv.Itoa(nums[i-1]) } ans = append(ans, s) } return }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0228.Summary-Ranges/228. Summary Ranges_test.go
leetcode/0228.Summary-Ranges/228. Summary Ranges_test.go
package leetcode import ( "fmt" "testing" ) type question228 struct { para228 ans228 } // para 是参数 // one 代表第一个参数 type para228 struct { nums []int } // ans 是答案 // one 代表第一个答案 type ans228 struct { ans []string } func Test_Problem228(t *testing.T) { qs := []question228{ { para228{[]int{0, 1, 2, 4, 5, 7}}, ans228{[]string{"0->2", "4->5", "7"}}, }, { para228{[]int{0, 2, 3, 4, 6, 8, 9}}, ans228{[]string{"0", "2->4", "6", "8->9"}}, }, { para228{[]int{}}, ans228{[]string{}}, }, { para228{[]int{-1}}, ans228{[]string{"-1"}}, }, { para228{[]int{0}}, ans228{[]string{"0"}}, }, } fmt.Printf("------------------------Leetcode Problem 228------------------------\n") for _, q := range qs { _, p := q.ans228, q.para228 fmt.Printf("【input】:%v 【output】:%v\n", p, summaryRanges(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/1004.Max-Consecutive-Ones-III/1004. Max Consecutive Ones III_test.go
leetcode/1004.Max-Consecutive-Ones-III/1004. Max Consecutive Ones III_test.go
package leetcode import ( "fmt" "testing" ) type question1004 struct { para1004 ans1004 } // para 是参数 // one 代表第一个参数 type para1004 struct { s []int k int } // ans 是答案 // one 代表第一个答案 type ans1004 struct { one int } func Test_Problem1004(t *testing.T) { qs := []question1004{ { para1004{[]int{1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0}, 2}, ans1004{6}, }, { para1004{[]int{0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1}, 3}, ans1004{10}, }, { para1004{[]int{0, 0, 0, 1}, 4}, ans1004{4}, }, } fmt.Printf("------------------------Leetcode Problem 1004------------------------\n") for _, q := range qs { _, p := q.ans1004, q.para1004 fmt.Printf("【input】:%v 【output】:%v\n", p, longestOnes(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/1004.Max-Consecutive-Ones-III/1004. Max Consecutive Ones III.go
leetcode/1004.Max-Consecutive-Ones-III/1004. Max Consecutive Ones III.go
package leetcode func longestOnes(A []int, K int) int { res, left, right := 0, 0, 0 for left < len(A) { if right < len(A) && ((A[right] == 0 && K > 0) || A[right] == 1) { if A[right] == 0 { K-- } right++ } else { if K == 0 || (right == len(A) && K > 0) { res = max(res, right-left) } if A[left] == 0 { K++ } 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/0055.Jump-Game/55. Jump Game_test.go
leetcode/0055.Jump-Game/55. Jump Game_test.go
package leetcode import ( "fmt" "testing" ) type question55 struct { para55 ans55 } // para 是参数 // one 代表第一个参数 type para55 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans55 struct { one bool } func Test_Problem55(t *testing.T) { qs := []question55{ { para55{[]int{2, 3, 1, 1, 4}}, ans55{true}, }, { para55{[]int{3, 2, 1, 0, 4}}, ans55{false}, }, } fmt.Printf("------------------------Leetcode Problem 55------------------------\n") for _, q := range qs { _, p := q.ans55, q.para55 fmt.Printf("【input】:%v 【output】:%v\n", p, canJump(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/0055.Jump-Game/55. Jump Game.go
leetcode/0055.Jump-Game/55. Jump Game.go
package leetcode func canJump(nums []int) bool { n := len(nums) if n == 0 { return false } if n == 1 { return true } maxJump := 0 for i, v := range nums { if i > maxJump { return false } maxJump = max(maxJump, i+v) } return true } 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/0992.Subarrays-with-K-Different-Integers/992. Subarrays with K Different Integers.go
leetcode/0992.Subarrays-with-K-Different-Integers/992. Subarrays with K Different Integers.go
package leetcode func subarraysWithKDistinct(A []int, K int) int { return subarraysWithKDistinctSlideWindow(A, K) - subarraysWithKDistinctSlideWindow(A, K-1) } func subarraysWithKDistinctSlideWindow(A []int, K int) int { left, right, counter, res, freq := 0, 0, K, 0, map[int]int{} for right = 0; right < len(A); right++ { if freq[A[right]] == 0 { counter-- } freq[A[right]]++ for counter < 0 { freq[A[left]]-- if freq[A[left]] == 0 { counter++ } left++ } res += right - left + 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/0992.Subarrays-with-K-Different-Integers/992. Subarrays with K Different Integers_test.go
leetcode/0992.Subarrays-with-K-Different-Integers/992. Subarrays with K Different Integers_test.go
package leetcode import ( "fmt" "testing" ) type question992 struct { para992 ans992 } // para 是参数 // one 代表第一个参数 type para992 struct { one []int k int } // ans 是答案 // one 代表第一个答案 type ans992 struct { one int } func Test_Problem992(t *testing.T) { qs := []question992{ { para992{[]int{1, 1, 1, 1, 1, 1, 1, 1}, 1}, ans992{36}, }, { para992{[]int{2, 1, 1, 1, 2}, 1}, ans992{8}, }, { para992{[]int{1, 2}, 1}, ans992{2}, }, { para992{[]int{1, 2, 1, 2, 3}, 2}, ans992{7}, }, { para992{[]int{1, 2, 1, 3, 4}, 3}, ans992{3}, }, { para992{[]int{1}, 5}, ans992{1}, }, { para992{[]int{}, 10}, ans992{0}, }, } fmt.Printf("------------------------Leetcode Problem 992------------------------\n") for _, q := range qs { _, p := q.ans992, q.para992 fmt.Printf("【input】:%v 【output】:%v\n", p, subarraysWithKDistinct(p.one, 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/0037.Sudoku-Solver/37. Sudoku Solver_test.go
leetcode/0037.Sudoku-Solver/37. Sudoku Solver_test.go
package leetcode import ( "fmt" "testing" ) type question37 struct { para37 ans37 } // para 是参数 // one 代表第一个参数 type para37 struct { s [][]byte } // ans 是答案 // one 代表第一个答案 type ans37 struct { s [][]byte } func Test_Problem37(t *testing.T) { qs := []question37{ { para37{[][]byte{ {'5', '3', '.', '.', '7', '.', '.', '.', '.'}, {'6', '.', '.', '1', '9', '5', '.', '.', '.'}, {'.', '9', '8', '.', '.', '.', '.', '6', '.'}, {'8', '.', '.', '.', '6', '.', '.', '.', '3'}, {'4', '.', '.', '8', '.', '3', '.', '.', '1'}, {'7', '.', '.', '.', '2', '.', '.', '.', '6'}, {'.', '6', '.', '.', '.', '.', '2', '8', '.'}, {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, {'.', '.', '.', '.', '8', '.', '.', '7', '9'}}}, ans37{[][]byte{ {'5', '3', '4', '6', '7', '8', '9', '1', '2'}, {'6', '7', '2', '1', '9', '5', '3', '4', '8'}, {'1', '9', '8', '3', '4', '2', '5', '6', '7'}, {'8', '5', '9', '7', '6', '1', '4', '2', '3'}, {'4', '2', '6', '8', '5', '3', '7', '9', '1'}, {'7', '1', '3', '9', '2', '4', '8', '5', '6'}, {'9', '6', '1', '5', '3', '7', '2', '8', '4'}, {'2', '8', '7', '4', '1', '9', '6', '3', '5'}, {'3', '4', '5', '2', '8', '6', '1', '7', '9'}}}, }, } fmt.Printf("------------------------Leetcode Problem 37------------------------\n") for _, q := range qs { _, p := q.ans37, q.para37 fmt.Printf("【input】:%v \n\n", p) solveSudoku(p.s) fmt.Printf("【output】:%v \n\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/0037.Sudoku-Solver/37. Sudoku Solver.go
leetcode/0037.Sudoku-Solver/37. Sudoku Solver.go
package leetcode type position struct { x int y int } func solveSudoku(board [][]byte) { pos, find := []position{}, false for i := 0; i < len(board); i++ { for j := 0; j < len(board[0]); j++ { if board[i][j] == '.' { pos = append(pos, position{x: i, y: j}) } } } putSudoku(&board, pos, 0, &find) } func putSudoku(board *[][]byte, pos []position, index int, succ *bool) { if *succ == true { return } if index == len(pos) { *succ = true return } for i := 1; i < 10; i++ { if checkSudoku(board, pos[index], i) && !*succ { (*board)[pos[index].x][pos[index].y] = byte(i) + '0' putSudoku(board, pos, index+1, succ) if *succ == true { return } (*board)[pos[index].x][pos[index].y] = '.' } } } func checkSudoku(board *[][]byte, pos position, val int) bool { // 判断横行是否有重复数字 for i := 0; i < len((*board)[0]); i++ { if (*board)[pos.x][i] != '.' && int((*board)[pos.x][i]-'0') == val { return false } } // 判断竖行是否有重复数字 for i := 0; i < len((*board)); i++ { if (*board)[i][pos.y] != '.' && int((*board)[i][pos.y]-'0') == val { return false } } // 判断九宫格是否有重复数字 posx, posy := pos.x-pos.x%3, pos.y-pos.y%3 for i := posx; i < posx+3; i++ { for j := posy; j < posy+3; j++ { if (*board)[i][j] != '.' && int((*board)[i][j]-'0') == val { return false } } } return true }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0377.Combination-Sum-IV/377. Combination Sum IV_test.go
leetcode/0377.Combination-Sum-IV/377. Combination Sum IV_test.go
package leetcode import ( "fmt" "testing" ) type question377 struct { para377 ans377 } // para 是参数 // one 代表第一个参数 type para377 struct { n []int k int } // ans 是答案 // one 代表第一个答案 type ans377 struct { one int } func Test_Problem377(t *testing.T) { qs := []question377{ { para377{[]int{1, 2, 3}, 4}, ans377{7}, }, { para377{[]int{1, 2, 3}, 32}, ans377{181997601}, }, } fmt.Printf("------------------------Leetcode Problem 377------------------------\n") for _, q := range qs { _, p := q.ans377, q.para377 fmt.Printf("【input】:%v 【output】:%v\n", p, combinationSum4(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/0377.Combination-Sum-IV/377. Combination Sum IV.go
leetcode/0377.Combination-Sum-IV/377. Combination Sum IV.go
package leetcode func combinationSum4(nums []int, target int) int { dp := make([]int, target+1) dp[0] = 1 for i := 1; i <= target; i++ { for _, num := range nums { if i-num >= 0 { dp[i] += dp[i-num] } } } return dp[target] } // 暴力解法超时 func combinationSum41(nums []int, target int) int { if len(nums) == 0 { return 0 } c, res := []int{}, 0 findcombinationSum4(nums, target, 0, c, &res) return res } func findcombinationSum4(nums []int, target, index int, c []int, res *int) { if target <= 0 { if target == 0 { *res++ } return } for i := 0; i < len(nums); i++ { c = append(c, nums[i]) findcombinationSum4(nums, target-nums[i], i, c, res) c = c[:len(c)-1] } }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0916.Word-Subsets/916. Word Subsets_test.go
leetcode/0916.Word-Subsets/916. Word Subsets_test.go
package leetcode import ( "fmt" "testing" ) type question916 struct { para916 ans916 } // para 是参数 // one 代表第一个参数 type para916 struct { A []string B []string } // ans 是答案 // one 代表第一个答案 type ans916 struct { one []string } func Test_Problem916(t *testing.T) { qs := []question916{ { para916{[]string{"amazon", "apple", "facebook", "google", "leetcode"}, []string{"e", "o"}}, ans916{[]string{"facebook", "google", "leetcode"}}, }, { para916{[]string{"amazon", "apple", "facebook", "google", "leetcode"}, []string{"l", "e"}}, ans916{[]string{"apple", "google", "leetcode"}}, }, { para916{[]string{"amazon", "apple", "facebook", "google", "leetcode"}, []string{"e", "oo"}}, ans916{[]string{"facebook", "google"}}, }, { para916{[]string{"amazon", "apple", "facebook", "google", "leetcode"}, []string{"lo", "eo"}}, ans916{[]string{"google", "leetcode"}}, }, { para916{[]string{"amazon", "apple", "facebook", "google", "leetcode"}, []string{"ec", "oc", "ceo"}}, ans916{[]string{"facebook", "leetcode"}}, }, } fmt.Printf("------------------------Leetcode Problem 916------------------------\n") for _, q := range qs { _, p := q.ans916, q.para916 fmt.Printf("【input】:%v 【output】:%v\n", p, wordSubsets(p.A, p.B)) } 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/0916.Word-Subsets/916. Word Subsets.go
leetcode/0916.Word-Subsets/916. Word Subsets.go
package leetcode func wordSubsets(A []string, B []string) []string { var counter [26]int for _, b := range B { var m [26]int for _, c := range b { j := c - 'a' m[j]++ } for i := 0; i < 26; i++ { if m[i] > counter[i] { counter[i] = m[i] } } } var res []string for _, a := range A { var m [26]int for _, c := range a { j := c - 'a' m[j]++ } ok := true for i := 0; i < 26; i++ { if m[i] < counter[i] { ok = false break } } if ok { res = append(res, a) } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1600.Throne-Inheritance/1600. Throne Inheritance.go
leetcode/1600.Throne-Inheritance/1600. Throne Inheritance.go
package leetcode type ThroneInheritance struct { king string edges map[string][]string dead map[string]bool } func Constructor(kingName string) (t ThroneInheritance) { return ThroneInheritance{kingName, map[string][]string{}, map[string]bool{}} } func (t *ThroneInheritance) Birth(parentName, childName string) { t.edges[parentName] = append(t.edges[parentName], childName) } func (t *ThroneInheritance) Death(name string) { t.dead[name] = true } func (t *ThroneInheritance) GetInheritanceOrder() (res []string) { var preorder func(string) preorder = func(name string) { if !t.dead[name] { res = append(res, name) } for _, childName := range t.edges[name] { preorder(childName) } } preorder(t.king) return } /** * Your ThroneInheritance object will be instantiated and called as such: * obj := Constructor(kingName); * obj.Birth(parentName,childName); * obj.Death(name); * param_3 := obj.GetInheritanceOrder(); */
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1600.Throne-Inheritance/1600. Throne Inheritance_test.go
leetcode/1600.Throne-Inheritance/1600. Throne Inheritance_test.go
package leetcode import ( "fmt" "testing" ) func Test_Problem1600(t *testing.T) { obj := Constructor("king") fmt.Printf("obj = %v\n", obj) obj.Birth("king", "andy") fmt.Printf("obj = %v\n", obj) obj.Birth("king", "bob") fmt.Printf("obj = %v\n", obj) obj.Birth("king", "catherine") fmt.Printf("obj = %v\n", obj) obj.Birth("andy", "matthew") fmt.Printf("obj = %v\n", obj) obj.Birth("bob", "alex") fmt.Printf("obj = %v\n", obj) obj.Birth("bob", "asha") fmt.Printf("obj = %v\n", obj) param2 := obj.GetInheritanceOrder() fmt.Printf("param_2 = %v obj = %v\n", param2, obj) obj.Death("bob") fmt.Printf("obj = %v\n", obj) param2 = obj.GetInheritanceOrder() fmt.Printf("param_2 = %v obj = %v\n", param2, obj) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0030.Substring-with-Concatenation-of-All-Words/30. Substring with Concatenation of All Words_test.go
leetcode/0030.Substring-with-Concatenation-of-All-Words/30. Substring with Concatenation of All Words_test.go
package leetcode import ( "fmt" "testing" ) type question30 struct { para30 ans30 } // para 是参数 // one 代表第一个参数 type para30 struct { one string two []string } // ans 是答案 // one 代表第一个答案 type ans30 struct { one []int } func Test_Problem30(t *testing.T) { qs := []question30{ { para30{"aaaaaaaa", []string{"aa", "aa", "aa"}}, ans30{[]int{0, 1, 2}}, }, { para30{"barfoothefoobarman", []string{"foo", "bar"}}, ans30{[]int{0, 9}}, }, { para30{"wordgoodgoodgoodbestword", []string{"word", "good", "best", "word"}}, ans30{[]int{}}, }, { para30{"goodgoodgoodgoodgood", []string{"good"}}, ans30{[]int{0, 4, 8, 12, 16}}, }, { para30{"barofoothefoolbarman", []string{"foo", "bar"}}, ans30{[]int{}}, }, { para30{"bbarffoothefoobarman", []string{"foo", "bar"}}, ans30{[]int{}}, }, { para30{"ooroodoofoodtoo", []string{"foo", "doo", "roo", "tee", "oo"}}, ans30{[]int{}}, }, { para30{"abc", []string{"a", "b", "c"}}, ans30{[]int{0}}, }, { para30{"a", []string{"b"}}, ans30{[]int{}}, }, { para30{"ab", []string{"ba"}}, ans30{[]int{}}, }, { para30{"n", []string{}}, ans30{[]int{}}, }, } fmt.Printf("------------------------Leetcode Problem 30------------------------\n") for _, q := range qs { _, p := q.ans30, q.para30 fmt.Printf("【input】:%v 【output】:%v\n", p, findSubstring(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/0030.Substring-with-Concatenation-of-All-Words/30. Substring with Concatenation of All Words.go
leetcode/0030.Substring-with-Concatenation-of-All-Words/30. Substring with Concatenation of All Words.go
package leetcode func findSubstring(s string, words []string) []int { if len(words) == 0 { return []int{} } res := []int{} counter := map[string]int{} for _, w := range words { counter[w]++ } length, totalLen, tmpCounter := len(words[0]), len(words[0])*len(words), copyMap(counter) for i, start := 0, 0; i < len(s)-length+1 && start < len(s)-length+1; i++ { //fmt.Printf("sub = %v i = %v lenght = %v start = %v tmpCounter = %v totalLen = %v\n", s[i:i+length], i, length, start, tmpCounter, totalLen) if tmpCounter[s[i:i+length]] > 0 { tmpCounter[s[i:i+length]]-- //fmt.Printf("******sub = %v i = %v lenght = %v start = %v tmpCounter = %v totalLen = %v\n", s[i:i+length], i, length, start, tmpCounter, totalLen) if checkWords(tmpCounter) && (i+length-start == totalLen) { res = append(res, start) continue } i = i + length - 1 } else { start++ i = start - 1 tmpCounter = copyMap(counter) } } return res } func checkWords(s map[string]int) bool { flag := true for _, v := range s { if v > 0 { flag = false break } } return flag } func copyMap(s map[string]int) map[string]int { c := map[string]int{} for k, v := range s { c[k] = v } return c }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0064.Minimum-Path-Sum/64. Minimum Path Sum.go
leetcode/0064.Minimum-Path-Sum/64. Minimum Path Sum.go
package leetcode // 解法一 原地 DP,无辅助空间 func minPathSum(grid [][]int) int { m, n := len(grid), len(grid[0]) for i := 1; i < m; i++ { grid[i][0] += grid[i-1][0] } for j := 1; j < n; j++ { grid[0][j] += grid[0][j-1] } for i := 1; i < m; i++ { for j := 1; j < n; j++ { grid[i][j] += min(grid[i-1][j], grid[i][j-1]) } } return grid[m-1][n-1] } // 解法二 最原始的方法,辅助空间 O(n^2) func minPathSum1(grid [][]int) int { if len(grid) == 0 { return 0 } m, n := len(grid), len(grid[0]) if m == 0 || n == 0 { return 0 } dp := make([][]int, m) for i := 0; i < m; i++ { dp[i] = make([]int, n) } // initFirstCol for i := 0; i < len(dp); i++ { if i == 0 { dp[i][0] = grid[i][0] } else { dp[i][0] = grid[i][0] + dp[i-1][0] } } // initFirstRow for i := 0; i < len(dp[0]); i++ { if i == 0 { dp[0][i] = grid[0][i] } else { dp[0][i] = grid[0][i] + dp[0][i-1] } } for i := 1; i < m; i++ { for j := 1; j < n; j++ { dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j] } } return dp[m-1][n-1] } func min(a int, b int) int { if a > b { return b } return a }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0064.Minimum-Path-Sum/64. Minimum Path Sum_test.go
leetcode/0064.Minimum-Path-Sum/64. Minimum Path Sum_test.go
package leetcode import ( "fmt" "testing" ) type question64 struct { para64 ans64 } // para 是参数 // one 代表第一个参数 type para64 struct { og [][]int } // ans 是答案 // one 代表第一个答案 type ans64 struct { one int } func Test_Problem64(t *testing.T) { qs := []question64{ { para64{[][]int{ {1, 3, 1}, {1, 5, 1}, {4, 2, 1}, }}, ans64{7}, }, } fmt.Printf("------------------------Leetcode Problem 64------------------------\n") for _, q := range qs { _, p := q.ans64, q.para64 fmt.Printf("【input】:%v 【output】:%v\n", p, minPathSum(p.og)) } 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/0231.Power-of-Two/231. Power of Two_test.go
leetcode/0231.Power-of-Two/231. Power of Two_test.go
package leetcode import ( "fmt" "testing" ) type question231 struct { para231 ans231 } // para 是参数 // one 代表第一个参数 type para231 struct { one int } // ans 是答案 // one 代表第一个答案 type ans231 struct { one bool } func Test_Problem231(t *testing.T) { qs := []question231{ { para231{1}, ans231{true}, }, { para231{16}, ans231{true}, }, { para231{218}, ans231{false}, }, } fmt.Printf("------------------------Leetcode Problem 231------------------------\n") for _, q := range qs { _, p := q.ans231, q.para231 fmt.Printf("【input】:%v 【output】:%v\n", p, isPowerOfTwo(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/0231.Power-of-Two/231. Power of Two.go
leetcode/0231.Power-of-Two/231. Power of Two.go
package leetcode // 解法一 二进制位操作法 func isPowerOfTwo(num int) bool { return (num > 0 && ((num & (num - 1)) == 0)) } // 解法二 数论 func isPowerOfTwo1(num int) bool { return num > 0 && (1073741824%num == 0) } // 解法三 打表法 func isPowerOfTwo2(num int) bool { allPowerOfTwoMap := map[int]int{1: 1, 2: 2, 4: 4, 8: 8, 16: 16, 32: 32, 64: 64, 128: 128, 256: 256, 512: 512, 1024: 1024, 2048: 2048, 4096: 4096, 8192: 8192, 16384: 16384, 32768: 32768, 65536: 65536, 131072: 131072, 262144: 262144, 524288: 524288, 1048576: 1048576, 2097152: 2097152, 4194304: 4194304, 8388608: 8388608, 16777216: 16777216, 33554432: 33554432, 67108864: 67108864, 134217728: 134217728, 268435456: 268435456, 536870912: 536870912, 1073741824: 1073741824} _, ok := allPowerOfTwoMap[num] return ok } // 解法四 循环 func isPowerOfTwo3(num int) bool { for num >= 2 { if num%2 == 0 { num = num / 2 } else { return false } } return num == 1 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1696.Jump-Game-VI/1696. Jump Game VI.go
leetcode/1696.Jump-Game-VI/1696. Jump Game VI.go
package leetcode import ( "math" ) // 单调队列 func maxResult(nums []int, k int) int { dp := make([]int, len(nums)) dp[0] = nums[0] for i := 1; i < len(dp); i++ { dp[i] = math.MinInt32 } window := make([]int, k) for i := 1; i < len(nums); i++ { dp[i] = nums[i] + dp[window[0]] for len(window) > 0 && dp[window[len(window)-1]] <= dp[i] { window = window[:len(window)-1] } for len(window) > 0 && i-k >= window[0] { window = window[1:] } window = append(window, i) } return dp[len(nums)-1] } // 超时 func maxResult1(nums []int, k int) int { dp := make([]int, len(nums)) if k > len(nums) { k = len(nums) } dp[0] = nums[0] for i := 1; i < len(dp); i++ { dp[i] = math.MinInt32 } for i := 1; i < len(nums); i++ { left, tmp := max(0, i-k), math.MinInt32 for j := left; j < i; j++ { tmp = max(tmp, dp[j]) } dp[i] = nums[i] + tmp } return dp[len(nums)-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/1696.Jump-Game-VI/1696. Jump Game VI_test.go
leetcode/1696.Jump-Game-VI/1696. Jump Game VI_test.go
package leetcode import ( "fmt" "testing" ) type question1696 struct { para1696 ans1696 } // para 是参数 // one 代表第一个参数 type para1696 struct { nums []int k int } // ans 是答案 // one 代表第一个答案 type ans1696 struct { one int } func Test_Problem1696(t *testing.T) { qs := []question1696{ { para1696{[]int{1, -1, -2, 4, -7, 3}, 2}, ans1696{7}, }, { para1696{[]int{10, -5, -2, 4, 0, 3}, 3}, ans1696{17}, }, { para1696{[]int{1, -5, -20, 4, -1, 3, -6, -3}, 2}, ans1696{0}, }, } fmt.Printf("------------------------Leetcode Problem 1696------------------------\n") for _, q := range qs { _, p := q.ans1696, q.para1696 fmt.Printf("【input】:%v 【output】:%v\n", p, maxResult(p.nums, 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/1051.Height-Checker/1051. Height Checker.go
leetcode/1051.Height-Checker/1051. Height Checker.go
package leetcode import "sort" func heightChecker(heights []int) int { result, checker := 0, []int{} checker = append(checker, heights...) sort.Ints(checker) for i := 0; i < len(heights); i++ { if heights[i] != checker[i] { result++ } } return result }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1051.Height-Checker/1051. Height Checker_test.go
leetcode/1051.Height-Checker/1051. Height Checker_test.go
package leetcode import ( "fmt" "testing" ) type question1051 struct { para1051 ans1051 } // para 是参数 // one 代表第一个参数 type para1051 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans1051 struct { one int } func Test_Problem1051(t *testing.T) { qs := []question1051{ { para1051{[]int{1, 1, 4, 2, 1, 3}}, ans1051{3}, }, { para1051{[]int{5, 1, 2, 3, 4}}, ans1051{5}, }, { para1051{[]int{1, 2, 3, 4, 5}}, ans1051{0}, }, { para1051{[]int{5, 4, 3, 2, 1}}, ans1051{4}, }, } fmt.Printf("------------------------Leetcode Problem 1051------------------------\n") for _, q := range qs { _, p := q.ans1051, q.para1051 fmt.Printf("【input】:%v 【output】:%v\n", p, heightChecker(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/0236.Lowest-Common-Ancestor-of-a-Binary-Tree/236. Lowest Common Ancestor of a Binary Tree.go
leetcode/0236.Lowest-Common-Ancestor-of-a-Binary-Tree/236. Lowest Common Ancestor of a 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 lowestCommonAncestor236(root, p, q *TreeNode) *TreeNode { if root == nil || root == q || root == p { return root } left := lowestCommonAncestor236(root.Left, p, q) right := lowestCommonAncestor236(root.Right, p, q) if left != nil { if right != nil { return root } return left } return right }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0236.Lowest-Common-Ancestor-of-a-Binary-Tree/236. Lowest Common Ancestor of a Binary Tree_test.go
leetcode/0236.Lowest-Common-Ancestor-of-a-Binary-Tree/236. Lowest Common Ancestor of a Binary Tree_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question236 struct { para236 ans236 } // para 是参数 // one 代表第一个参数 type para236 struct { one []int two []int thr []int } // ans 是答案 // one 代表第一个答案 type ans236 struct { one []int } func Test_Problem236(t *testing.T) { qs := []question236{ { para236{[]int{}, []int{}, []int{}}, ans236{[]int{}}, }, { para236{[]int{3, 5, 1, 6, 2, 0, 8, structures.NULL, structures.NULL, 7, 4}, []int{5}, []int{1}}, ans236{[]int{3}}, }, { para236{[]int{3, 5, 1, 6, 2, 0, 8, structures.NULL, structures.NULL, 7, 4}, []int{5}, []int{4}}, ans236{[]int{5}}, }, { para236{[]int{6, 2, 8, 0, 4, 7, 9, structures.NULL, structures.NULL, 3, 5}, []int{2}, []int{8}}, ans236{[]int{6}}, }, { para236{[]int{6, 2, 8, 0, 4, 7, 9, structures.NULL, structures.NULL, 3, 5}, []int{2}, []int{4}}, ans236{[]int{2}}, }, } fmt.Printf("------------------------Leetcode Problem 236------------------------\n") for _, q := range qs { _, p := q.ans236, q.para236 fmt.Printf("【input】:%v ", p) rootOne := structures.Ints2TreeNode(p.one) rootTwo := structures.Ints2TreeNode(p.two) rootThr := structures.Ints2TreeNode(p.thr) fmt.Printf("【output】:%v \n", lowestCommonAncestor236(rootOne, rootTwo, rootThr)) } 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/1123.Lowest-Common-Ancestor-of-Deepest-Leaves/1123. Lowest Common Ancestor of Deepest Leaves.go
leetcode/1123.Lowest-Common-Ancestor-of-Deepest-Leaves/1123. Lowest Common Ancestor of Deepest Leaves.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 lcaDeepestLeaves(root *TreeNode) *TreeNode { if root == nil { return nil } lca, maxLevel := &TreeNode{}, 0 lcaDeepestLeavesDFS(&lca, &maxLevel, 0, root) return lca } func lcaDeepestLeavesDFS(lca **TreeNode, maxLevel *int, depth int, root *TreeNode) int { *maxLevel = max(*maxLevel, depth) if root == nil { return depth } depthLeft := lcaDeepestLeavesDFS(lca, maxLevel, depth+1, root.Left) depthRight := lcaDeepestLeavesDFS(lca, maxLevel, depth+1, root.Right) if depthLeft == *maxLevel && depthRight == *maxLevel { *lca = root } return max(depthLeft, depthRight) } 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/1123.Lowest-Common-Ancestor-of-Deepest-Leaves/1123. Lowest Common Ancestor of Deepest Leaves_test.go
leetcode/1123.Lowest-Common-Ancestor-of-Deepest-Leaves/1123. Lowest Common Ancestor of Deepest Leaves_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question1123 struct { para1123 ans1123 } // para 是参数 // one 代表第一个参数 type para1123 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans1123 struct { one []int } func Test_Problem1123(t *testing.T) { qs := []question1123{ { para1123{[]int{}}, ans1123{[]int{}}, }, { para1123{[]int{1}}, ans1123{[]int{1}}, }, { para1123{[]int{1, 2, 3, 4}}, ans1123{[]int{4}}, }, { para1123{[]int{1, 2, 3}}, ans1123{[]int{1, 2, 3}}, }, { para1123{[]int{1, 2, 3, 4, 5}}, ans1123{[]int{2, 4, 5}}, }, { para1123{[]int{1, 2, structures.NULL, 3, 4, structures.NULL, 6, structures.NULL, 5}}, ans1123{[]int{2, 3, 4, structures.NULL, 6, structures.NULL, 5}}, }, } fmt.Printf("------------------------Leetcode Problem 1123------------------------\n") for _, q := range qs { _, p := q.ans1123, q.para1123 fmt.Printf("【input】:%v ", p) root := structures.Ints2TreeNode(p.one) fmt.Printf("【output】:%v \n", structures.Tree2ints(lcaDeepestLeaves(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/1329.Sort-the-Matrix-Diagonally/1329. Sort the Matrix Diagonally_test.go
leetcode/1329.Sort-the-Matrix-Diagonally/1329. Sort the Matrix Diagonally_test.go
package leetcode import ( "fmt" "testing" ) type question1329 struct { para1329 ans1329 } // para 是参数 // one 代表第一个参数 type para1329 struct { mat [][]int } // ans 是答案 // one 代表第一个答案 type ans1329 struct { one [][]int } func Test_Problem1329(t *testing.T) { qs := []question1329{ { para1329{[][]int{{3, 3, 1, 1}, {2, 2, 1, 2}, {1, 1, 1, 2}}}, ans1329{[][]int{{1, 1, 1, 1}, {1, 2, 2, 2}, {1, 2, 3, 3}}}, }, } fmt.Printf("------------------------Leetcode Problem 1329------------------------\n") for _, q := range qs { _, p := q.ans1329, q.para1329 fmt.Printf("【input】:%v 【output】:%v\n", p, diagonalSort(p.mat)) } 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/1329.Sort-the-Matrix-Diagonally/1329. Sort the Matrix Diagonally.go
leetcode/1329.Sort-the-Matrix-Diagonally/1329. Sort the Matrix Diagonally.go
package leetcode import ( "sort" ) func diagonalSort(mat [][]int) [][]int { m, n, diagonalsMap := len(mat), len(mat[0]), make(map[int][]int) for i := 0; i < m; i++ { for j := 0; j < n; j++ { diagonalsMap[i-j] = append(diagonalsMap[i-j], mat[i][j]) } } for _, v := range diagonalsMap { sort.Ints(v) } for i := 0; i < m; i++ { for j := 0; j < n; j++ { mat[i][j] = diagonalsMap[i-j][0] diagonalsMap[i-j] = diagonalsMap[i-j][1:] } } return mat }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false