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/0200.Number-of-Islands/200. Number of Islands_test.go
leetcode/0200.Number-of-Islands/200. Number of Islands_test.go
package leetcode import ( "fmt" "testing" ) type question200 struct { para200 ans200 } // para 是参数 // one 代表第一个参数 type para200 struct { one [][]byte } // ans 是答案 // one 代表第一个答案 type ans200 struct { one int } func Test_Problem200(t *testing.T) { qs := []question200{ { para200{[][]byte{ {'1', '1', '1', '1', '0'}, {'1', '1', '0', '1', '0'}, {'1', '1', '0', '0', '0'}, {'0', '0', '0', '0', '0'}, }}, ans200{1}, }, { para200{[][]byte{ {'1', '1', '0', '0', '0'}, {'1', '1', '0', '0', '0'}, {'0', '0', '1', '0', '0'}, {'0', '0', '0', '1', '1'}, }}, ans200{3}, }, } fmt.Printf("------------------------Leetcode Problem 200------------------------\n") for _, q := range qs { _, p := q.ans200, q.para200 fmt.Printf("【input】:%v 【output】:%v\n", p, numIslands(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/0200.Number-of-Islands/200. Number of Islands.go
leetcode/0200.Number-of-Islands/200. Number of Islands.go
package leetcode var dir = [][]int{ {-1, 0}, {0, 1}, {1, 0}, {0, -1}, } func numIslands(grid [][]byte) int { m := len(grid) if m == 0 { return 0 } n := len(grid[0]) if n == 0 { return 0 } res, visited := 0, make([][]bool, m) for i := 0; i < m; i++ { visited[i] = make([]bool, n) } for i := 0; i < m; i++ { for j := 0; j < n; j++ { if grid[i][j] == '1' && !visited[i][j] { searchIslands(grid, &visited, i, j) res++ } } } return res } func searchIslands(grid [][]byte, visited *[][]bool, x, y int) { (*visited)[x][y] = true for i := 0; i < 4; i++ { nx := x + dir[i][0] ny := y + dir[i][1] if isInBoard(grid, nx, ny) && !(*visited)[nx][ny] && grid[nx][ny] == '1' { searchIslands(grid, visited, nx, ny) } } } func isInBoard(board [][]byte, x, y int) bool { return x >= 0 && x < len(board) && y >= 0 && y < len(board[0]) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0744.Find-Smallest-Letter-Greater-Than-Target/744. Find Smallest Letter Greater Than Target.go
leetcode/0744.Find-Smallest-Letter-Greater-Than-Target/744. Find Smallest Letter Greater Than Target.go
package leetcode func nextGreatestLetter(letters []byte, target byte) byte { low, high := 0, len(letters)-1 for low <= high { mid := low + (high-low)>>1 if letters[mid] > target { high = mid - 1 } else { low = mid + 1 } } find := letters[low%len(letters)] if find <= target { return letters[0] } return find }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0744.Find-Smallest-Letter-Greater-Than-Target/744. Find Smallest Letter Greater Than Target_test.go
leetcode/0744.Find-Smallest-Letter-Greater-Than-Target/744. Find Smallest Letter Greater Than Target_test.go
package leetcode import ( "fmt" "testing" ) type question744 struct { para744 ans744 } // para 是参数 // one 代表第一个参数 type para744 struct { letters []byte target byte } // ans 是答案 // one 代表第一个答案 type ans744 struct { one byte } func Test_Problem744(t *testing.T) { qs := []question744{ { para744{[]byte{'c', 'f', 'j'}, 'a'}, ans744{'c'}, }, { para744{[]byte{'c', 'f', 'j'}, 'c'}, ans744{'f'}, }, { para744{[]byte{'c', 'f', 'j'}, 'd'}, ans744{'f'}, }, { para744{[]byte{'c', 'f', 'j'}, 'g'}, ans744{'j'}, }, { para744{[]byte{'c', 'f', 'j'}, 'j'}, ans744{'c'}, }, { para744{[]byte{'c', 'f', 'j'}, 'k'}, ans744{'c'}, }, } fmt.Printf("------------------------Leetcode Problem 744------------------------\n") for _, q := range qs { _, p := q.ans744, q.para744 fmt.Printf("【input】:%v 【output】:%v\n", p, nextGreatestLetter(p.letters, 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/1609.Even-Odd-Tree/1609.Even Odd Tree.go
leetcode/1609.Even-Odd-Tree/1609.Even Odd Tree.go
package leetcode type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func isEvenOddTree(root *TreeNode) bool { level := 0 queue := []*TreeNode{root} for len(queue) != 0 { length := len(queue) var nums []int for i := 0; i < length; i++ { node := queue[i] if node.Left != nil { queue = append(queue, node.Left) } if node.Right != nil { queue = append(queue, node.Right) } nums = append(nums, node.Val) } if level%2 == 0 { if !even(nums) { return false } } else { if !odd(nums) { return false } } queue = queue[length:] level++ } return true } func odd(nums []int) bool { cur := nums[0] if cur%2 != 0 { return false } for _, num := range nums[1:] { if num >= cur { return false } if num%2 != 0 { return false } cur = num } return true } func even(nums []int) bool { cur := nums[0] if cur%2 == 0 { return false } for _, num := range nums[1:] { if num <= cur { return false } if num%2 == 0 { return false } cur = num } return true }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1609.Even-Odd-Tree/1609.Even Odd Tree_test.go
leetcode/1609.Even-Odd-Tree/1609.Even Odd Tree_test.go
package leetcode import ( "fmt" "testing" ) type question1609 struct { para1609 ans1609 } // para 是参数 type para1609 struct { root *TreeNode } // ans 是答案 type ans1609 struct { ans bool } func Test_Problem1609(t *testing.T) { qs := []question1609{ { para1609{&TreeNode{1, &TreeNode{10, &TreeNode{3, &TreeNode{12, nil, nil}, &TreeNode{8, nil, nil}}, nil}, &TreeNode{4, &TreeNode{7, &TreeNode{6, nil, nil}, nil}, &TreeNode{9, nil, &TreeNode{2, nil, nil}}}}}, ans1609{true}, }, { para1609{&TreeNode{5, &TreeNode{4, &TreeNode{3, nil, nil}, &TreeNode{3, nil, nil}}, &TreeNode{2, &TreeNode{7, nil, nil}, nil}}}, ans1609{false}, }, { para1609{&TreeNode{5, &TreeNode{9, &TreeNode{3, nil, nil}, &TreeNode{5, nil, nil}}, &TreeNode{1, &TreeNode{7, nil, nil}, nil}}}, ans1609{false}, }, { para1609{&TreeNode{1, nil, nil}}, ans1609{true}, }, { para1609{&TreeNode{11, &TreeNode{8, &TreeNode{1, &TreeNode{30, &TreeNode{17, nil, nil}, nil}, &TreeNode{20, nil, nil}}, &TreeNode{3, &TreeNode{18, nil, nil}, &TreeNode{16, nil, nil}}}, &TreeNode{6, &TreeNode{9, &TreeNode{12, nil, nil}, &TreeNode{10, nil, nil}}, &TreeNode{11, &TreeNode{4, nil, nil}, &TreeNode{2, nil, nil}}}}}, ans1609{true}, }, } fmt.Printf("------------------------Leetcode Problem 1609------------------------\n") for _, q := range qs { _, p := q.ans1609, q.para1609 fmt.Printf("【input】:%v 【output】:%v\n", p.root, isEvenOddTree(p.root)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0923.3Sum-With-Multiplicity/923. 3Sum With Multiplicity_test.go
leetcode/0923.3Sum-With-Multiplicity/923. 3Sum With Multiplicity_test.go
package leetcode import ( "fmt" "testing" ) type question923 struct { para923 ans923 } // para 是参数 // one 代表第一个参数 type para923 struct { a []int t int } // ans 是答案 // one 代表第一个答案 type ans923 struct { one int } func Test_Problem923(t *testing.T) { qs := []question923{ { para923{[]int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}, 8}, ans923{20}, }, { para923{[]int{1, 1, 2, 2, 2, 2}, 5}, ans923{12}, }, } fmt.Printf("------------------------Leetcode Problem 923------------------------\n") for _, q := range qs { _, p := q.ans923, q.para923 fmt.Printf("【input】:%v 【output】:%v\n", p, threeSumMulti(p.a, p.t)) } 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/0923.3Sum-With-Multiplicity/923. 3Sum With Multiplicity.go
leetcode/0923.3Sum-With-Multiplicity/923. 3Sum With Multiplicity.go
package leetcode import ( "sort" ) func threeSumMulti(A []int, target int) int { mod := 1000000007 counter := map[int]int{} for _, value := range A { counter[value]++ } uniqNums := []int{} for key := range counter { uniqNums = append(uniqNums, key) } sort.Ints(uniqNums) res := 0 for i := 0; i < len(uniqNums); i++ { ni := counter[uniqNums[i]] if (uniqNums[i]*3 == target) && counter[uniqNums[i]] >= 3 { res += ni * (ni - 1) * (ni - 2) / 6 } for j := i + 1; j < len(uniqNums); j++ { nj := counter[uniqNums[j]] if (uniqNums[i]*2+uniqNums[j] == target) && counter[uniqNums[i]] > 1 { res += ni * (ni - 1) / 2 * nj } if (uniqNums[j]*2+uniqNums[i] == target) && counter[uniqNums[j]] > 1 { res += nj * (nj - 1) / 2 * ni } c := target - uniqNums[i] - uniqNums[j] if c > uniqNums[j] && counter[c] > 0 { res += ni * nj * counter[c] } } } return res % mod }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1266.Minimum-Time-Visiting-All-Points/1266. Minimum Time Visiting All Points_test.go
leetcode/1266.Minimum-Time-Visiting-All-Points/1266. Minimum Time Visiting All Points_test.go
package leetcode import ( "fmt" "testing" ) type question1266 struct { para1266 ans1266 } // para 是参数 // one 代表第一个参数 type para1266 struct { points [][]int } // ans 是答案 // one 代表第一个答案 type ans1266 struct { one int } func Test_Problem1266(t *testing.T) { qs := []question1266{ { para1266{[][]int{{1, 1}, {3, 4}, {-1, 0}}}, ans1266{7}, }, { para1266{[][]int{{3, 2}, {-2, 2}}}, ans1266{5}, }, } fmt.Printf("------------------------Leetcode Problem 1266------------------------\n") for _, q := range qs { _, p := q.ans1266, q.para1266 fmt.Printf("【input】:%v 【output】:%v\n", p, minTimeToVisitAllPoints(p.points)) } 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/1266.Minimum-Time-Visiting-All-Points/1266. Minimum Time Visiting All Points.go
leetcode/1266.Minimum-Time-Visiting-All-Points/1266. Minimum Time Visiting All Points.go
package leetcode func minTimeToVisitAllPoints(points [][]int) int { res := 0 for i := 1; i < len(points); i++ { res += max(abs(points[i][0]-points[i-1][0]), abs(points[i][1]-points[i-1][1])) } return res } func max(a int, b int) int { if a > b { return a } return b } 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/0060.Permutation-Sequence/60. Permutation Sequence_test.go
leetcode/0060.Permutation-Sequence/60. Permutation Sequence_test.go
package leetcode import ( "fmt" "testing" ) type question60 struct { para60 ans60 } // para 是参数 // one 代表第一个参数 type para60 struct { n int k int } // ans 是答案 // one 代表第一个答案 type ans60 struct { one string } func Test_Problem60(t *testing.T) { qs := []question60{ { para60{3, 3}, ans60{"213"}, }, { para60{4, 9}, ans60{"2314"}, }, } fmt.Printf("------------------------Leetcode Problem 60------------------------\n") for _, q := range qs { _, p := q.ans60, q.para60 fmt.Printf("【input】:%v 【output】:%v\n", p, getPermutation(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/0060.Permutation-Sequence/60. Permutation Sequence.go
leetcode/0060.Permutation-Sequence/60. Permutation Sequence.go
package leetcode import ( "fmt" "strconv" ) func getPermutation(n int, k int) string { if k == 0 { return "" } used, p, res := make([]bool, n), []int{}, "" findPermutation(n, 0, &k, p, &res, &used) return res } func findPermutation(n, index int, k *int, p []int, res *string, used *[]bool) { fmt.Printf("n = %v index = %v k = %v p = %v res = %v user = %v\n", n, index, *k, p, *res, *used) if index == n { *k-- if *k == 0 { for _, v := range p { *res += strconv.Itoa(v + 1) } } return } for i := 0; i < n; i++ { if !(*used)[i] { (*used)[i] = true p = append(p, i) findPermutation(n, index+1, k, p, res, used) p = p[:len(p)-1] (*used)[i] = false } } return }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0910.Smallest-Range-II/910.Smallest Range II_test.go
leetcode/0910.Smallest-Range-II/910.Smallest Range II_test.go
package leetcode import ( "fmt" "testing" ) type question910 struct { para910 ans910 } type para910 struct { A []int K int } type ans910 struct { one int } // Test_Problem910 ... func Test_Problem910(t *testing.T) { qs := []question910{ { para910{[]int{1}, 0}, ans910{0}, }, { para910{[]int{0, 10}, 2}, ans910{6}, }, { para910{[]int{1, 3, 6}, 3}, ans910{3}, }, } fmt.Printf("------------------------Leetcode Problem 910------------------------\n") for _, q := range qs { _, p := q.ans910, q.para910 fmt.Printf("【input】:%v 【output】:%v\n", p, smallestRangeII(p.A, 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/0910.Smallest-Range-II/910.Smallest Range II.go
leetcode/0910.Smallest-Range-II/910.Smallest Range II.go
package leetcode import "sort" func smallestRangeII(A []int, K int) int { n := len(A) sort.Ints(A) res := A[n-1] - A[0] for i := 0; i < n-1; i++ { a, b := A[i], A[i+1] high := max(A[n-1]-K, a+K) low := min(A[0]+K, b-K) res = min(res, high-low) } return res } func max(a, b int) int { if a > b { return a } return b } 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/0137.Single-Number-II/137. Single Number II_test.go
leetcode/0137.Single-Number-II/137. Single Number II_test.go
package leetcode import ( "fmt" "testing" ) type question137 struct { para137 ans137 } // para 是参数 // one 代表第一个参数 type para137 struct { s []int } // ans 是答案 // one 代表第一个答案 type ans137 struct { one int } func Test_Problem137(t *testing.T) { qs := []question137{ { para137{[]int{2, 2, 3, 2}}, ans137{3}, }, { para137{[]int{0, 1, 0, 1, 0, 1, 99}}, ans137{99}, }, } fmt.Printf("------------------------Leetcode Problem 137------------------------\n") for _, q := range qs { _, p := q.ans137, q.para137 fmt.Printf("【input】:%v 【output】:%v\n", p, singleNumberII(p.s)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0137.Single-Number-II/137. Single Number II.go
leetcode/0137.Single-Number-II/137. Single Number II.go
package leetcode func singleNumberII(nums []int) int { ones, twos := 0, 0 for i := 0; i < len(nums); i++ { ones = (ones ^ nums[i]) & ^twos twos = (twos ^ nums[i]) & ^ones } return ones } // 以下是拓展题 // 在数组中每个元素都出现 5 次,找出只出现 1 次的数。 // 解法一 func singleNumberIIIII(nums []int) int { na, nb, nc := 0, 0, 0 for i := 0; i < len(nums); i++ { nb = nb ^ (nums[i] & na) na = (na ^ nums[i]) & ^nc nc = nc ^ (nums[i] & ^na & ^nb) } return na & ^nb & ^nc } // 解法二 func singleNumberIIIII1(nums []int) int { twos, threes, ones := 0xffffffff, 0xffffffff, 0 for i := 0; i < len(nums); i++ { threes = threes ^ (nums[i] & twos) twos = (twos ^ nums[i]) & ^ones ones = ones ^ (nums[i] & ^twos & ^threes) } return ones }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0423.Reconstruct-Original-Digits-from-English/423. Reconstruct Original Digits from English.go
leetcode/0423.Reconstruct-Original-Digits-from-English/423. Reconstruct Original Digits from English.go
package leetcode import ( "strings" ) func originalDigits(s string) string { digits := make([]int, 26) for i := 0; i < len(s); i++ { digits[int(s[i]-'a')]++ } res := make([]string, 10) res[0] = convert('z', digits, "zero", "0") res[6] = convert('x', digits, "six", "6") res[2] = convert('w', digits, "two", "2") res[4] = convert('u', digits, "four", "4") res[5] = convert('f', digits, "five", "5") res[1] = convert('o', digits, "one", "1") res[7] = convert('s', digits, "seven", "7") res[3] = convert('r', digits, "three", "3") res[8] = convert('t', digits, "eight", "8") res[9] = convert('i', digits, "nine", "9") return strings.Join(res, "") } func convert(b byte, digits []int, s string, num string) string { v := digits[int(b-'a')] for i := 0; i < len(s); i++ { digits[int(s[i]-'a')] -= v } return strings.Repeat(num, v) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0423.Reconstruct-Original-Digits-from-English/423. Reconstruct Original Digits from English_test.go
leetcode/0423.Reconstruct-Original-Digits-from-English/423. Reconstruct Original Digits from English_test.go
package leetcode import ( "fmt" "testing" ) type question423 struct { para423 ans423 } // para 是参数 // one 代表第一个参数 type para423 struct { one string } // ans 是答案 // one 代表第一个答案 type ans423 struct { one int } func Test_Problem423(t *testing.T) { qs := []question423{ { para423{"owoztneoer"}, ans423{012}, }, { para423{"fviefuro"}, ans423{45}, }, } fmt.Printf("------------------------Leetcode Problem 423------------------------\n") for _, q := range qs { _, p := q.ans423, q.para423 fmt.Printf("【input】:%v 【output】:%v\n", p, originalDigits(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/0785.Is-Graph-Bipartite/785. Is Graph Bipartite_test.go
leetcode/0785.Is-Graph-Bipartite/785. Is Graph Bipartite_test.go
package leetcode import ( "fmt" "testing" ) type question785 struct { para785 ans785 } // para 是参数 // one 代表第一个参数 type para785 struct { graph [][]int } // ans 是答案 // one 代表第一个答案 type ans785 struct { one bool } func Test_Problem785(t *testing.T) { qs := []question785{ { para785{[][]int{{1, 3}, {0, 2}, {1, 3}, {0, 2}}}, ans785{true}, }, { para785{[][]int{{1, 2, 3}, {0, 2}, {0, 1, 3}, {0, 2}}}, ans785{false}, }, { para785{[][]int{{1, 2, 3}, {0, 2, 3}, {0, 1, 3}, {0, 1, 2}}}, ans785{false}, }, } fmt.Printf("------------------------Leetcode Problem 785------------------------\n") for _, q := range qs { _, p := q.ans785, q.para785 fmt.Printf("【input】:%v 【output】:%v\n", p, isBipartite(p.graph)) } 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/0785.Is-Graph-Bipartite/785. Is Graph Bipartite.go
leetcode/0785.Is-Graph-Bipartite/785. Is Graph Bipartite.go
package leetcode // DFS 染色,1 是红色,0 是绿色,-1 是未染色 func isBipartite(graph [][]int) bool { colors := make([]int, len(graph)) for i := range colors { colors[i] = -1 } for i := range graph { if !dfs(i, graph, colors, -1) { return false } } return true } func dfs(n int, graph [][]int, colors []int, parentCol int) bool { if colors[n] == -1 { if parentCol == 1 { colors[n] = 0 } else { colors[n] = 1 } } else if colors[n] == parentCol { return false } else if colors[n] != parentCol { return true } for _, c := range graph[n] { if !dfs(c, graph, colors, colors[n]) { 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/0669.Trim-a-Binary-Search-Tree/669. Trim a Binary Search Tree.go
leetcode/0669.Trim-a-Binary-Search-Tree/669. Trim a Binary Search Tree.go
package leetcode import ( "github.com/halfrost/LeetCode-Go/structures" ) // TreeNode define type TreeNode = structures.TreeNode /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func trimBST(root *TreeNode, low int, high int) *TreeNode { if root == nil { return root } if root.Val > high { return trimBST(root.Left, low, high) } if root.Val < low { return trimBST(root.Right, low, high) } root.Left = trimBST(root.Left, low, high) root.Right = trimBST(root.Right, low, high) return root }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0669.Trim-a-Binary-Search-Tree/669. Trim a Binary Search Tree_test.go
leetcode/0669.Trim-a-Binary-Search-Tree/669. Trim a Binary Search Tree_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question669 struct { para669 ans669 } // para 是参数 // one 代表第一个参数 type para669 struct { one []int low int high int } // ans 是答案 // one 代表第一个答案 type ans669 struct { one []int } func Test_Problem669(t *testing.T) { qs := []question669{ { para669{[]int{1, 0, 2}, 1, 2}, ans669{[]int{1, structures.NULL, 2}}, }, { para669{[]int{3, 0, 4, structures.NULL, 2, structures.NULL, structures.NULL, 1}, 1, 3}, ans669{[]int{3, 2, structures.NULL, 1}}, }, { para669{[]int{1}, 1, 2}, ans669{[]int{1}}, }, { para669{[]int{1, structures.NULL, 2}, 1, 3}, ans669{[]int{1, structures.NULL, 2}}, }, { para669{[]int{1, structures.NULL, 2}, 2, 4}, ans669{[]int{2}}, }, } fmt.Printf("------------------------Leetcode Problem 669------------------------\n") for _, q := range qs { _, p := q.ans669, q.para669 fmt.Printf("【input】:%v ", p) root := structures.Ints2TreeNode(p.one) fmt.Printf("【output】:%v \n", structures.Tree2ints(trimBST(root, p.low, p.high))) } 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/0409.Longest-Palindrome/409. Longest Palindrome_test.go
leetcode/0409.Longest-Palindrome/409. Longest Palindrome_test.go
package leetcode import ( "fmt" "testing" ) type question409 struct { para409 ans409 } // para 是参数 // one 代表第一个参数 type para409 struct { one string } // ans 是答案 // one 代表第一个答案 type ans409 struct { one int } func Test_Problem409(t *testing.T) { qs := []question409{ { para409{"abccccdd"}, ans409{7}, }, } fmt.Printf("------------------------Leetcode Problem 409------------------------\n") for _, q := range qs { _, p := q.ans409, q.para409 fmt.Printf("【input】:%v 【output】:%v\n", p, longestPalindrome(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/0409.Longest-Palindrome/409. Longest Palindrome.go
leetcode/0409.Longest-Palindrome/409. Longest Palindrome.go
package leetcode func longestPalindrome(s string) int { counter := make(map[rune]int) for _, r := range s { counter[r]++ } answer := 0 for _, v := range counter { answer += v / 2 * 2 if answer%2 == 0 && v%2 == 1 { answer++ } } return answer }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0543.Diameter-of-Binary-Tree/543. Diameter of Binary Tree_test.go
leetcode/0543.Diameter-of-Binary-Tree/543. Diameter of Binary Tree_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question543 struct { para543 ans543 } // para 是参数 // one 代表第一个参数 type para543 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans543 struct { one int } func Test_Problem543(t *testing.T) { qs := []question543{ { para543{[]int{1, 2, 3, 4, 5}}, ans543{3}, }, { para543{[]int{1, 2}}, ans543{1}, }, { para543{[]int{4, -7, -3, structures.NULL, structures.NULL, -9, -3, 9, -7, -4, structures.NULL, 6, structures.NULL, -6, -6, structures.NULL, structures.NULL, 0, 6, 5, structures.NULL, 9, structures.NULL, structures.NULL, -1, -4, structures.NULL, structures.NULL, structures.NULL, -2}}, ans543{8}, }, } fmt.Printf("------------------------Leetcode Problem 543------------------------\n") for _, q := range qs { _, p := q.ans543, q.para543 fmt.Printf("【input】:%v ", p) root := structures.Ints2TreeNode(p.one) fmt.Printf("【output】:%v \n", diameterOfBinaryTree(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/0543.Diameter-of-Binary-Tree/543. Diameter of Binary Tree.go
leetcode/0543.Diameter-of-Binary-Tree/543. Diameter of Binary Tree.go
package leetcode import ( "github.com/halfrost/LeetCode-Go/structures" ) // TreeNode define type TreeNode = structures.TreeNode /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func diameterOfBinaryTree(root *TreeNode) int { result := 0 checkDiameter(root, &result) return result } func checkDiameter(root *TreeNode, result *int) int { if root == nil { return 0 } left := checkDiameter(root.Left, result) right := checkDiameter(root.Right, result) *result = max(*result, left+right) return max(left, right) + 1 } func max(a int, b int) int { if a > b { return a } return b }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0695.Max-Area-of-Island/695. Max Area of Island.go
leetcode/0695.Max-Area-of-Island/695. Max Area of Island.go
package leetcode var dir = [][]int{ {-1, 0}, {0, 1}, {1, 0}, {0, -1}, } func maxAreaOfIsland(grid [][]int) int { res := 0 for i, row := range grid { for j, col := range row { if col == 0 { continue } area := areaOfIsland(grid, i, j) if area > res { res = area } } } return res } func isInGrid(grid [][]int, x, y int) bool { return x >= 0 && x < len(grid) && y >= 0 && y < len(grid[0]) } func areaOfIsland(grid [][]int, x, y int) int { if !isInGrid(grid, x, y) || grid[x][y] == 0 { return 0 } grid[x][y] = 0 total := 1 for i := 0; i < 4; i++ { nx := x + dir[i][0] ny := y + dir[i][1] total += areaOfIsland(grid, nx, ny) } return total }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0695.Max-Area-of-Island/695. Max Area of Island_test.go
leetcode/0695.Max-Area-of-Island/695. Max Area of Island_test.go
package leetcode import ( "fmt" "testing" ) type question695 struct { para695 ans695 } // para 是参数 // one 代表第一个参数 type para695 struct { one [][]int } // ans 是答案 // one 代表第一个答案 type ans695 struct { one int } func Test_Problem695(t *testing.T) { qs := []question695{ { para695{[][]int{ {1, 1, 1, 1, 0}, {1, 1, 0, 1, 0}, {1, 1, 0, 0, 0}, {0, 0, 0, 0, 0}, }}, ans695{9}, }, { para695{[][]int{ {1, 1, 0, 0, 0}, {1, 1, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 0, 1, 1}, }}, ans695{4}, }, { para695{[][]int{ {1, 1, 1, 1, 1, 1, 1, 0}, {1, 0, 0, 0, 0, 1, 1, 0}, {1, 0, 1, 0, 1, 1, 1, 0}, {1, 0, 0, 0, 0, 1, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 0}, }}, ans695{23}, }, { para695{[][]int{ {0, 0, 1, 0, 0}, {0, 1, 0, 1, 0}, {0, 1, 1, 1, 0}, }}, ans695{5}, }, { para695{[][]int{ {1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 1, 0, 1}, {1, 0, 1, 0, 1, 0, 1}, {1, 0, 1, 1, 1, 0, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1}, }}, ans695{24}, }, } fmt.Printf("------------------------Leetcode Problem 695------------------------\n") for _, q := range qs { _, p := q.ans695, q.para695 fmt.Printf("【input】:%v 【output】:%v\n", p, maxAreaOfIsland(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/0877.Stone-Game/877. Stone Game.go
leetcode/0877.Stone-Game/877. Stone Game.go
package leetcode func stoneGame(piles []int) bool { return true }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0877.Stone-Game/877. Stone Game_test.go
leetcode/0877.Stone-Game/877. Stone Game_test.go
package leetcode import ( "fmt" "testing" ) type question877 struct { para877 ans877 } // para 是参数 // one 代表第一个参数 type para877 struct { piles []int } // ans 是答案 // one 代表第一个答案 type ans877 struct { one bool } func Test_Problem877(t *testing.T) { qs := []question877{ { para877{[]int{5, 3, 4, 5}}, ans877{true}, }, } fmt.Printf("------------------------Leetcode Problem 877------------------------\n") for _, q := range qs { _, p := q.ans877, q.para877 fmt.Printf("【input】:%v 【output】:%v\n", p, stoneGame(p.piles)) } 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/1037.Valid-Boomerang/1037. Valid Boomerang_test.go
leetcode/1037.Valid-Boomerang/1037. Valid Boomerang_test.go
package leetcode import ( "fmt" "testing" ) type question1037 struct { para1037 ans1037 } // para 是参数 // one 代表第一个参数 type para1037 struct { one [][]int } // ans 是答案 // one 代表第一个答案 type ans1037 struct { one bool } func Test_Problem1037(t *testing.T) { qs := []question1037{ { para1037{[][]int{{1, 2}, {2, 3}, {3, 2}}}, ans1037{true}, }, { para1037{[][]int{{1, 1}, {2, 2}, {3, 3}}}, ans1037{false}, }, } fmt.Printf("------------------------Leetcode Problem 1037------------------------\n") for _, q := range qs { _, p := q.ans1037, q.para1037 fmt.Printf("【input】:%v 【output】:%v\n", p, isBoomerang(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/1037.Valid-Boomerang/1037. Valid Boomerang.go
leetcode/1037.Valid-Boomerang/1037. Valid Boomerang.go
package leetcode func isBoomerang(points [][]int) bool { return (points[0][0]-points[1][0])*(points[0][1]-points[2][1]) != (points[0][0]-points[2][0])*(points[0][1]-points[1][1]) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1030.Matrix-Cells-in-Distance-Order/1030. Matrix Cells in Distance Order_test.go
leetcode/1030.Matrix-Cells-in-Distance-Order/1030. Matrix Cells in Distance Order_test.go
package leetcode import ( "fmt" "testing" ) type question1030 struct { para1030 ans1030 } // para 是参数 // one 代表第一个参数 type para1030 struct { R int C int r0 int c0 int } // ans 是答案 // one 代表第一个答案 type ans1030 struct { one [][]int } func Test_Problem1030(t *testing.T) { qs := []question1030{ { para1030{1, 2, 0, 0}, ans1030{[][]int{{0, 0}, {0, 1}}}, }, { para1030{2, 2, 0, 1}, ans1030{[][]int{{0, 1}, {0, 0}, {1, 1}, {1, 0}}}, }, { para1030{2, 3, 1, 2}, ans1030{[][]int{{1, 2}, {0, 2}, {1, 1}, {0, 1}, {1, 0}, {0, 0}}}, }, } fmt.Printf("------------------------Leetcode Problem 1030------------------------\n") for _, q := range qs { _, p := q.ans1030, q.para1030 fmt.Printf("【input】:%v 【output】:%v\n", p, allCellsDistOrder(p.R, p.C, p.r0, p.c0)) } 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/1030.Matrix-Cells-in-Distance-Order/1030. Matrix Cells in Distance Order.go
leetcode/1030.Matrix-Cells-in-Distance-Order/1030. Matrix Cells in Distance Order.go
package leetcode func allCellsDistOrder(R int, C int, r0 int, c0 int) [][]int { longRow, longCol, result := max(abs(r0-0), abs(R-r0)), max(abs(c0-0), abs(C-c0)), make([][]int, 0) maxDistance := longRow + longCol bucket := make([][][]int, maxDistance+1) for i := 0; i <= maxDistance; i++ { bucket[i] = make([][]int, 0) } for r := 0; r < R; r++ { for c := 0; c < C; c++ { distance := abs(r-r0) + abs(c-c0) tmp := []int{r, c} bucket[distance] = append(bucket[distance], tmp) } } for i := 0; i <= maxDistance; i++ { for _, buk := range bucket[i] { result = append(result, buk) } } return result } func max(a int, b int) int { if a > b { return a } return b } 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/1040.Moving-Stones-Until-Consecutive-II/1040. Moving Stones Until Consecutive II_test.go
leetcode/1040.Moving-Stones-Until-Consecutive-II/1040. Moving Stones Until Consecutive II_test.go
package leetcode import ( "fmt" "testing" ) type question1040 struct { para1040 ans1040 } // para 是参数 // one 代表第一个参数 type para1040 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans1040 struct { one []int } func Test_Problem1040(t *testing.T) { qs := []question1040{ { para1040{[]int{7, 4, 9}}, ans1040{[]int{1, 2}}, }, { para1040{[]int{6, 5, 4, 3, 10}}, ans1040{[]int{2, 3}}, }, { para1040{[]int{100, 101, 104, 102, 103}}, ans1040{[]int{0, 0}}, }, { para1040{[]int{1, 3, 5, 7, 10}}, ans1040{[]int{2, 4}}, }, } fmt.Printf("------------------------Leetcode Problem 1040------------------------\n") for _, q := range qs { _, p := q.ans1040, q.para1040 fmt.Printf("【input】:%v 【output】:%v\n", p, numMovesStonesII(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/1040.Moving-Stones-Until-Consecutive-II/1040. Moving Stones Until Consecutive II.go
leetcode/1040.Moving-Stones-Until-Consecutive-II/1040. Moving Stones Until Consecutive II.go
package leetcode import ( "math" "sort" ) func numMovesStonesII(stones []int) []int { if len(stones) == 0 { return []int{0, 0} } sort.Ints(stones) n := len(stones) maxStep, minStep, left, right := max(stones[n-1]-stones[1]-n+2, stones[n-2]-stones[0]-n+2), math.MaxInt64, 0, 0 for left < n { if right+1 < n && stones[right]-stones[left] < n { right++ } else { if stones[right]-stones[left] >= n { right-- } if right-left+1 == n-1 && stones[right]-stones[left]+1 == n-1 { minStep = min(minStep, 2) } else { minStep = min(minStep, n-(right-left+1)) } if right == n-1 && stones[right]-stones[left] < n { break } left++ } } return []int{minStep, maxStep} } 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 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0541.Reverse-String-II/541. Reverse String II.go
leetcode/0541.Reverse-String-II/541. Reverse String II.go
package leetcode func reverseStr(s string, k int) string { if k > len(s) { k = len(s) } for i := 0; i < len(s); i = i + 2*k { if len(s)-i >= k { ss := revers(s[i : i+k]) s = s[:i] + ss + s[i+k:] } else { ss := revers(s[i:]) s = s[:i] + ss } } return s } func revers(s string) string { bytes := []byte(s) i, j := 0, len(bytes)-1 for i < j { bytes[i], bytes[j] = bytes[j], bytes[i] i++ j-- } return string(bytes) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0541.Reverse-String-II/541. Reverse String II_test.go
leetcode/0541.Reverse-String-II/541. Reverse String II_test.go
package leetcode import ( "fmt" "testing" ) type question541 struct { para541 ans541 } // para 是参数 // one 代表第一个参数 type para541 struct { s string k int } // ans 是答案 // one 代表第一个答案 type ans541 struct { one string } func Test_Problem541(t *testing.T) { qs := []question541{ { para541{"abcdefg", 2}, ans541{"bacdfeg"}, }, { para541{"abcdefg", 5}, ans541{"edcbafg"}, }, { para541{"abcd", 4}, ans541{"dcba"}, }, { para541{"", 100}, ans541{""}, }, } fmt.Printf("------------------------Leetcode Problem 541------------------------\n") for _, q := range qs { _, p := q.ans541, q.para541 fmt.Printf("【input】:%v 【output】:%v\n", p, reverseStr(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/0131.Palindrome-Partitioning/131. Palindrome Partitioning.go
leetcode/0131.Palindrome-Partitioning/131. Palindrome Partitioning.go
package leetcode // 解法一 func partition131(s string) [][]string { if s == "" { return [][]string{} } res, pal := [][]string{}, []string{} findPalindrome(s, 0, "", true, pal, &res) return res } func findPalindrome(str string, index int, s string, isPal bool, pal []string, res *[][]string) { if index == len(str) { if isPal { tmp := make([]string, len(pal)) copy(tmp, pal) *res = append(*res, tmp) } return } if index == 0 { s = string(str[index]) pal = append(pal, s) findPalindrome(str, index+1, s, isPal && isPalindrome131(s), pal, res) } else { temp := pal[len(pal)-1] s = pal[len(pal)-1] + string(str[index]) pal[len(pal)-1] = s findPalindrome(str, index+1, s, isPalindrome131(s), pal, res) pal[len(pal)-1] = temp if isPalindrome131(temp) { pal = append(pal, string(str[index])) findPalindrome(str, index+1, temp, isPal && isPalindrome131(temp), pal, res) pal = pal[:len(pal)-1] } } return } func isPalindrome131(s string) bool { slen := len(s) for i, j := 0, slen-1; i < j; i, j = i+1, j-1 { if s[i] != s[j] { return false } } return true } // 解法二 func partition131_1(s string) [][]string { result := [][]string{} size := len(s) if size == 0 { return result } current := make([]string, 0, size) dfs131(s, 0, current, &result) return result } func dfs131(s string, idx int, cur []string, result *[][]string) { start, end := idx, len(s) if start == end { temp := make([]string, len(cur)) copy(temp, cur) *result = append(*result, temp) return } for i := start; i < end; i++ { if isPal(s, start, i) { dfs131(s, i+1, append(cur, s[start:i+1]), result) } } } func isPal(str string, s, e int) bool { for s < e { if str[s] != str[e] { return false } s++ e-- } return true }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0131.Palindrome-Partitioning/131. Palindrome Partitioning_test.go
leetcode/0131.Palindrome-Partitioning/131. Palindrome Partitioning_test.go
package leetcode import ( "fmt" "testing" ) type question131 struct { para131 ans131 } // para 是参数 // one 代表第一个参数 type para131 struct { s string } // ans 是答案 // one 代表第一个答案 type ans131 struct { one [][]string } func Test_Problem131(t *testing.T) { qs := []question131{ { para131{"aab"}, ans131{[][]string{{"aa", "b"}, {"a", "a", "b"}}}, }, { para131{"bb"}, ans131{[][]string{{"b", "b"}, {"bb"}}}, }, { para131{"efe"}, ans131{[][]string{{"e", "f", "e"}, {"efe"}}}, }, { para131{"abbab"}, ans131{[][]string{{"a", "b", "b", "a", "b"}, {"a", "b", "bab"}, {"a", "bb", "a", "b"}, {"abba", "b"}}}, }, } fmt.Printf("------------------------Leetcode Problem 131------------------------\n") for _, q := range qs { _, p := q.ans131, q.para131 fmt.Printf("【input】:%v 【output】:%v\n", p, partition131(p.s)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1310.XOR-Queries-of-a-Subarray/1310. XOR Queries of a Subarray_test.go
leetcode/1310.XOR-Queries-of-a-Subarray/1310. XOR Queries of a Subarray_test.go
package leetcode import ( "fmt" "testing" ) type question1310 struct { para1310 ans1310 } // para 是参数 // one 代表第一个参数 type para1310 struct { arr []int queries [][]int } // ans 是答案 // one 代表第一个答案 type ans1310 struct { one []int } func Test_Problem1310(t *testing.T) { qs := []question1310{ { para1310{[]int{1, 3, 4, 8}, [][]int{{0, 1}, {1, 2}, {0, 3}, {3, 3}}}, ans1310{[]int{2, 7, 14, 8}}, }, { para1310{[]int{4, 8, 2, 10}, [][]int{{2, 3}, {1, 3}, {0, 0}, {0, 3}}}, ans1310{[]int{8, 0, 4, 4}}, }, } fmt.Printf("------------------------Leetcode Problem 1310------------------------\n") for _, q := range qs { _, p := q.ans1310, q.para1310 fmt.Printf("【input】:%v 【output】:%v\n", p, xorQueries(p.arr, p.queries)) } 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/1310.XOR-Queries-of-a-Subarray/1310. XOR Queries of a Subarray.go
leetcode/1310.XOR-Queries-of-a-Subarray/1310. XOR Queries of a Subarray.go
package leetcode func xorQueries(arr []int, queries [][]int) []int { xors := make([]int, len(arr)) xors[0] = arr[0] for i := 1; i < len(arr); i++ { xors[i] = arr[i] ^ xors[i-1] } res := make([]int, len(queries)) for i, q := range queries { res[i] = xors[q[1]] if q[0] > 0 { res[i] ^= xors[q[0]-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/0581.Shortest-Unsorted-Continuous-Subarray/581. Shortest Unsorted Continuous Subarray.go
leetcode/0581.Shortest-Unsorted-Continuous-Subarray/581. Shortest Unsorted Continuous Subarray.go
package leetcode import "math" func findUnsortedSubarray(nums []int) int { n, left, right, minR, maxL, isSort := len(nums), -1, -1, math.MaxInt32, math.MinInt32, false // left for i := 1; i < n; i++ { if nums[i] < nums[i-1] { isSort = true } if isSort { minR = min(minR, nums[i]) } } isSort = false // right for i := n - 2; i >= 0; i-- { if nums[i] > nums[i+1] { isSort = true } if isSort { maxL = max(maxL, nums[i]) } } // minR for i := 0; i < n; i++ { if nums[i] > minR { left = i break } } // maxL for i := n - 1; i >= 0; i-- { if nums[i] < maxL { right = i break } } if left == -1 || right == -1 { return 0 } return right - left + 1 } func max(a, b int) int { if a > b { return a } return b } 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/0581.Shortest-Unsorted-Continuous-Subarray/581. Shortest Unsorted Continuous Subarray_test.go
leetcode/0581.Shortest-Unsorted-Continuous-Subarray/581. Shortest Unsorted Continuous Subarray_test.go
package leetcode import ( "fmt" "testing" ) type question581 struct { para581 ans581 } // para 是参数 // one 代表第一个参数 type para581 struct { nums []int } // ans 是答案 // one 代表第一个答案 type ans581 struct { one int } func Test_Problem581(t *testing.T) { qs := []question581{ { para581{[]int{2, 6, 4, 8, 10, 9, 15}}, ans581{5}, }, { para581{[]int{1, 2, 3, 4}}, ans581{0}, }, { para581{[]int{1}}, ans581{0}, }, } fmt.Printf("------------------------Leetcode Problem 581------------------------\n") for _, q := range qs { _, p := q.ans581, q.para581 fmt.Printf("【input】:%v 【output】:%v\n", p, findUnsortedSubarray(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/0395.Longest-Substring-with-At-Least-K-Repeating-Characters/395. Longest Substring with At Least K Repeating Characters.go
leetcode/0395.Longest-Substring-with-At-Least-K-Repeating-Characters/395. Longest Substring with At Least K Repeating Characters.go
package leetcode import "strings" // 解法一 滑动窗口 func longestSubstring(s string, k int) int { res := 0 for t := 1; t <= 26; t++ { freq, total, lessK, left, right := [26]int{}, 0, 0, 0, -1 for left < len(s) { if right+1 < len(s) && total <= t { if freq[s[right+1]-'a'] == 0 { total++ lessK++ } freq[s[right+1]-'a']++ if freq[s[right+1]-'a'] == k { lessK-- } right++ } else { if freq[s[left]-'a'] == k { lessK++ } freq[s[left]-'a']-- if freq[s[left]-'a'] == 0 { total-- lessK-- } left++ } if lessK == 0 { res = max(res, right-left+1) } } } return res } func max(a, b int) int { if a > b { return a } return b } // 解法二 递归分治 func longestSubstring1(s string, k int) int { if s == "" { return 0 } freq, split, res := [26]int{}, byte(0), 0 for _, ch := range s { freq[ch-'a']++ } for i, c := range freq[:] { if 0 < c && c < k { split = 'a' + byte(i) break } } if split == 0 { return len(s) } for _, subStr := range strings.Split(s, string(split)) { res = max(res, longestSubstring1(subStr, 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/0395.Longest-Substring-with-At-Least-K-Repeating-Characters/395. Longest Substring with At Least K Repeating Characters_test.go
leetcode/0395.Longest-Substring-with-At-Least-K-Repeating-Characters/395. Longest Substring with At Least K Repeating Characters_test.go
package leetcode import ( "fmt" "testing" ) type question395 struct { para395 ans395 } // para 是参数 // one 代表第一个参数 type para395 struct { s string k int } // ans 是答案 // one 代表第一个答案 type ans395 struct { one int } func Test_Problem395(t *testing.T) { qs := []question395{ { para395{"aaabb", 3}, ans395{3}, }, { para395{"ababbc", 2}, ans395{5}, }, } fmt.Printf("------------------------Leetcode Problem 395------------------------\n") for _, q := range qs { _, p := q.ans395, q.para395 fmt.Printf("【input】:%v 【output】:%v\n", p, longestSubstring(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/0611.Valid-Triangle-Number/611. Valid Triangle Number_test.go
leetcode/0611.Valid-Triangle-Number/611. Valid Triangle Number_test.go
package leetcode import ( "fmt" "testing" ) type question611 struct { para611 ans611 } // para 是参数 // one 代表第一个参数 type para611 struct { nums []int } // ans 是答案 // one 代表第一个答案 type ans611 struct { one int } func Test_Problem611(t *testing.T) { qs := []question611{ { para611{[]int{2, 2, 3, 4}}, ans611{3}, }, { para611{[]int{4, 2, 3, 4}}, ans611{4}, }, { para611{[]int{0, 0}}, ans611{0}, }, } fmt.Printf("------------------------Leetcode Problem 611------------------------\n") for _, q := range qs { _, p := q.ans611, q.para611 fmt.Printf("【input】:%v 【output】:%v\n", p, triangleNumber(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/0611.Valid-Triangle-Number/611. Valid Triangle Number.go
leetcode/0611.Valid-Triangle-Number/611. Valid Triangle Number.go
package leetcode import "sort" func triangleNumber(nums []int) int { res := 0 sort.Ints(nums) for i := 0; i < len(nums)-2; i++ { k := i + 2 for j := i + 1; j < len(nums)-1 && nums[i] != 0; j++ { for k < len(nums) && nums[i]+nums[j] > nums[k] { k++ } res += k - j - 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/0682.Baseball-Game/682. Baseball Game.go
leetcode/0682.Baseball-Game/682. Baseball Game.go
package leetcode import "strconv" func calPoints(ops []string) int { stack := make([]int, len(ops)) top := 0 for i := 0; i < len(ops); i++ { op := ops[i] switch op { case "+": last1 := stack[top-1] last2 := stack[top-2] stack[top] = last1 + last2 top++ case "D": last1 := stack[top-1] stack[top] = last1 * 2 top++ case "C": top-- default: stack[top], _ = strconv.Atoi(op) top++ } } points := 0 for i := 0; i < top; i++ { points += stack[i] } return points }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0682.Baseball-Game/682. Baseball Game_test.go
leetcode/0682.Baseball-Game/682. Baseball Game_test.go
package leetcode import ( "fmt" "testing" ) type question682 struct { para682 ans682 } // para 是参数 // one 代表第一个参数 type para682 struct { one []string } // ans 是答案 // one 代表第一个答案 type ans682 struct { one int } func Test_Problem682(t *testing.T) { qs := []question682{ { para682{[]string{"5", "2", "C", "D", "+"}}, ans682{30}, }, { para682{[]string{"5", "-2", "4", "C", "D", "9", "+", "+"}}, ans682{27}, }, } fmt.Printf("------------------------Leetcode Problem 682------------------------\n") for _, q := range qs { _, p := q.ans682, q.para682 fmt.Printf("【input】:%v 【output】:%v\n", p, calPoints(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/0458.Poor-Pigs/458.Poor Pigs_test.go
leetcode/0458.Poor-Pigs/458.Poor Pigs_test.go
package leetcode import ( "fmt" "testing" ) type question458 struct { para458 ans458 } // para 是参数 type para458 struct { buckets int minutesToDie int minutesToTest int } // ans 是答案 type ans458 struct { ans int } func Test_Problem458(t *testing.T) { qs := []question458{ { para458{1000, 15, 60}, ans458{5}, }, { para458{4, 15, 15}, ans458{2}, }, { para458{4, 15, 30}, ans458{2}, }, } fmt.Printf("------------------------Leetcode Problem 458------------------------\n") for _, q := range qs { _, p := q.ans458, q.para458 fmt.Printf("【input】:%v 【output】:%v\n", p, poorPigs(p.buckets, p.minutesToDie, p.minutesToTest)) } 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/0458.Poor-Pigs/458.Poor Pigs.go
leetcode/0458.Poor-Pigs/458.Poor Pigs.go
package leetcode import "math" func poorPigs(buckets int, minutesToDie int, minutesToTest int) int { base := minutesToTest/minutesToDie + 1 return int(math.Ceil(math.Log10(float64(buckets)) / math.Log10(float64(base)))) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0448.Find-All-Numbers-Disappeared-in-an-Array/448. Find All Numbers Disappeared in an Array.go
leetcode/0448.Find-All-Numbers-Disappeared-in-an-Array/448. Find All Numbers Disappeared in an Array.go
package leetcode func findDisappearedNumbers(nums []int) []int { res := []int{} for _, v := range nums { if v < 0 { v = -v } if nums[v-1] > 0 { nums[v-1] = -nums[v-1] } } for i, v := range nums { if v > 0 { res = append(res, i+1) } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0448.Find-All-Numbers-Disappeared-in-an-Array/448. Find All Numbers Disappeared in an Array_test.go
leetcode/0448.Find-All-Numbers-Disappeared-in-an-Array/448. Find All Numbers Disappeared in an Array_test.go
package leetcode import ( "fmt" "testing" ) type question448 struct { para448 ans448 } // para 是参数 // one 代表第一个参数 type para448 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans448 struct { one []int } func Test_Problem448(t *testing.T) { qs := []question448{ { para448{[]int{4, 3, 2, 7, 8, 2, 3, 1}}, ans448{[]int{5, 6}}, }, { para448{[]int{4, 3, 2, 10, 9, 2, 3, 1, 1, 1, 1}}, ans448{[]int{5, 6, 7, 8, 11}}, }, // 如需多个测试,可以复制上方元素。 } fmt.Printf("------------------------Leetcode Problem 448------------------------\n") for _, q := range qs { _, p := q.ans448, q.para448 fmt.Printf("【input】:%v 【output】:%v\n", p, findDisappearedNumbers(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/2182.Construct-String-With-Repeat-Limit/2182. Construct String With Repeat Limit.go
leetcode/2182.Construct-String-With-Repeat-Limit/2182. Construct String With Repeat Limit.go
package leetcode func repeatLimitedString(s string, repeatLimit int) string { cnt := make([]int, 26) for _, c := range s { cnt[int(c-'a')]++ } var ns []byte for i := 25; i >= 0; { k := i - 1 for cnt[i] > 0 { for j := 0; j < min(cnt[i], repeatLimit); j++ { ns = append(ns, byte(i)+'a') } cnt[i] -= repeatLimit if cnt[i] > 0 { for ; k >= 0 && cnt[k] == 0; k-- { } if k < 0 { break } else { ns = append(ns, byte(k)+'a') cnt[k]-- } } } i = k } return string(ns) } func min(a, b int) int { if a < b { return a } else { return b } }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/2182.Construct-String-With-Repeat-Limit/2182. Construct String With Repeat Limit_test.go
leetcode/2182.Construct-String-With-Repeat-Limit/2182. Construct String With Repeat Limit_test.go
package leetcode import ( "fmt" "testing" ) type question2182 struct { para2182 ans2182 } // para 是参数 // one 代表第一个参数 type para2182 struct { one string limit int } // ans 是答案 // one 代表第一个答案 type ans2182 struct { one string } func Test_Problem2182(t *testing.T) { qs := []question2182{ { para2182{"cczazcc", 3}, ans2182{"zzcccac"}, }, { para2182{"aababab", 2}, ans2182{"bbabaa"}, }, } fmt.Printf("------------------------Leetcode Problem 2182------------------------\n") for _, q := range qs { _, p := q.ans2182, q.para2182 fmt.Printf("【input】:%v 【output】:%v\n", p, repeatLimitedString(p.one, p.limit)) } 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/0092.Reverse-Linked-List-II/92. Reverse Linked List II.go
leetcode/0092.Reverse-Linked-List-II/92. Reverse Linked List II.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 reverseBetween(head *ListNode, m int, n int) *ListNode { if head == nil || m >= n { return head } newHead := &ListNode{Val: 0, Next: head} pre := newHead for count := 0; pre.Next != nil && count < m-1; count++ { pre = pre.Next } if pre.Next == nil { return head } cur := pre.Next for i := 0; i < n-m; i++ { tmp := pre.Next pre.Next = cur.Next cur.Next = cur.Next.Next pre.Next.Next = tmp } return newHead.Next }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0092.Reverse-Linked-List-II/92. Reverse Linked List II_test.go
leetcode/0092.Reverse-Linked-List-II/92. Reverse Linked List II_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question92 struct { para92 ans92 } // para 是参数 // one 代表第一个参数 type para92 struct { one []int m, n int } // ans 是答案 // one 代表第一个答案 type ans92 struct { one []int } func Test_Problem92(t *testing.T) { qs := []question92{ { para92{[]int{1, 2, 3, 4, 5}, 2, 4}, ans92{[]int{1, 4, 3, 2, 5}}, }, { para92{[]int{1, 2, 3, 4, 5}, 2, 2}, ans92{[]int{1, 2, 3, 4, 5}}, }, { para92{[]int{1, 2, 3, 4, 5}, 1, 5}, ans92{[]int{5, 4, 3, 2, 1}}, }, { para92{[]int{1, 2, 3, 4, 5, 6}, 3, 4}, ans92{[]int{1, 2, 4, 3, 5, 6}}, }, { para92{[]int{3, 5}, 1, 2}, ans92{[]int{5, 3}}, }, { para92{[]int{3}, 3, 5}, ans92{[]int{3}}, }, } fmt.Printf("------------------------Leetcode Problem 92------------------------\n") for _, q := range qs { _, p := q.ans92, q.para92 fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(reverseBetween(structures.Ints2List(p.one), p.m, p.n))) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0041.First-Missing-Positive/41. First Missing Positive_test.go
leetcode/0041.First-Missing-Positive/41. First Missing Positive_test.go
package leetcode import ( "fmt" "testing" ) type question41 struct { para41 ans41 } // para 是参数 // one 代表第一个参数 type para41 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans41 struct { one int } func Test_Problem41(t *testing.T) { qs := []question41{ { para41{[]int{10, -1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, -3}}, ans41{6}, }, { para41{[]int{10, -1, 8, 6, 7, 3, -2, 5, 4, 2, 1, -3}}, ans41{9}, }, { para41{[]int{1}}, ans41{2}, }, { para41{[]int{0, 2, 2, 1, 1}}, ans41{3}, }, { para41{[]int{}}, ans41{1}, }, { para41{[]int{1, 2, 0}}, ans41{3}, }, { para41{[]int{3, 4, -1, 1}}, ans41{2}, }, } fmt.Printf("------------------------Leetcode Problem 41------------------------\n") for _, q := range qs { _, p := q.ans41, q.para41 fmt.Printf("【input】:%v 【output】:%v\n", p, firstMissingPositive(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/0041.First-Missing-Positive/41. First Missing Positive.go
leetcode/0041.First-Missing-Positive/41. First Missing Positive.go
package leetcode func firstMissingPositive(nums []int) int { numMap := make(map[int]int, len(nums)) for _, v := range nums { numMap[v] = v } for index := 1; index < len(nums)+1; index++ { if _, ok := numMap[index]; !ok { return index } } return len(nums) + 1 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0648.Replace-Words/648. Replace Words_test.go
leetcode/0648.Replace-Words/648. Replace Words_test.go
package leetcode import ( "fmt" "testing" ) type question648 struct { para648 ans648 } // para 是参数 // one 代表第一个参数 type para648 struct { one []string s string } // ans 是答案 // one 代表第一个答案 type ans648 struct { one string } func Test_Problem648(t *testing.T) { qs := []question648{ { para648{[]string{"cat", "bat", "rat"}, "the cattle was rattled by the battery"}, ans648{"the cat was rat by the bat"}, }, } fmt.Printf("------------------------Leetcode Problem 648------------------------\n") for _, q := range qs { _, p := q.ans648, q.para648 fmt.Printf("【input】:%v 【output】:%v\n", p, replaceWords(p.one, p.s)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0648.Replace-Words/648. Replace Words.go
leetcode/0648.Replace-Words/648. Replace Words.go
package leetcode import "strings" // 解法一 哈希表 func replaceWords(dict []string, sentence string) string { roots := make(map[byte][]string) for _, root := range dict { b := root[0] roots[b] = append(roots[b], root) } words := strings.Split(sentence, " ") for i, word := range words { b := []byte(word) for j := 1; j < len(b) && j <= 100; j++ { if findWord(roots, b[0:j]) { words[i] = string(b[0:j]) break } } } return strings.Join(words, " ") } func findWord(roots map[byte][]string, word []byte) bool { if roots[word[0]] == nil { return false } for _, root := range roots[word[0]] { if root == string(word) { return true } } return false } // 解法二 Trie func replaceWords1(dict []string, sentence string) string { trie := Constructor208() for _, v := range dict { trie.Insert(v) } words := strings.Split(sentence, " ") var result []string word := "" i := 0 for _, value := range words { word = "" for i = 1; i < len(value); i++ { if trie.Search(value[:i]) { word = value[:i] break } } if len(word) == 0 { result = append(result, value) } else { result = append(result, word) } } return strings.Join(result, " ") } type Trie struct { isWord bool children map[rune]*Trie } /** Initialize your data structure here. */ func Constructor208() Trie { return Trie{isWord: false, children: make(map[rune]*Trie)} } /** Inserts a word into the trie. */ func (this *Trie) Insert(word string) { parent := this for _, ch := range word { if child, ok := parent.children[ch]; ok { parent = child } else { newChild := &Trie{children: make(map[rune]*Trie)} parent.children[ch] = newChild parent = newChild } } parent.isWord = true } /** Returns if the word is in the trie. */ func (this *Trie) Search(word string) bool { parent := this for _, ch := range word { if child, ok := parent.children[ch]; ok { parent = child continue } return false } return parent.isWord }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0876.Middle-of-the-Linked-List/876. Middle of the Linked List_test.go
leetcode/0876.Middle-of-the-Linked-List/876. Middle of the Linked List_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question876 struct { para876 ans876 } // para 是参数 // one 代表第一个参数 type para876 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans876 struct { one int } func Test_Problem876(t *testing.T) { qs := []question876{ { para876{[]int{1, 2, 3, 4, 5}}, ans876{3}, }, { para876{[]int{1, 2, 3, 4}}, ans876{3}, }, { para876{[]int{1}}, ans876{1}, }, { para876{[]int{}}, ans876{}, }, } fmt.Printf("------------------------Leetcode Problem 876------------------------\n") for _, q := range qs { _, p := q.ans876, q.para876 fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(middleNode(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/0876.Middle-of-the-Linked-List/876. Middle of the Linked List.go
leetcode/0876.Middle-of-the-Linked-List/876. Middle of the 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 middleNode(head *ListNode) *ListNode { if head == nil || head.Next == nil { return head } p1 := head p2 := head for p2.Next != nil && p2.Next.Next != nil { p1 = p1.Next p2 = p2.Next.Next } length := 0 cur := head for cur != nil { length++ cur = cur.Next } if length%2 == 0 { return p1.Next } return p1 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0046.Permutations/46. Permutations_test.go
leetcode/0046.Permutations/46. Permutations_test.go
package leetcode import ( "fmt" "testing" ) type question46 struct { para46 ans46 } // para 是参数 // one 代表第一个参数 type para46 struct { s []int } // ans 是答案 // one 代表第一个答案 type ans46 struct { one [][]int } func Test_Problem46(t *testing.T) { qs := []question46{ { para46{[]int{1, 2, 3}}, ans46{[][]int{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}}, }, } fmt.Printf("------------------------Leetcode Problem 46------------------------\n") for _, q := range qs { _, p := q.ans46, q.para46 fmt.Printf("【input】:%v 【output】:%v\n", p, permute(p.s)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0046.Permutations/46. Permutations.go
leetcode/0046.Permutations/46. Permutations.go
package leetcode func permute(nums []int) [][]int { if len(nums) == 0 { return [][]int{} } used, p, res := make([]bool, len(nums)), []int{}, [][]int{} generatePermutation(nums, 0, p, &res, &used) return res } func generatePermutation(nums []int, index int, p []int, res *[][]int, used *[]bool) { if index == len(nums) { temp := make([]int, len(p)) copy(temp, p) *res = append(*res, temp) return } for i := 0; i < len(nums); i++ { if !(*used)[i] { (*used)[i] = true p = append(p, nums[i]) generatePermutation(nums, index+1, p, res, used) p = p[:len(p)-1] (*used)[i] = false } } return }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1232.Check-If-It-Is-a-Straight-Line/1232. Check If It Is a Straight Line.go
leetcode/1232.Check-If-It-Is-a-Straight-Line/1232. Check If It Is a Straight Line.go
package leetcode func checkStraightLine(coordinates [][]int) bool { dx0 := coordinates[1][0] - coordinates[0][0] dy0 := coordinates[1][1] - coordinates[0][1] for i := 1; i < len(coordinates)-1; i++ { dx := coordinates[i+1][0] - coordinates[i][0] dy := coordinates[i+1][1] - coordinates[i][1] if dy*dx0 != dy0*dx { // check cross product 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/1232.Check-If-It-Is-a-Straight-Line/1232. Check If It Is a Straight Line_test.go
leetcode/1232.Check-If-It-Is-a-Straight-Line/1232. Check If It Is a Straight Line_test.go
package leetcode import ( "fmt" "testing" ) type question1232 struct { para1232 ans1232 } // para 是参数 // one 代表第一个参数 type para1232 struct { arr [][]int } // ans 是答案 // one 代表第一个答案 type ans1232 struct { one bool } func Test_Problem1232(t *testing.T) { qs := []question1232{ { para1232{[][]int{{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}}}, ans1232{true}, }, { para1232{[][]int{{1, 1}, {2, 2}, {3, 4}, {4, 5}, {5, 6}, {7, 7}}}, ans1232{false}, }, } fmt.Printf("------------------------Leetcode Problem 1232------------------------\n") for _, q := range qs { _, p := q.ans1232, q.para1232 fmt.Printf("【input】:%v 【output】:%v\n", p, checkStraightLine(p.arr)) } 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/0315.Count-of-Smaller-Numbers-After-Self/315. Count of Smaller Numbers After Self.go
leetcode/0315.Count-of-Smaller-Numbers-After-Self/315. Count of Smaller Numbers After Self.go
package leetcode import ( "sort" "github.com/halfrost/LeetCode-Go/template" ) // 解法一 线段树 func countSmaller(nums []int) []int { if len(nums) == 0 { return []int{} } st, minNum, numsMap, numsArray, res := template.SegmentCountTree{}, 0, make(map[int]int, 0), []int{}, make([]int, len(nums)) for i := 0; i < len(nums); i++ { numsMap[nums[i]] = nums[i] } for _, v := range numsMap { numsArray = append(numsArray, v) } // 排序是为了使得线段树中的区间 left <= right,如果此处不排序,线段树中的区间有很多不合法。 sort.Ints(numsArray) minNum = numsArray[0] // 初始化线段树,节点内的值都赋值为 0,即计数为 0 st.Init(numsArray, func(i, j int) int { return 0 }) for i := len(nums) - 1; i >= 0; i-- { if nums[i] == minNum { res[i] = 0 st.UpdateCount(nums[i]) continue } st.UpdateCount(nums[i]) res[i] = st.Query(minNum, nums[i]-1) } return res } // 解法二 树状数组 func countSmaller1(nums []int) []int { // copy 一份原数组至所有数字 allNums 数组中 allNums, res := make([]int, len(nums)), []int{} copy(allNums, nums) // 将 allNums 离散化 sort.Ints(allNums) k := 1 kth := map[int]int{allNums[0]: k} for i := 1; i < len(allNums); i++ { if allNums[i] != allNums[i-1] { k++ kth[allNums[i]] = k } } // 树状数组 Query bit := template.BinaryIndexedTree{} bit.Init(k) for i := len(nums) - 1; i >= 0; i-- { res = append(res, bit.Query(kth[nums[i]]-1)) bit.Add(kth[nums[i]], 1) } for i := 0; i < len(res)/2; i++ { res[i], res[len(res)-1-i] = res[len(res)-1-i], res[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/0315.Count-of-Smaller-Numbers-After-Self/315. Count of Smaller Numbers After Self_test.go
leetcode/0315.Count-of-Smaller-Numbers-After-Self/315. Count of Smaller Numbers After Self_test.go
package leetcode import ( "fmt" "testing" ) type question315 struct { para315 ans315 } // para 是参数 // one 代表第一个参数 type para315 struct { nums []int } // ans 是答案 // one 代表第一个答案 type ans315 struct { one []int } func Test_Problem315(t *testing.T) { qs := []question315{ { para315{[]int{5, 2, 6, 1}}, ans315{[]int{2, 1, 1, 0}}, }, { para315{[]int{-1, -1}}, ans315{[]int{0, 0}}, }, { para315{[]int{26, 78, 27, 100, 33, 67, 90, 23, 66, 5, 38, 7, 35, 23, 52, 22, 83, 51, 98, 69, 81, 32, 78, 28, 94, 13, 2, 97, 3, 76, 99, 51, 9, 21, 84, 66, 65, 36, 100, 41}}, ans315{[]int{10, 27, 10, 35, 12, 22, 28, 8, 19, 2, 12, 2, 9, 6, 12, 5, 17, 9, 19, 12, 14, 6, 12, 5, 12, 3, 0, 10, 0, 7, 8, 4, 0, 0, 4, 3, 2, 0, 1, 0}}, }, } fmt.Printf("------------------------Leetcode Problem 315------------------------\n") for _, q := range qs { _, p := q.ans315, q.para315 fmt.Printf("【input】:%v 【output】:%v\n", p, countSmaller(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/0417.Pacific-Atlantic-Water-Flow/417. Pacific Atlantic Water Flow.go
leetcode/0417.Pacific-Atlantic-Water-Flow/417. Pacific Atlantic Water Flow.go
package leetcode import "math" func pacificAtlantic(matrix [][]int) [][]int { if len(matrix) == 0 || len(matrix[0]) == 0 { return nil } row, col, res := len(matrix), len(matrix[0]), make([][]int, 0) pacific, atlantic := make([][]bool, row), make([][]bool, row) for i := 0; i < row; i++ { pacific[i] = make([]bool, col) atlantic[i] = make([]bool, col) } for i := 0; i < row; i++ { dfs(matrix, i, 0, &pacific, math.MinInt32) dfs(matrix, i, col-1, &atlantic, math.MinInt32) } for j := 0; j < col; j++ { dfs(matrix, 0, j, &pacific, math.MinInt32) dfs(matrix, row-1, j, &atlantic, math.MinInt32) } for i := 0; i < row; i++ { for j := 0; j < col; j++ { if atlantic[i][j] && pacific[i][j] { res = append(res, []int{i, j}) } } } return res } func dfs(matrix [][]int, row, col int, visited *[][]bool, height int) { if row < 0 || row >= len(matrix) || col < 0 || col >= len(matrix[0]) { return } if (*visited)[row][col] || matrix[row][col] < height { return } (*visited)[row][col] = true dfs(matrix, row+1, col, visited, matrix[row][col]) dfs(matrix, row-1, col, visited, matrix[row][col]) dfs(matrix, row, col+1, visited, matrix[row][col]) dfs(matrix, row, col-1, visited, matrix[row][col]) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0417.Pacific-Atlantic-Water-Flow/417. Pacific Atlantic Water Flow_test.go
leetcode/0417.Pacific-Atlantic-Water-Flow/417. Pacific Atlantic Water Flow_test.go
package leetcode import ( "fmt" "testing" ) type question417 struct { para417 ans417 } // para 是参数 // one 代表第一个参数 type para417 struct { matrix [][]int } // ans 是答案 // one 代表第一个答案 type ans417 struct { one [][]int } func Test_Problem417(t *testing.T) { qs := []question417{ { para417{[][]int{ {1, 2, 2, 3, 5}, {3, 2, 3, 4, 4}, {2, 4, 5, 3, 1}, {6, 7, 1, 4, 5}, {5, 1, 1, 2, 4}, }}, ans417{[][]int{ {0, 4}, {1, 3}, {1, 4}, {2, 2}, {3, 0}, {3, 1}, {4, 0}, }}, }, } fmt.Printf("------------------------Leetcode Problem 417------------------------\n") for _, q := range qs { _, p := q.ans417, q.para417 fmt.Printf("【input】:%v 【output】:%v\n", p, pacificAtlantic(p.matrix)) } 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/0930.Binary-Subarrays-With-Sum/930. Binary Subarrays With Sum_test.go
leetcode/0930.Binary-Subarrays-With-Sum/930. Binary Subarrays With Sum_test.go
package leetcode import ( "fmt" "testing" ) type question930 struct { para930 ans930 } // para 是参数 // one 代表第一个参数 type para930 struct { s []int k int } // ans 是答案 // one 代表第一个答案 type ans930 struct { one int } func Test_Problem930(t *testing.T) { qs := []question930{ { para930{[]int{1, 0, 1, 0, 1}, 2}, ans930{4}, }, { para930{[]int{0, 0, 0, 0, 0}, 0}, ans930{15}, }, { para930{[]int{1, 0, 1, 1, 1, 1, 0, 1, 0, 1}, 2}, ans930{4}, }, } fmt.Printf("------------------------Leetcode Problem 930------------------------\n") for _, q := range qs { _, p := q.ans930, q.para930 fmt.Printf("【input】:%v 【output】:%v\n", p, numSubarraysWithSum(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/0930.Binary-Subarrays-With-Sum/930. Binary Subarrays With Sum.go
leetcode/0930.Binary-Subarrays-With-Sum/930. Binary Subarrays With Sum.go
package leetcode import "fmt" func numSubarraysWithSum(A []int, S int) int { freq, sum, res := make([]int, len(A)+1), 0, 0 freq[0] = 1 for _, v := range A { t := sum + v - S if t >= 0 { // 总和有多余的,需要减去 t,除去的方法有 freq[t] 种 res += freq[t] } sum += v freq[sum]++ fmt.Printf("freq = %v sum = %v res = %v t = %v\n", freq, sum, res, t) } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442. Count Triplets That Can Form Two Arrays of Equal XOR.go
leetcode/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442. Count Triplets That Can Form Two Arrays of Equal XOR.go
package leetcode func countTriplets(arr []int) int { prefix, num, count, total := make([]int, len(arr)), 0, 0, 0 for i, v := range arr { num ^= v prefix[i] = num } for i := 0; i < len(prefix)-1; i++ { for k := i + 1; k < len(prefix); k++ { total = prefix[k] if i > 0 { total ^= prefix[i-1] } if total == 0 { count += k - i } } } return count }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442. Count Triplets That Can Form Two Arrays of Equal XOR_test.go
leetcode/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442. Count Triplets That Can Form Two Arrays of Equal XOR_test.go
package leetcode import ( "fmt" "testing" ) type question1442 struct { para1442 ans1442 } // para 是参数 // one 代表第一个参数 type para1442 struct { arr []int } // ans 是答案 // one 代表第一个答案 type ans1442 struct { one int } func Test_Problem1442(t *testing.T) { qs := []question1442{ { para1442{[]int{2, 3, 1, 6, 7}}, ans1442{4}, }, { para1442{[]int{1, 1, 1, 1, 1}}, ans1442{10}, }, { para1442{[]int{2, 3}}, ans1442{0}, }, { para1442{[]int{1, 3, 5, 7, 9}}, ans1442{3}, }, { para1442{[]int{7, 11, 12, 9, 5, 2, 7, 17, 22}}, ans1442{8}, }, } fmt.Printf("------------------------Leetcode Problem 1442------------------------\n") for _, q := range qs { _, p := q.ans1442, q.para1442 fmt.Printf("【input】:%v 【output】:%v\n", p, countTriplets(p.arr)) } 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/0112.Path-Sum/112. Path Sum_test.go
leetcode/0112.Path-Sum/112. Path Sum_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question112 struct { para112 ans112 } // para 是参数 // one 代表第一个参数 type para112 struct { one []int sum int } // ans 是答案 // one 代表第一个答案 type ans112 struct { one bool } func Test_Problem112(t *testing.T) { qs := []question112{ { para112{[]int{}, 0}, ans112{false}, }, { para112{[]int{5, 4, 8, 11, structures.NULL, 13, 4, 7, 2, structures.NULL, structures.NULL, structures.NULL, 1}, 22}, ans112{true}, }, } fmt.Printf("------------------------Leetcode Problem 112------------------------\n") for _, q := range qs { _, p := q.ans112, q.para112 fmt.Printf("【input】:%v ", p) root := structures.Ints2TreeNode(p.one) fmt.Printf("【output】:%v \n", hasPathSum(root, p.sum)) } 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/0112.Path-Sum/112. Path Sum.go
leetcode/0112.Path-Sum/112. Path 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 hasPathSum(root *TreeNode, sum int) bool { if root == nil { return false } if root.Left == nil && root.Right == nil { return sum == root.Val } return hasPathSum(root.Left, sum-root.Val) || hasPathSum(root.Right, sum-root.Val) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0714.Best-Time-to-Buy-and-Sell-Stock-with-Transaction-Fee/714. Best Time to Buy and Sell Stock with Transaction Fee_test.go
leetcode/0714.Best-Time-to-Buy-and-Sell-Stock-with-Transaction-Fee/714. Best Time to Buy and Sell Stock with Transaction Fee_test.go
package leetcode import ( "fmt" "testing" ) type question714 struct { para714 ans714 } // para 是参数 // one 代表第一个参数 type para714 struct { one []int f int } // ans 是答案 // one 代表第一个答案 type ans714 struct { one int } func Test_Problem714(t *testing.T) { qs := []question714{ { para714{[]int{}, 0}, ans714{0}, }, { para714{[]int{7, 1, 5, 3, 6, 4}, 0}, ans714{7}, }, { para714{[]int{7, 6, 4, 3, 1}, 0}, ans714{0}, }, { para714{[]int{1, 3, 2, 8, 4, 9}, 2}, ans714{8}, }, } fmt.Printf("------------------------Leetcode Problem 714------------------------\n") for _, q := range qs { _, p := q.ans714, q.para714 fmt.Printf("【input】:%v 【output】:%v\n", p, maxProfit714(p.one, p.f)) } 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/0714.Best-Time-to-Buy-and-Sell-Stock-with-Transaction-Fee/714. Best Time to Buy and Sell Stock with Transaction Fee.go
leetcode/0714.Best-Time-to-Buy-and-Sell-Stock-with-Transaction-Fee/714. Best Time to Buy and Sell Stock with Transaction Fee.go
package leetcode import ( "math" ) // 解法一 模拟 DP func maxProfit714(prices []int, fee 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] for i := 1; i < len(prices); i++ { buy[i] = max(buy[i-1], sell[i-1]-prices[i]) sell[i] = max(sell[i-1], buy[i-1]+prices[i]-fee) } return sell[len(sell)-1] } func max(a int, b int) int { if a > b { return a } return b } // 解法二 优化辅助空间的 DP func maxProfit714_1(prices []int, fee int) int { sell, buy := 0, -prices[0] for i := 1; i < len(prices); i++ { sell = max(sell, buy+prices[i]-fee) buy = max(buy, sell-prices[i]) } return sell }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0463.Island-Perimeter/463. Island Perimeter_test.go
leetcode/0463.Island-Perimeter/463. Island Perimeter_test.go
package leetcode import ( "fmt" "testing" ) type question463 struct { para463 ans463 } // para 是参数 // one 代表第一个参数 type para463 struct { one [][]int } // ans 是答案 // one 代表第一个答案 type ans463 struct { one int } func Test_Problem463(t *testing.T) { qs := []question463{ { para463{[][]int{{0, 1, 0, 0}, {1, 1, 1, 0}, {0, 1, 0, 0}, {1, 1, 0, 0}}}, ans463{16}, }, } fmt.Printf("------------------------Leetcode Problem 463------------------------\n") for _, q := range qs { _, p := q.ans463, q.para463 fmt.Printf("【input】:%v 【output】:%v\n", p, islandPerimeter(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/0463.Island-Perimeter/463. Island Perimeter.go
leetcode/0463.Island-Perimeter/463. Island Perimeter.go
package leetcode func islandPerimeter(grid [][]int) int { counter := 0 for i := 0; i < len(grid); i++ { for j := 0; j < len(grid[0]); j++ { if grid[i][j] == 1 { if i-1 < 0 || grid[i-1][j] == 0 { counter++ } if i+1 >= len(grid) || grid[i+1][j] == 0 { counter++ } if j-1 < 0 || grid[i][j-1] == 0 { counter++ } if j+1 >= len(grid[0]) || grid[i][j+1] == 0 { counter++ } } } } return counter }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0747.Largest-Number-At-Least-Twice-of-Others/747. Largest Number At Least Twice of Others_test.go
leetcode/0747.Largest-Number-At-Least-Twice-of-Others/747. Largest Number At Least Twice of Others_test.go
package leetcode import ( "fmt" "testing" ) type question747 struct { para747 ans747 } // para 是参数 // one 代表第一个参数 type para747 struct { nums []int } // ans 是答案 // one 代表第一个答案 type ans747 struct { one int } func Test_Problem747(t *testing.T) { qs := []question747{ { para747{[]int{3, 6, 1, 0}}, ans747{1}, }, { para747{[]int{1, 2, 3, 4}}, ans747{-1}, }, { para747{[]int{1}}, ans747{0}, }, } fmt.Printf("------------------------Leetcode Problem 747------------------------\n") for _, q := range qs { _, p := q.ans747, q.para747 fmt.Printf("【input】:%v 【output】:%v\n", p, dominantIndex(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/0747.Largest-Number-At-Least-Twice-of-Others/747. Largest Number At Least Twice of Others.go
leetcode/0747.Largest-Number-At-Least-Twice-of-Others/747. Largest Number At Least Twice of Others.go
package leetcode func dominantIndex(nums []int) int { maxNum, flag, index := 0, false, 0 for i, v := range nums { if v > maxNum { maxNum = v index = i } } for _, v := range nums { if v != maxNum && 2*v > maxNum { flag = true } } if flag { return -1 } return index }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0973.K-Closest-Points-to-Origin/973. K Closest Points to Origin_test.go
leetcode/0973.K-Closest-Points-to-Origin/973. K Closest Points to Origin_test.go
package leetcode import ( "fmt" "testing" ) type question973 struct { para973 ans973 } // para 是参数 // one 代表第一个参数 type para973 struct { one [][]int two int } // ans 是答案 // one 代表第一个答案 type ans973 struct { one [][]int } func Test_Problem973(t *testing.T) { qs := []question973{ { para973{[][]int{{1, 3}, {-2, 2}}, 1}, ans973{[][]int{{-2, 2}}}, }, { para973{[][]int{{1, 3}, {-2, 2}}, 0}, ans973{[][]int{{}}}, }, { para973{[][]int{{3, 3}, {5, -1}, {-2, 4}}, 2}, ans973{[][]int{{3, 3}, {-2, 4}}}, }, { para973{[][]int{{0, 1}, {1, 0}}, 2}, ans973{[][]int{{1, 0}, {0, 1}}}, }, } fmt.Printf("------------------------Leetcode Problem 973------------------------\n") for _, q := range qs { _, p := q.ans973, q.para973 fmt.Printf("【input】:%v 【output】:%v\n", p, KClosest(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/0973.K-Closest-Points-to-Origin/973. K Closest Points to Origin.go
leetcode/0973.K-Closest-Points-to-Origin/973. K Closest Points to Origin.go
package leetcode import "sort" // KClosest define func KClosest(points [][]int, K int) [][]int { sort.Slice(points, func(i, j int) bool { return points[i][0]*points[i][0]+points[i][1]*points[i][1] < points[j][0]*points[j][0]+points[j][1]*points[j][1] }) ans := make([][]int, K) for i := 0; i < K; i++ { ans[i] = points[i] } return ans }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0146.LRU-Cache/146. LRU Cache.go
leetcode/0146.LRU-Cache/146. LRU Cache.go
package leetcode type LRUCache struct { head, tail *Node Keys map[int]*Node Cap int } type Node struct { Key, Val int Prev, Next *Node } func Constructor(capacity int) LRUCache { return LRUCache{Keys: make(map[int]*Node), Cap: capacity} } func (this *LRUCache) Get(key int) int { if node, ok := this.Keys[key]; ok { this.Remove(node) this.Add(node) return node.Val } return -1 } func (this *LRUCache) Put(key int, value int) { if node, ok := this.Keys[key]; ok { node.Val = value this.Remove(node) this.Add(node) return } else { node = &Node{Key: key, Val: value} this.Keys[key] = node this.Add(node) } if len(this.Keys) > this.Cap { delete(this.Keys, this.tail.Key) this.Remove(this.tail) } } func (this *LRUCache) Add(node *Node) { node.Prev = nil node.Next = this.head if this.head != nil { this.head.Prev = node } this.head = node if this.tail == nil { this.tail = node this.tail.Next = nil } } func (this *LRUCache) Remove(node *Node) { if node == this.head { this.head = node.Next node.Next = nil return } if node == this.tail { this.tail = node.Prev node.Prev.Next = nil node.Prev = nil return } node.Prev.Next = node.Next node.Next.Prev = node.Prev }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0146.LRU-Cache/146. LRU Cache_test.go
leetcode/0146.LRU-Cache/146. LRU Cache_test.go
package leetcode import ( "fmt" "testing" ) func Test_Problem146(t *testing.T) { obj := Constructor(2) fmt.Printf("obj = %v\n", MList2Ints(&obj)) obj.Put(1, 1) fmt.Printf("obj = %v\n", MList2Ints(&obj)) obj.Put(2, 2) fmt.Printf("obj = %v\n", MList2Ints(&obj)) param1 := obj.Get(1) fmt.Printf("param_1 = %v obj = %v\n", param1, MList2Ints(&obj)) obj.Put(3, 3) fmt.Printf("obj = %v\n", MList2Ints(&obj)) param1 = obj.Get(2) fmt.Printf("param_1 = %v obj = %v\n", param1, MList2Ints(&obj)) obj.Put(4, 4) fmt.Printf("obj = %v\n", MList2Ints(&obj)) param1 = obj.Get(1) fmt.Printf("param_1 = %v obj = %v\n", param1, MList2Ints(&obj)) param1 = obj.Get(3) fmt.Printf("param_1 = %v obj = %v\n", param1, MList2Ints(&obj)) param1 = obj.Get(4) fmt.Printf("param_1 = %v obj = %v\n", param1, MList2Ints(&obj)) } func MList2Ints(lru *LRUCache) [][]int { res := [][]int{} head := lru.head for head != nil { tmp := []int{head.Key, head.Val} res = append(res, tmp) head = head.Next } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1011.Capacity-To-Ship-Packages-Within-D-Days/1011. Capacity To Ship Packages Within D Days.go
leetcode/1011.Capacity-To-Ship-Packages-Within-D-Days/1011. Capacity To Ship Packages Within D Days.go
package leetcode func shipWithinDays(weights []int, D int) int { maxNum, sum := 0, 0 for _, num := range weights { sum += num if num > maxNum { maxNum = num } } if D == 1 { return sum } low, high := maxNum, sum for low < high { mid := low + (high-low)>>1 if calSum(mid, D, weights) { high = mid } else { low = mid + 1 } } return low } func calSum(mid, m int, nums []int) bool { sum, count := 0, 0 for _, v := range nums { sum += v if sum > mid { sum = v count++ // 分成 m 块,只需要插桩 m -1 个 if count > m-1 { 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/1011.Capacity-To-Ship-Packages-Within-D-Days/1011. Capacity To Ship Packages Within D Days_test.go
leetcode/1011.Capacity-To-Ship-Packages-Within-D-Days/1011. Capacity To Ship Packages Within D Days_test.go
package leetcode import ( "fmt" "testing" ) type question1011 struct { para1011 ans1011 } // para 是参数 // one 代表第一个参数 type para1011 struct { weights []int D int } // ans 是答案 // one 代表第一个答案 type ans1011 struct { one int } func Test_Problem1011(t *testing.T) { qs := []question1011{ { para1011{[]int{7, 2, 5, 10, 8}, 2}, ans1011{18}, }, { para1011{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5}, ans1011{15}, }, { para1011{[]int{3, 2, 2, 4, 1, 4}, 3}, ans1011{6}, }, { para1011{[]int{1, 2, 3, 1, 1}, 4}, ans1011{3}, }, } fmt.Printf("------------------------Leetcode Problem 1011------------------------\n") for _, q := range qs { _, p := q.ans1011, q.para1011 fmt.Printf("【input】:%v 【output】:%v\n", p, shipWithinDays(p.weights, 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/0052.N-Queens-II/52. N-Queens II.go
leetcode/0052.N-Queens-II/52. N-Queens II.go
package leetcode // 解法一,暴力打表法 func totalNQueens(n int) int { res := []int{0, 1, 0, 0, 2, 10, 4, 40, 92, 352, 724} return res[n] } // 解法二,DFS 回溯法 func totalNQueens1(n int) int { col, dia1, dia2, row, res := make([]bool, n), make([]bool, 2*n-1), make([]bool, 2*n-1), []int{}, 0 putQueen52(n, 0, &col, &dia1, &dia2, &row, &res) return res } // 尝试在一个n皇后问题中, 摆放第index行的皇后位置 func putQueen52(n, index int, col, dia1, dia2 *[]bool, row *[]int, res *int) { if index == n { *res++ return } for i := 0; i < n; i++ { // 尝试将第index行的皇后摆放在第i列 if !(*col)[i] && !(*dia1)[index+i] && !(*dia2)[index-i+n-1] { *row = append(*row, i) (*col)[i] = true (*dia1)[index+i] = true (*dia2)[index-i+n-1] = true putQueen52(n, index+1, col, dia1, dia2, row, res) (*col)[i] = false (*dia1)[index+i] = false (*dia2)[index-i+n-1] = false *row = (*row)[:len(*row)-1] } } return } // 解法三 二进制位操作法 // class Solution { // public: // int totalNQueens(int n) { // int ans=0; // int row=0,leftDiagonal=0,rightDiagonal=0; // int bit=(1<<n)-1;//to clear high bits of the 32-bit int // Queens(bit,row,leftDiagonal,rightDiagonal,ans); // return ans; // } // void Queens(int bit,int row,int leftDiagonal,int rightDiagonal,int &ans){ // int cur=(~(row|leftDiagonal|rightDiagonal))&bit;//possible place for this queen // if (!cur) return;//no pos for this queen // while(cur){ // int curPos=(cur&(~cur + 1))&bit;//choose possible place in the right // //update row,ld and rd // row+=curPos; // if (row==bit) ans++;//last row // else Queens(bit,row,((leftDiagonal|curPos)<<1)&bit,((rightDiagonal|curPos)>>1)&bit,ans); // cur-=curPos;//for next possible place // row-=curPos;//reset row // } // } // };
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0052.N-Queens-II/52. N-Queens II_test.go
leetcode/0052.N-Queens-II/52. N-Queens II_test.go
package leetcode import ( "fmt" "testing" ) type question52 struct { para52 ans52 } // para 是参数 // one 代表第一个参数 type para52 struct { one int } // ans 是答案 // one 代表第一个答案 type ans52 struct { one int } func Test_Problem52(t *testing.T) { qs := []question52{ { para52{1}, ans52{1}, }, { para52{2}, ans52{0}, }, { para52{3}, ans52{0}, }, { para52{4}, ans52{2}, }, { para52{5}, ans52{10}, }, { para52{6}, ans52{4}, }, { para52{7}, ans52{40}, }, { para52{8}, ans52{92}, }, { para52{9}, ans52{352}, }, { para52{10}, ans52{724}, }, } fmt.Printf("------------------------Leetcode Problem 52------------------------\n") for _, q := range qs { _, p := q.ans52, q.para52 fmt.Printf("【input】:%v 【output】:%v\n", p, totalNQueens(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/0076.Minimum-Window-Substring/76. Minimum Window Substring_test.go
leetcode/0076.Minimum-Window-Substring/76. Minimum Window Substring_test.go
package leetcode import ( "fmt" "testing" ) type question76 struct { para76 ans76 } // para 是参数 // one 代表第一个参数 type para76 struct { s string p string } // ans 是答案 // one 代表第一个答案 type ans76 struct { one string } func Test_Problem76(t *testing.T) { qs := []question76{ { para76{"ADOBECODEBANC", "ABC"}, ans76{"BANC"}, }, { para76{"a", "aa"}, ans76{""}, }, { para76{"a", "a"}, ans76{"a"}, }, } fmt.Printf("------------------------Leetcode Problem 76------------------------\n") for _, q := range qs { _, p := q.ans76, q.para76 fmt.Printf("【input】:%v 【output】:%v\n\n\n", p, minWindow(p.s, p.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/0076.Minimum-Window-Substring/76. Minimum Window Substring.go
leetcode/0076.Minimum-Window-Substring/76. Minimum Window Substring.go
package leetcode func minWindow(s string, t string) string { if s == "" || t == "" { return "" } var tFreq, sFreq [256]int result, left, right, finalLeft, finalRight, minW, count := "", 0, -1, -1, -1, len(s)+1, 0 for i := 0; i < len(t); i++ { tFreq[t[i]-'a']++ } for left < len(s) { if right+1 < len(s) && count < len(t) { sFreq[s[right+1]-'a']++ if sFreq[s[right+1]-'a'] <= tFreq[s[right+1]-'a'] { count++ } right++ } else { if right-left+1 < minW && count == len(t) { minW = right - left + 1 finalLeft = left finalRight = right } if sFreq[s[left]-'a'] == tFreq[s[left]-'a'] { count-- } sFreq[s[left]-'a']-- left++ } } if finalLeft != -1 { result = string(s[finalLeft : finalRight+1]) } return result }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0424.Longest-Repeating-Character-Replacement/424. Longest Repeating Character Replacement.go
leetcode/0424.Longest-Repeating-Character-Replacement/424. Longest Repeating Character Replacement.go
package leetcode func characterReplacement(s string, k int) int { res, left, counter, freq := 0, 0, 0, make([]int, 26) for right := 0; right < len(s); right++ { freq[s[right]-'A']++ counter = max(counter, freq[s[right]-'A']) for right-left+1-counter > k { freq[s[left]-'A']-- left++ } res = max(res, right-left+1) } 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/0424.Longest-Repeating-Character-Replacement/424. Longest Repeating Character Replacement_test.go
leetcode/0424.Longest-Repeating-Character-Replacement/424. Longest Repeating Character Replacement_test.go
package leetcode import ( "fmt" "testing" ) type question424 struct { para424 ans424 } // para 是参数 // one 代表第一个参数 type para424 struct { s string k int } // ans 是答案 // one 代表第一个答案 type ans424 struct { one int } func Test_Problem424(t *testing.T) { qs := []question424{ { para424{"AABABBA", 1}, ans424{4}, }, { para424{"ABBB", 2}, ans424{4}, }, { para424{"BAAA", 0}, ans424{3}, }, { para424{"ABCDE", 1}, ans424{2}, }, { para424{"BAAAB", 2}, ans424{5}, }, } fmt.Printf("------------------------Leetcode Problem 424------------------------\n") for _, q := range qs { _, p := q.ans424, q.para424 fmt.Printf("【input】:%v 【output】:%v\n", p, characterReplacement(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/0349.Intersection-of-Two-Arrays/349. Intersection of Two Arrays.go
leetcode/0349.Intersection-of-Two-Arrays/349. Intersection of Two Arrays.go
package leetcode func intersection(nums1 []int, nums2 []int) []int { m := map[int]bool{} var res []int for _, n := range nums1 { m[n] = true } for _, n := range nums2 { if m[n] { delete(m, n) res = append(res, n) } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0349.Intersection-of-Two-Arrays/349. Intersection of Two Arrays_test.go
leetcode/0349.Intersection-of-Two-Arrays/349. Intersection of Two Arrays_test.go
package leetcode import ( "fmt" "testing" ) type question349 struct { para349 ans349 } // para 是参数 // one 代表第一个参数 type para349 struct { one []int another []int } // ans 是答案 // one 代表第一个答案 type ans349 struct { one []int } func Test_Problem349(t *testing.T) { qs := []question349{ { para349{[]int{}, []int{}}, ans349{[]int{}}, }, { para349{[]int{1}, []int{1}}, ans349{[]int{1}}, }, { para349{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}}, ans349{[]int{1, 2, 3, 4}}, }, { para349{[]int{1, 2, 2, 1}, []int{2, 2}}, ans349{[]int{2}}, }, { para349{[]int{1}, []int{9, 9, 9, 9, 9}}, ans349{[]int{}}, }, { para349{[]int{4, 9, 5}, []int{9, 4, 9, 8, 4}}, ans349{[]int{9, 4}}, }, // 如需多个测试,可以复制上方元素。 } fmt.Printf("------------------------Leetcode Problem 349------------------------\n") for _, q := range qs { _, p := q.ans349, q.para349 fmt.Printf("【input】:%v 【output】:%v\n", p, intersection(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/0599.Minimum-Index-Sum-of-Two-Lists/599. Minimum Index Sum of Two Lists_test.go
leetcode/0599.Minimum-Index-Sum-of-Two-Lists/599. Minimum Index Sum of Two Lists_test.go
package leetcode import ( "fmt" "testing" ) type question599 struct { para599 ans599 } // para 是参数 // one 代表第一个参数 type para599 struct { one []string two []string } // ans 是答案 // one 代表第一个答案 type ans599 struct { one []string } func Test_Problem599(t *testing.T) { qs := []question599{ { para599{[]string{"Shogun", "Tapioca Express", "Burger King", "KFC"}, []string{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"}}, ans599{[]string{"Shogun"}}, }, { para599{[]string{"Shogun", "Tapioca Express", "Burger King", "KFC"}, []string{"KFC", "Shogun", "Burger King"}}, ans599{[]string{"Shogun"}}, }, } fmt.Printf("------------------------Leetcode Problem 599------------------------\n") for _, q := range qs { _, p := q.ans599, q.para599 fmt.Printf("【input】:%v 【output】:%v\n", p, findRestaurant(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/0599.Minimum-Index-Sum-of-Two-Lists/599. Minimum Index Sum of Two Lists.go
leetcode/0599.Minimum-Index-Sum-of-Two-Lists/599. Minimum Index Sum of Two Lists.go
package leetcode func findRestaurant(list1 []string, list2 []string) []string { m, ans := make(map[string]int, len(list1)), []string{} for i, r := range list1 { m[r] = i } for j, r := range list2 { if _, ok := m[r]; ok { m[r] += j if len(ans) == 0 || m[r] == m[ans[0]] { ans = append(ans, r) } else if m[r] < m[ans[0]] { ans = []string{r} } } } return ans }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false