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/1105.Filling-Bookcase-Shelves/1105. Filling Bookcase Shelves.go | leetcode/1105.Filling-Bookcase-Shelves/1105. Filling Bookcase Shelves.go | package leetcode
func minHeightShelves(books [][]int, shelfWidth int) int {
dp := make([]int, len(books)+1)
dp[0] = 0
for i := 1; i <= len(books); i++ {
width, height := books[i-1][0], books[i-1][1]
dp[i] = dp[i-1] + height
for j := i - 1; j > 0 && width+books[j-1][0] <= shelfWidth; j-- {
height = max(height, books[j-1][1])
width += books[j-1][0]
dp[i] = min(dp[i], dp[j-1]+height)
}
}
return dp[len(books)]
}
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/1105.Filling-Bookcase-Shelves/1105. Filling Bookcase Shelves_test.go | leetcode/1105.Filling-Bookcase-Shelves/1105. Filling Bookcase Shelves_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1105 struct {
para1105
ans1105
}
// para 是参数
// one 代表第一个参数
type para1105 struct {
one [][]int
w int
}
// ans 是答案
// one 代表第一个答案
type ans1105 struct {
one int
}
func Test_Problem1105(t *testing.T) {
qs := []question1105{
{
para1105{[][]int{{1, 1}, {2, 3}, {2, 3}, {1, 1}, {1, 1}, {1, 1}, {1, 2}}, 4},
ans1105{6},
},
}
fmt.Printf("------------------------Leetcode Problem 1105------------------------\n")
for _, q := range qs {
_, p := q.ans1105, q.para1105
fmt.Printf("【input】:%v 【output】:%v\n", p, minHeightShelves(p.one, p.w))
}
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/0756.Pyramid-Transition-Matrix/756. Pyramid Transition Matrix.go | leetcode/0756.Pyramid-Transition-Matrix/756. Pyramid Transition Matrix.go | package leetcode
func pyramidTransition(bottom string, allowed []string) bool {
pyramid := make(map[string][]string)
for _, v := range allowed {
pyramid[v[:len(v)-1]] = append(pyramid[v[:len(v)-1]], string(v[len(v)-1]))
}
return dfsT(bottom, "", pyramid)
}
func dfsT(bottom, above string, pyramid map[string][]string) bool {
if len(bottom) == 2 && len(above) == 1 {
return true
}
if len(bottom) == len(above)+1 {
return dfsT(above, "", pyramid)
}
base := bottom[len(above) : len(above)+2]
if data, ok := pyramid[base]; ok {
for _, key := range data {
if dfsT(bottom, above+key, pyramid) {
return true
}
}
}
return false
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0756.Pyramid-Transition-Matrix/756. Pyramid Transition Matrix_test.go | leetcode/0756.Pyramid-Transition-Matrix/756. Pyramid Transition Matrix_test.go | package leetcode
import (
"fmt"
"testing"
)
type question756 struct {
para756
ans756
}
// para 是参数
// one 代表第一个参数
type para756 struct {
b string
a []string
}
// ans 是答案
// one 代表第一个答案
type ans756 struct {
one bool
}
func Test_Problem756(t *testing.T) {
qs := []question756{
{
para756{"BCD", []string{"BCG", "CDE", "GEA", "FFF"}},
ans756{true},
},
{
para756{"AABA", []string{"AAA", "AAB", "ABA", "ABB", "BAC"}},
ans756{false},
},
}
fmt.Printf("------------------------Leetcode Problem 756------------------------\n")
for _, q := range qs {
_, p := q.ans756, q.para756
fmt.Printf("【input】:%v 【output】:%v\n", p, pyramidTransition(p.b, p.a))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1846.Maximum-Element-After-Decreasing-and-Rearranging/1846. Maximum Element After Decreasing and Rearranging_test.go | leetcode/1846.Maximum-Element-After-Decreasing-and-Rearranging/1846. Maximum Element After Decreasing and Rearranging_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1846 struct {
para1846
ans1846
}
// para 是参数
// one 代表第一个参数
type para1846 struct {
arr []int
}
// ans 是答案
// one 代表第一个答案
type ans1846 struct {
one int
}
func Test_Problem1846(t *testing.T) {
qs := []question1846{
{
para1846{[]int{2, 2, 1, 2, 1}},
ans1846{2},
},
{
para1846{[]int{100, 1, 1000}},
ans1846{3},
},
{
para1846{[]int{1, 2, 3, 4, 5}},
ans1846{5},
},
}
fmt.Printf("------------------------Leetcode Problem 1846------------------------\n")
for _, q := range qs {
_, p := q.ans1846, q.para1846
fmt.Printf("【input】:%v 【output】:%v\n", p, maximumElementAfterDecrementingAndRearranging(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/1846.Maximum-Element-After-Decreasing-and-Rearranging/1846. Maximum Element After Decreasing and Rearranging.go | leetcode/1846.Maximum-Element-After-Decreasing-and-Rearranging/1846. Maximum Element After Decreasing and Rearranging.go | package leetcode
func maximumElementAfterDecrementingAndRearranging(arr []int) int {
n := len(arr)
count := make([]int, n+1)
for _, v := range arr {
count[min(v, n)]++
}
miss := 0
for _, c := range count[1:] {
if c == 0 {
miss++
} else {
miss -= min(c-1, miss)
}
}
return n - miss
}
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/1624.Largest-Substring-Between-Two-Equal-Characters/1624. Largest Substring Between Two Equal Characters_test.go | leetcode/1624.Largest-Substring-Between-Two-Equal-Characters/1624. Largest Substring Between Two Equal Characters_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1624 struct {
para1624
ans1624
}
// para 是参数
// one 代表第一个参数
type para1624 struct {
s string
}
// ans 是答案
// one 代表第一个答案
type ans1624 struct {
one int
}
func Test_Problem1624(t *testing.T) {
qs := []question1624{
{
para1624{"aa"},
ans1624{0},
},
{
para1624{"abca"},
ans1624{2},
},
{
para1624{"cbzxy"},
ans1624{-1},
},
{
para1624{"cabbac"},
ans1624{4},
},
}
fmt.Printf("------------------------Leetcode Problem 1624------------------------\n")
for _, q := range qs {
_, p := q.ans1624, q.para1624
fmt.Printf("【input】:%v 【output】:%v \n", p, maxLengthBetweenEqualCharacters(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/1624.Largest-Substring-Between-Two-Equal-Characters/1624. Largest Substring Between Two Equal Characters.go | leetcode/1624.Largest-Substring-Between-Two-Equal-Characters/1624. Largest Substring Between Two Equal Characters.go | package leetcode
import "strings"
func maxLengthBetweenEqualCharacters(s string) int {
res := -1
for k, v := range s {
tmp := strings.LastIndex(s, string(v))
if tmp > 0 {
if res < tmp-k-1 {
res = tmp - k - 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/0240.Search-a-2D-Matrix-II/240. Search a 2D Matrix II.go | leetcode/0240.Search-a-2D-Matrix-II/240. Search a 2D Matrix II.go | package leetcode
// 解法一 模拟,时间复杂度 O(m+n)
func searchMatrix240(matrix [][]int, target int) bool {
if len(matrix) == 0 {
return false
}
row, col := 0, len(matrix[0])-1
for col >= 0 && row <= len(matrix)-1 {
if target == matrix[row][col] {
return true
} else if target > matrix[row][col] {
row++
} else {
col--
}
}
return false
}
// 解法二 二分搜索,时间复杂度 O(n log n)
func searchMatrix2401(matrix [][]int, target int) bool {
if len(matrix) == 0 {
return false
}
for _, row := range matrix {
low, high := 0, len(matrix[0])-1
for low <= high {
mid := low + (high-low)>>1
if row[mid] > target {
high = mid - 1
} else if row[mid] < target {
low = mid + 1
} else {
return true
}
}
}
return false
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0240.Search-a-2D-Matrix-II/240. Search a 2D Matrix II_test.go | leetcode/0240.Search-a-2D-Matrix-II/240. Search a 2D Matrix II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question240 struct {
para240
ans240
}
// para 是参数
// one 代表第一个参数
type para240 struct {
matrix [][]int
target int
}
// ans 是答案
// one 代表第一个答案
type ans240 struct {
one bool
}
func Test_Problem240(t *testing.T) {
qs := []question240{
{
para240{[][]int{{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}}, 5},
ans240{true},
},
{
para240{[][]int{{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}}, 20},
ans240{false},
},
}
fmt.Printf("------------------------Leetcode Problem 240------------------------\n")
for _, q := range qs {
_, p := q.ans240, q.para240
fmt.Printf("【input】:%v 【output】:%v\n", p, searchMatrix240(p.matrix, 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/0229.Majority-Element-II/229. Majority Element II.go | leetcode/0229.Majority-Element-II/229. Majority Element II.go | package leetcode
// 解法一 时间复杂度 O(n) 空间复杂度 O(1)
func majorityElement229(nums []int) []int {
// since we are checking if a num appears more than 1/3 of the time
// it is only possible to have at most 2 nums (>1/3 + >1/3 = >2/3)
count1, count2, candidate1, candidate2 := 0, 0, 0, 1
// Select Candidates
for _, num := range nums {
if num == candidate1 {
count1++
} else if num == candidate2 {
count2++
} else if count1 <= 0 {
// We have a bad first candidate, replace!
candidate1, count1 = num, 1
} else if count2 <= 0 {
// We have a bad second candidate, replace!
candidate2, count2 = num, 1
} else {
// Both candidates suck, boo!
count1--
count2--
}
}
// Recount!
count1, count2 = 0, 0
for _, num := range nums {
if num == candidate1 {
count1++
} else if num == candidate2 {
count2++
}
}
length := len(nums)
if count1 > length/3 && count2 > length/3 {
return []int{candidate1, candidate2}
}
if count1 > length/3 {
return []int{candidate1}
}
if count2 > length/3 {
return []int{candidate2}
}
return []int{}
}
// 解法二 时间复杂度 O(n) 空间复杂度 O(n)
func majorityElement229_1(nums []int) []int {
result, m := make([]int, 0), make(map[int]int)
for _, val := range nums {
if v, ok := m[val]; ok {
m[val] = v + 1
} else {
m[val] = 1
}
}
for k, v := range m {
if v > len(nums)/3 {
result = append(result, k)
}
}
return result
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0229.Majority-Element-II/229. Majority Element II_test.go | leetcode/0229.Majority-Element-II/229. Majority Element II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question229 struct {
para229
ans229
}
// para 是参数
// one 代表第一个参数
type para229 struct {
s []int
}
// ans 是答案
// one 代表第一个答案
type ans229 struct {
one []int
}
func Test_Problem229(t *testing.T) {
qs := []question229{
{
para229{[]int{3, 2, 3}},
ans229{[]int{3}},
},
{
para229{[]int{1, 1, 1, 3, 3, 2, 2, 2}},
ans229{[]int{1, 2}},
},
}
fmt.Printf("------------------------Leetcode Problem 229------------------------\n")
for _, q := range qs {
_, p := q.ans229, q.para229
fmt.Printf("【input】:%v 【output】:%v\n", p, majorityElement229(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/0765.Couples-Holding-Hands/765. Couples Holding Hands_test.go | leetcode/0765.Couples-Holding-Hands/765. Couples Holding Hands_test.go | package leetcode
import (
"fmt"
"testing"
)
type question765 struct {
para765
ans765
}
// para 是参数
// one 代表第一个参数
type para765 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans765 struct {
one int
}
func Test_Problem765(t *testing.T) {
qs := []question765{
{
para765{[]int{0, 2, 1, 3}},
ans765{1},
},
{
para765{[]int{3, 2, 0, 1}},
ans765{0},
},
{
para765{[]int{3, 1, 4, 0, 2, 5}},
ans765{2},
},
{
para765{[]int{10, 7, 4, 2, 3, 0, 9, 11, 1, 5, 6, 8}},
ans765{4},
},
}
fmt.Printf("------------------------Leetcode Problem 765------------------------\n")
for _, q := range qs {
_, p := q.ans765, q.para765
fmt.Printf("【input】:%v 【output】:%v\n", p, minSwapsCouples(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/0765.Couples-Holding-Hands/765. Couples Holding Hands.go | leetcode/0765.Couples-Holding-Hands/765. Couples Holding Hands.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/template"
)
func minSwapsCouples(row []int) int {
if len(row)&1 == 1 {
return 0
}
uf := template.UnionFind{}
uf.Init(len(row))
for i := 0; i < len(row)-1; i = i + 2 {
uf.Union(i, i+1)
}
for i := 0; i < len(row)-1; i = i + 2 {
if uf.Find(row[i]) != uf.Find(row[i+1]) {
uf.Union(row[i], row[i+1])
}
}
return len(row)/2 - uf.TotalCount()
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0301.Remove-Invalid-Parentheses/301.Remove Invalid Parentheses_test.go | leetcode/0301.Remove-Invalid-Parentheses/301.Remove Invalid Parentheses_test.go | package leetcode
import (
"fmt"
"testing"
)
type question301 struct {
para301
ans301
}
// s 是参数
type para301 struct {
s string
}
// ans 是答案
type ans301 struct {
ans []string
}
func Test_Problem301(t *testing.T) {
qs := []question301{
{
para301{"()())()"},
ans301{[]string{"(())()", "()()()"}},
},
{
para301{"(a)())()"},
ans301{[]string{"(a())()", "(a)()()"}},
},
{
para301{")("},
ans301{[]string{""}},
},
}
fmt.Printf("------------------------Leetcode Problem 301------------------------\n")
for _, q := range qs {
_, p := q.ans301, q.para301
fmt.Printf("【input】:%v 【output】:%v\n", p, removeInvalidParentheses(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/0301.Remove-Invalid-Parentheses/301.Remove Invalid Parentheses.go | leetcode/0301.Remove-Invalid-Parentheses/301.Remove Invalid Parentheses.go | package leetcode
var (
res []string
mp map[string]int
n int
length int
maxScore int
str string
)
func removeInvalidParentheses(s string) []string {
lmoves, rmoves, lcnt, rcnt := 0, 0, 0, 0
for _, v := range s {
if v == '(' {
lmoves++
lcnt++
} else if v == ')' {
if lmoves != 0 {
lmoves--
} else {
rmoves++
}
rcnt++
}
}
n = len(s)
length = n - lmoves - rmoves
res = []string{}
mp = make(map[string]int)
maxScore = min(lcnt, rcnt)
str = s
backtrace(0, "", lmoves, rmoves, 0)
return res
}
func backtrace(i int, cur string, lmoves int, rmoves int, score int) {
if lmoves < 0 || rmoves < 0 || score < 0 || score > maxScore {
return
}
if lmoves == 0 && rmoves == 0 {
if len(cur) == length {
if _, ok := mp[cur]; !ok {
res = append(res, cur)
mp[cur] = 1
}
return
}
}
if i == n {
return
}
if str[i] == '(' {
backtrace(i+1, cur+string('('), lmoves, rmoves, score+1)
backtrace(i+1, cur, lmoves-1, rmoves, score)
} else if str[i] == ')' {
backtrace(i+1, cur+string(')'), lmoves, rmoves, score-1)
backtrace(i+1, cur, lmoves, rmoves-1, score)
} else {
backtrace(i+1, cur+string(str[i]), lmoves, rmoves, score)
}
}
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/0938.Range-Sum-of-BST/938. Range Sum of BST_test.go | leetcode/0938.Range-Sum-of-BST/938. Range Sum of BST_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question938 struct {
para938
ans938
}
// para 是参数
// one 代表第一个参数
type para938 struct {
one []int
low int
high int
}
// ans 是答案
// one 代表第一个答案
type ans938 struct {
one int
}
func Test_Problem938(t *testing.T) {
qs := []question938{
{
para938{[]int{10, 5, 15, 3, 7, structures.NULL, 18}, 7, 15},
ans938{32},
},
{
para938{[]int{10, 5, 15, 3, 7, 13, 18, 1, structures.NULL, 6}, 6, 10},
ans938{23},
},
}
fmt.Printf("------------------------Leetcode Problem 938------------------------\n")
for _, q := range qs {
_, p := q.ans938, q.para938
fmt.Printf("【input】:%v ", p)
rootOne := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", rangeSumBST(rootOne, 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/0938.Range-Sum-of-BST/938. Range Sum of BST.go | leetcode/0938.Range-Sum-of-BST/938. Range Sum of BST.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func rangeSumBST(root *TreeNode, low int, high int) int {
res := 0
preOrder(root, low, high, &res)
return res
}
func preOrder(root *TreeNode, low, high int, res *int) {
if root == nil {
return
}
if low <= root.Val && root.Val <= high {
*res += root.Val
}
preOrder(root.Left, low, high, res)
preOrder(root.Right, low, high, res)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0492.Construct-the-Rectangle/492.Construct the Rectangle.go | leetcode/0492.Construct-the-Rectangle/492.Construct the Rectangle.go | package leetcode
import "math"
func constructRectangle(area int) []int {
ans := make([]int, 2)
W := int(math.Sqrt(float64(area)))
for W >= 1 {
if area%W == 0 {
ans[0], ans[1] = area/W, W
break
}
W -= 1
}
return ans
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0492.Construct-the-Rectangle/492.Construct the Rectangle_test.go | leetcode/0492.Construct-the-Rectangle/492.Construct the Rectangle_test.go | package leetcode
import (
"fmt"
"testing"
)
type question492 struct {
para492
ans492
}
// area 是参数
type para492 struct {
area int
}
// ans 是答案
type ans492 struct {
ans []int
}
func Test_Problem492(t *testing.T) {
qs := []question492{
{
para492{4},
ans492{[]int{2, 2}},
},
{
para492{37},
ans492{[]int{37, 1}},
},
{
para492{122122},
ans492{[]int{427, 286}},
},
}
fmt.Printf("------------------------Leetcode Problem 492------------------------\n")
for _, q := range qs {
_, p := q.ans492, q.para492
fmt.Printf("【input】:%v 【output】:%v\n", p, constructRectangle(p.area))
}
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/0637.Average-of-Levels-in-Binary-Tree/637. Average of Levels in Binary Tree_test.go | leetcode/0637.Average-of-Levels-in-Binary-Tree/637. Average of Levels in Binary Tree_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question637 struct {
para637
ans637
}
// para 是参数
// one 代表第一个参数
type para637 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans637 struct {
one [][]int
}
func Test_Problem637(t *testing.T) {
qs := []question637{
{
para637{[]int{}},
ans637{[][]int{}},
},
{
para637{[]int{1}},
ans637{[][]int{{1}}},
},
{
para637{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans637{[][]int{{3}, {9, 20}, {15, 7}}},
},
}
fmt.Printf("------------------------Leetcode Problem 637------------------------\n")
for _, q := range qs {
_, p := q.ans637, q.para637
fmt.Printf("【input】:%v ", p)
root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", averageOfLevels(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/0637.Average-of-Levels-in-Binary-Tree/637. Average of Levels in Binary Tree.go | leetcode/0637.Average-of-Levels-in-Binary-Tree/637. Average of Levels in 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 averageOfLevels(root *TreeNode) []float64 {
if root == nil {
return []float64{0}
}
queue := []*TreeNode{}
queue = append(queue, root)
curNum, nextLevelNum, res, count, sum := 1, 0, []float64{}, 1, 0
for len(queue) != 0 {
if curNum > 0 {
node := queue[0]
if node.Left != nil {
queue = append(queue, node.Left)
nextLevelNum++
}
if node.Right != nil {
queue = append(queue, node.Right)
nextLevelNum++
}
curNum--
sum += node.Val
queue = queue[1:]
}
if curNum == 0 {
res = append(res, float64(sum)/float64(count))
curNum, count, nextLevelNum, sum = nextLevelNum, nextLevelNum, 0, 0
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1252.Cells-with-Odd-Values-in-a-Matrix/1252. Cells with Odd Values in a Matrix.go | leetcode/1252.Cells-with-Odd-Values-in-a-Matrix/1252. Cells with Odd Values in a Matrix.go | package leetcode
// 解法一 暴力法
func oddCells(n int, m int, indices [][]int) int {
matrix, res := make([][]int, n), 0
for i := range matrix {
matrix[i] = make([]int, m)
}
for _, indice := range indices {
for i := 0; i < m; i++ {
matrix[indice[0]][i]++
}
for j := 0; j < n; j++ {
matrix[j][indice[1]]++
}
}
for _, m := range matrix {
for _, v := range m {
if v&1 == 1 {
res++
}
}
}
return res
}
// 解法二 暴力法
func oddCells1(n int, m int, indices [][]int) int {
rows, cols, count := make([]int, n), make([]int, m), 0
for _, pair := range indices {
rows[pair[0]]++
cols[pair[1]]++
}
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
if (rows[i]+cols[j])%2 == 1 {
count++
}
}
}
return count
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1252.Cells-with-Odd-Values-in-a-Matrix/1252. Cells with Odd Values in a Matrix_test.go | leetcode/1252.Cells-with-Odd-Values-in-a-Matrix/1252. Cells with Odd Values in a Matrix_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1252 struct {
para1252
ans1252
}
// para 是参数
// one 代表第一个参数
type para1252 struct {
n int
m int
indices [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1252 struct {
one int
}
func Test_Problem1252(t *testing.T) {
qs := []question1252{
{
para1252{2, 3, [][]int{{0, 1}, {1, 1}}},
ans1252{6},
},
{
para1252{2, 2, [][]int{{1, 1}, {0, 0}}},
ans1252{0},
},
}
fmt.Printf("------------------------Leetcode Problem 1252------------------------\n")
for _, q := range qs {
_, p := q.ans1252, q.para1252
fmt.Printf("【input】:%v 【output】:%v\n", p, oddCells(p.n, p.m, p.indices))
}
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/0617.Merge-Two-Binary-Trees/617. Merge Two Binary Trees.go | leetcode/0617.Merge-Two-Binary-Trees/617. Merge Two Binary Trees.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 mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
if root1 == nil {
return root2
}
if root2 == nil {
return root1
}
root1.Val += root2.Val
root1.Left = mergeTrees(root1.Left, root2.Left)
root1.Right = mergeTrees(root1.Right, root2.Right)
return root1
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0617.Merge-Two-Binary-Trees/617. Merge Two Binary Trees_test.go | leetcode/0617.Merge-Two-Binary-Trees/617. Merge Two Binary Trees_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question617 struct {
para617
ans617
}
// para 是参数
// one 代表第一个参数
type para617 struct {
one []int
another []int
}
// ans 是答案
// one 代表第一个答案
type ans617 struct {
one []int
}
func Test_Problem617(t *testing.T) {
qs := []question617{
{
para617{[]int{}, []int{}},
ans617{[]int{}},
},
{
para617{[]int{}, []int{1}},
ans617{[]int{1}},
},
{
para617{[]int{1, 3, 2, 5}, []int{2, 1, 3, structures.NULL, 4, structures.NULL, 7}},
ans617{[]int{3, 4, 5, 5, 4, structures.NULL, 7}},
},
{
para617{[]int{1}, []int{1, 2}},
ans617{[]int{2, 2}},
},
}
fmt.Printf("------------------------Leetcode Problem 617------------------------\n")
for _, q := range qs {
_, p := q.ans617, q.para617
fmt.Printf("【input】:%v ", p)
root1 := structures.Ints2TreeNode(p.one)
root2 := structures.Ints2TreeNode(p.another)
fmt.Printf("【output】:%v \n", mergeTrees(root1, root2))
}
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/0345.Reverse-Vowels-of-a-String/345. Reverse Vowels of a String.go | leetcode/0345.Reverse-Vowels-of-a-String/345. Reverse Vowels of a String.go | package leetcode
func reverseVowels(s string) string {
b := []byte(s)
for i, j := 0, len(b)-1; i < j; {
if !isVowel(b[i]) {
i++
continue
}
if !isVowel(b[j]) {
j--
continue
}
b[i], b[j] = b[j], b[i]
i++
j--
}
return string(b)
}
func isVowel(s byte) bool {
return s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u' || s == 'A' ||
s == 'E' || s == 'I' || s == 'O' || s == 'U'
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0345.Reverse-Vowels-of-a-String/345. Reverse Vowels of a String_test.go | leetcode/0345.Reverse-Vowels-of-a-String/345. Reverse Vowels of a String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question345 struct {
para345
ans345
}
// para 是参数
// one 代表第一个参数
type para345 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans345 struct {
one string
}
func Test_Problem345(t *testing.T) {
qs := []question345{
{
para345{"hello"},
ans345{"holle"},
},
{
para345{"leetcode"},
ans345{"leotcede"},
},
{
para345{"aA"},
ans345{"Aa"},
},
}
fmt.Printf("------------------------Leetcode Problem 345------------------------\n")
for _, q := range qs {
_, p := q.ans345, q.para345
fmt.Printf("【input】:%v 【output】:%v\n", p, reverseVowels(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/0529.Minesweeper/529. Minesweeper.go | leetcode/0529.Minesweeper/529. Minesweeper.go | package leetcode
var dir8 = [][]int{
{-1, -1},
{-1, 0},
{-1, 1},
{0, 1},
{1, 1},
{1, 0},
{1, -1},
{0, -1},
}
func updateBoard(board [][]byte, click []int) [][]byte {
if board[click[0]][click[1]] == 'M' {
board[click[0]][click[1]] = 'X'
return board
}
dfs(board, click[0], click[1])
return board
}
func dfs(board [][]byte, x, y int) {
cnt := 0
for i := 0; i < 8; i++ {
nx, ny := x+dir8[i][0], y+dir8[i][1]
if isInBoard(board, nx, ny) && board[nx][ny] == 'M' {
cnt++
}
}
if cnt > 0 {
board[x][y] = byte(cnt + '0')
return
}
board[x][y] = 'B'
for i := 0; i < 8; i++ {
nx, ny := x+dir8[i][0], y+dir8[i][1]
if isInBoard(board, nx, ny) && board[nx][ny] != 'B' {
dfs(board, 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/0529.Minesweeper/529. Minesweeper_test.go | leetcode/0529.Minesweeper/529. Minesweeper_test.go | package leetcode
import (
"fmt"
"testing"
)
type question529 struct {
para529
ans529
}
// para 是参数
// one 代表第一个参数
type para529 struct {
b [][]byte
click []int
}
// ans 是答案
// one 代表第一个答案
type ans529 struct {
one [][]byte
}
func Test_Problem529(t *testing.T) {
qs := []question529{
{
para529{[][]byte{
{'E', 'E', 'E', 'E', 'E'},
{'E', 'E', 'M', 'E', 'E'},
{'E', 'E', 'E', 'E', 'E'},
{'E', 'E', 'E', 'E', 'E'},
}, []int{3, 0}},
ans529{[][]byte{
{'B', '1', 'E', '1', 'B'},
{'B', '1', 'M', '1', 'B'},
{'B', '1', '1', '1', 'B'},
{'B', 'B', 'B', 'B', 'B'},
}},
},
{
para529{[][]byte{
{'B', '1', 'E', '1', 'B'},
{'B', '1', 'M', '1', 'B'},
{'B', '1', '1', '1', 'B'},
{'B', 'B', 'B', 'B', 'B'},
}, []int{1, 2}},
ans529{[][]byte{
{'B', '1', 'E', '1', 'B'},
{'B', '1', 'X', '1', 'B'},
{'B', '1', '1', '1', 'B'},
{'B', 'B', 'B', 'B', 'B'},
}},
},
}
fmt.Printf("------------------------Leetcode Problem 529------------------------\n")
for _, q := range qs {
_, p := q.ans529, q.para529
fmt.Printf("【input】:%v 【output】:%v\n\n\n", p, updateBoard(p.b, p.click))
}
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/0414.Third-Maximum-Number/414. Third Maximum Number.go | leetcode/0414.Third-Maximum-Number/414. Third Maximum Number.go | package leetcode
import (
"math"
)
func thirdMax(nums []int) int {
a, b, c := math.MinInt64, math.MinInt64, math.MinInt64
for _, v := range nums {
if v > a {
c = b
b = a
a = v
} else if v < a && v > b {
c = b
b = v
} else if v < b && v > c {
c = v
}
}
if c == math.MinInt64 {
return a
}
return c
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0414.Third-Maximum-Number/414. Third Maximum Number_test.go | leetcode/0414.Third-Maximum-Number/414. Third Maximum Number_test.go | package leetcode
import (
"fmt"
"testing"
)
type question414 struct {
para414
ans414
}
// para 是参数
// one 代表第一个参数
type para414 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans414 struct {
one int
}
func Test_Problem414(t *testing.T) {
qs := []question414{
{
para414{[]int{1, 1, 2}},
ans414{2},
},
{
para414{[]int{3, 2, 1}},
ans414{1},
},
{
para414{[]int{1, 2}},
ans414{2},
},
{
para414{[]int{2, 2, 3, 1}},
ans414{1},
},
}
fmt.Printf("------------------------Leetcode Problem 414------------------------\n")
for _, q := range qs {
_, p := q.ans414, q.para414
fmt.Printf("【input】:%v 【output】:%v\n", p, thirdMax(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/0896.Monotonic-Array/896. Monotonic Array.go | leetcode/0896.Monotonic-Array/896. Monotonic Array.go | package leetcode
func isMonotonic(A []int) bool {
if len(A) <= 1 {
return true
}
if A[0] < A[1] {
return inc(A[1:])
}
if A[0] > A[1] {
return dec(A[1:])
}
return inc(A[1:]) || dec(A[1:])
}
func inc(A []int) bool {
for i := 0; i < len(A)-1; i++ {
if A[i] > A[i+1] {
return false
}
}
return true
}
func dec(A []int) bool {
for i := 0; i < len(A)-1; i++ {
if A[i] < A[i+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/0896.Monotonic-Array/896. Monotonic Array_test.go | leetcode/0896.Monotonic-Array/896. Monotonic Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question896 struct {
para896
ans896
}
// para 是参数
// one 代表第一个参数
type para896 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans896 struct {
one bool
}
func Test_Problem896(t *testing.T) {
qs := []question896{
{
para896{[]int{2, 1, 3}},
ans896{false},
},
{
para896{[]int{1, 2, 2, 3}},
ans896{true},
},
{
para896{[]int{6, 5, 4, 4}},
ans896{true},
},
{
para896{[]int{1, 3, 2}},
ans896{false},
},
{
para896{[]int{1, 2, 4, 5}},
ans896{true},
},
{
para896{[]int{1, 1, 1}},
ans896{true},
},
}
fmt.Printf("------------------------Leetcode Problem 896------------------------\n")
for _, q := range qs {
_, p := q.ans896, q.para896
fmt.Printf("【input】:%v 【output】:%v\n", p, isMonotonic(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/1137.N-th-Tribonacci-Number/1137. N-th Tribonacci Number.go | leetcode/1137.N-th-Tribonacci-Number/1137. N-th Tribonacci Number.go | package leetcode
func tribonacci(n int) int {
if n < 2 {
return n
}
trib, prev, prev2 := 1, 1, 0
for n > 2 {
trib, prev, prev2 = trib+prev+prev2, trib, prev
n--
}
return trib
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1137.N-th-Tribonacci-Number/1137. N-th Tribonacci Number_test.go | leetcode/1137.N-th-Tribonacci-Number/1137. N-th Tribonacci Number_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1137 struct {
para1137
ans1137
}
// para 是参数
// one 代表第一个参数
type para1137 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans1137 struct {
one int
}
func Test_Problem1137(t *testing.T) {
qs := []question1137{
{
para1137{1},
ans1137{1},
},
{
para1137{2},
ans1137{1},
},
{
para1137{3},
ans1137{2},
},
{
para1137{4},
ans1137{4},
},
{
para1137{25},
ans1137{1389537},
},
}
fmt.Printf("------------------------Leetcode Problem 1137------------------------\n")
for _, q := range qs {
_, p := q.ans1137, q.para1137
fmt.Printf("【input】:%v 【output】:%v\n", p, tribonacci(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/0875.Koko-Eating-Bananas/875. Koko Eating Bananas.go | leetcode/0875.Koko-Eating-Bananas/875. Koko Eating Bananas.go | package leetcode
import "math"
func minEatingSpeed(piles []int, H int) int {
low, high := 1, maxInArr(piles)
for low < high {
mid := low + (high-low)>>1
if !isPossible(piles, mid, H) {
low = mid + 1
} else {
high = mid
}
}
return low
}
func isPossible(piles []int, h, H int) bool {
res := 0
for _, p := range piles {
res += int(math.Ceil(float64(p) / float64(h)))
}
return res <= H
}
func maxInArr(xs []int) int {
res := 0
for _, x := range xs {
if res < x {
res = x
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0875.Koko-Eating-Bananas/875. Koko Eating Bananas_test.go | leetcode/0875.Koko-Eating-Bananas/875. Koko Eating Bananas_test.go | package leetcode
import (
"fmt"
"testing"
)
type question875 struct {
para875
ans875
}
// para 是参数
// one 代表第一个参数
type para875 struct {
piles []int
H int
}
// ans 是答案
// one 代表第一个答案
type ans875 struct {
one int
}
func Test_Problem875(t *testing.T) {
qs := []question875{
{
para875{[]int{3, 6, 7, 11}, 8},
ans875{4},
},
{
para875{[]int{30, 11, 23, 4, 20}, 5},
ans875{30},
},
{
para875{[]int{30, 11, 23, 4, 20}, 6},
ans875{23},
},
{
para875{[]int{4, 9, 11, 17}, 8},
ans875{6},
},
}
fmt.Printf("------------------------Leetcode Problem 875------------------------\n")
for _, q := range qs {
_, p := q.ans875, q.para875
fmt.Printf("【input】:%v 【output】:%v\n", p, minEatingSpeed(p.piles, p.H))
}
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/0508.Most-Frequent-Subtree-Sum/508. Most Frequent Subtree Sum.go | leetcode/0508.Most-Frequent-Subtree-Sum/508. Most Frequent Subtree Sum.go | package leetcode
import (
"sort"
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// 解法一 维护最大频次,不用排序
func findFrequentTreeSum(root *TreeNode) []int {
memo := make(map[int]int)
collectSum(root, memo)
res := []int{}
most := 0
for key, val := range memo {
if most == val {
res = append(res, key)
} else if most < val {
most = val
res = []int{key}
}
}
return res
}
func collectSum(root *TreeNode, memo map[int]int) int {
if root == nil {
return 0
}
sum := root.Val + collectSum(root.Left, memo) + collectSum(root.Right, memo)
if v, ok := memo[sum]; ok {
memo[sum] = v + 1
} else {
memo[sum] = 1
}
return sum
}
// 解法二 求出所有和再排序
func findFrequentTreeSum1(root *TreeNode) []int {
if root == nil {
return []int{}
}
freMap, freList, reFreMap := map[int]int{}, []int{}, map[int][]int{}
findTreeSum(root, freMap)
for k, v := range freMap {
tmp := reFreMap[v]
tmp = append(tmp, k)
reFreMap[v] = tmp
}
for k := range reFreMap {
freList = append(freList, k)
}
sort.Ints(freList)
return reFreMap[freList[len(freList)-1]]
}
func findTreeSum(root *TreeNode, fre map[int]int) int {
if root == nil {
return 0
}
if root != nil && root.Left == nil && root.Right == nil {
fre[root.Val]++
return root.Val
}
val := findTreeSum(root.Left, fre) + findTreeSum(root.Right, fre) + root.Val
fre[val]++
return val
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0508.Most-Frequent-Subtree-Sum/508. Most Frequent Subtree Sum_test.go | leetcode/0508.Most-Frequent-Subtree-Sum/508. Most Frequent Subtree Sum_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question508 struct {
para508
ans508
}
// para 是参数
// one 代表第一个参数
type para508 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans508 struct {
one []int
}
func Test_Problem508(t *testing.T) {
qs := []question508{
{
para508{[]int{}},
ans508{[]int{}},
},
{
para508{[]int{1, 1}},
ans508{[]int{1, 2}},
},
{
para508{[]int{1}},
ans508{[]int{1}},
},
{
para508{[]int{5, 2, -3}},
ans508{[]int{2, -3, 4}},
},
{
para508{[]int{5, 2, -5}},
ans508{[]int{2}},
},
}
fmt.Printf("------------------------Leetcode Problem 508------------------------\n")
for _, q := range qs {
_, p := q.ans508, q.para508
fmt.Printf("【input】:%v ", p)
root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", findFrequentTreeSum(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/1074.Number-of-Submatrices-That-Sum-to-Target/1074. Number of Submatrices That Sum to Target.go | leetcode/1074.Number-of-Submatrices-That-Sum-to-Target/1074. Number of Submatrices That Sum to Target.go | package leetcode
func numSubmatrixSumTarget(matrix [][]int, target int) int {
m, n, res := len(matrix), len(matrix[0]), 0
for row := range matrix {
for col := 1; col < len(matrix[row]); col++ {
matrix[row][col] += matrix[row][col-1]
}
}
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
counterMap, sum := make(map[int]int, m), 0
counterMap[0] = 1 // 题目保证一定有解,所以这里初始化是 1
for row := 0; row < m; row++ {
if i > 0 {
sum += matrix[row][j] - matrix[row][i-1]
} else {
sum += matrix[row][j]
}
res += counterMap[sum-target]
counterMap[sum]++
}
}
}
return res
}
// 暴力解法 O(n^4)
func numSubmatrixSumTarget1(matrix [][]int, target int) int {
m, n, res, sum := len(matrix), len(matrix[0]), 0, 0
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
counterMap := map[int]int{}
counterMap[0] = 1 // 题目保证一定有解,所以这里初始化是 1
sum = 0
for row := 0; row < m; row++ {
for k := i; k <= j; k++ {
sum += matrix[row][k]
}
res += counterMap[sum-target]
counterMap[sum]++
}
}
}
return res
}
// 暴力解法超时! O(n^6)
func numSubmatrixSumTarget2(matrix [][]int, target int) int {
res := 0
for startx := 0; startx < len(matrix); startx++ {
for starty := 0; starty < len(matrix[startx]); starty++ {
for endx := startx; endx < len(matrix); endx++ {
for endy := starty; endy < len(matrix[startx]); endy++ {
if sumSubmatrix(matrix, startx, starty, endx, endy) == target {
//fmt.Printf("startx = %v, starty = %v, endx = %v, endy = %v\n", startx, starty, endx, endy)
res++
}
}
}
}
}
return res
}
func sumSubmatrix(matrix [][]int, startx, starty, endx, endy int) int {
sum := 0
for i := startx; i <= endx; i++ {
for j := starty; j <= endy; j++ {
sum += matrix[i][j]
}
}
return sum
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1074.Number-of-Submatrices-That-Sum-to-Target/1074. Number of Submatrices That Sum to Target_test.go | leetcode/1074.Number-of-Submatrices-That-Sum-to-Target/1074. Number of Submatrices That Sum to Target_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1074 struct {
para1074
ans1074
}
// para 是参数
// one 代表第一个参数
type para1074 struct {
one [][]int
t int
}
// ans 是答案
// one 代表第一个答案
type ans1074 struct {
one int
}
func Test_Problem1074(t *testing.T) {
qs := []question1074{
{
para1074{[][]int{{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}, 0},
ans1074{4},
},
{
para1074{[][]int{{1, -1}, {-1, 1}}, 0},
ans1074{5},
},
}
fmt.Printf("------------------------Leetcode Problem 1074------------------------\n")
for _, q := range qs {
_, p := q.ans1074, q.para1074
fmt.Printf("【input】:%v 【output】:%v\n", p, numSubmatrixSumTarget1(p.one, 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/1669.Merge-In-Between-Linked-Lists/1669. Merge In Between Linked Lists_test.go | leetcode/1669.Merge-In-Between-Linked-Lists/1669. Merge In Between Linked Lists_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question1669 struct {
para1669
ans1669
}
// para 是参数
// one 代表第一个参数
type para1669 struct {
one []int
a int
b int
another []int
}
// ans 是答案
// one 代表第一个答案
type ans1669 struct {
one []int
}
func Test_Problem1669(t *testing.T) {
qs := []question1669{
{
para1669{[]int{0, 1, 2, 3, 4, 5}, 3, 4, []int{1000000, 1000001, 1000002}},
ans1669{[]int{0, 1, 2, 1000000, 1000001, 1000002, 5}},
},
{
para1669{[]int{0, 1, 2, 3, 4, 5, 6}, 2, 5, []int{1000000, 1000001, 1000002, 1000003, 1000004}},
ans1669{[]int{0, 1, 1000000, 1000001, 1000002, 1000003, 1000004, 6}},
},
{
para1669{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 3, 5, []int{1000000, 1000001, 1000002, 1000003, 1000004, 1000005, 1000006}},
ans1669{[]int{0, 1, 2, 1000000, 1000001, 1000002, 1000003, 1000004, 1000005, 1000006, 6, 7, 8, 9}},
},
{
para1669{[]int{0, 1, 2}, 1, 1, []int{1000000, 1000001, 1000002, 1000003}},
ans1669{[]int{0, 1000000, 1000001, 1000002, 1000003, 2}},
},
}
fmt.Printf("------------------------Leetcode Problem 1669------------------------\n")
for _, q := range qs {
_, p := q.ans1669, q.para1669
fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(mergeInBetween(structures.Ints2List(p.one), p.a, p.b, structures.Ints2List(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/1669.Merge-In-Between-Linked-Lists/1669. Merge In Between Linked Lists.go | leetcode/1669.Merge-In-Between-Linked-Lists/1669. Merge In Between Linked Lists.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 mergeInBetween(list1 *ListNode, a int, b int, list2 *ListNode) *ListNode {
n := list1
var startRef, endRef *ListNode
for i := 0; i <= b; i++ {
if i == a-1 {
startRef = n
}
if i == b {
endRef = n
}
n = n.Next
}
startRef.Next = list2
n = list2
for n.Next != nil {
n = n.Next
}
n.Next = endRef.Next
return list1
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0307.Range-Sum-Query-Mutable/307. Range Sum Query - Mutable_test.go | leetcode/0307.Range-Sum-Query-Mutable/307. Range Sum Query - Mutable_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem307(t *testing.T) {
obj := Constructor307([]int{1, 3, 5})
fmt.Printf("obj = %v\n", obj)
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 2))
obj.Update(1, 2)
fmt.Printf("obj = %v\n", obj)
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 2))
obj = Constructor307([]int{-1})
fmt.Printf("obj = %v\n", obj)
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 0))
obj.Update(0, 1)
fmt.Printf("obj = %v\n", obj)
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 0))
obj = Constructor307([]int{7, 2, 7, 2, 0})
fmt.Printf("obj = %v\n", obj)
obj.Update(4, 6)
obj.Update(0, 2)
obj.Update(0, 9)
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(4, 4))
obj.Update(3, 8)
fmt.Printf("obj = %v\n", obj)
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 4))
obj.Update(4, 1)
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 3))
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 4))
obj.Update(0, 4)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0307.Range-Sum-Query-Mutable/307. Range Sum Query - Mutable.go | leetcode/0307.Range-Sum-Query-Mutable/307. Range Sum Query - Mutable.go | package leetcode
import "github.com/halfrost/LeetCode-Go/template"
// NumArray define
type NumArray struct {
st *template.SegmentTree
}
// Constructor307 define
func Constructor307(nums []int) NumArray {
st := template.SegmentTree{}
st.Init(nums, func(i, j int) int {
return i + j
})
return NumArray{st: &st}
}
// Update define
func (this *NumArray) Update(i int, val int) {
this.st.Update(i, val)
}
// SumRange define
func (this *NumArray) SumRange(i int, j int) int {
return this.st.Query(i, j)
}
//解法二 prefixSum,sumRange 时间复杂度 O(1)
// // NumArray define
// type NumArray307 struct {
// prefixSum []int
// data []int
// }
// // Constructor307 define
// func Constructor307(nums []int) NumArray307 {
// data := make([]int, len(nums))
// for i := 0; i < len(nums); i++ {
// data[i] = nums[i]
// }
// for i := 1; i < len(nums); i++ {
// nums[i] += nums[i-1]
// }
// return NumArray307{prefixSum: nums, data: data}
// }
// // Update define
// func (this *NumArray307) Update(i int, val int) {
// this.data[i] = val
// this.prefixSum[0] = this.data[0]
// for i := 1; i < len(this.data); i++ {
// this.prefixSum[i] = this.prefixSum[i-1] + this.data[i]
// }
// }
// // SumRange define
// func (this *NumArray307) SumRange(i int, j int) int {
// if i > 0 {
// return this.prefixSum[j] - this.prefixSum[i-1]
// }
// return this.prefixSum[j]
// }
// 解法三 树状数组
// type NumArray struct {
// bit template.BinaryIndexedTree
// data []int
// }
// // Constructor define
// func Constructor307(nums []int) NumArray {
// bit := template.BinaryIndexedTree{}
// bit.InitWithNums(nums)
// return NumArray{bit: bit, data: nums}
// }
// // Update define
// func (this *NumArray) Update(i int, val int) {
// this.bit.Add(i+1, val-this.data[i])
// this.data[i] = val
// }
// // SumRange define
// func (this *NumArray) SumRange(i int, j int) int {
// return this.bit.Query(j+1) - this.bit.Query(i)
// }
/**
* Your NumArray object will be instantiated and called as such:
* obj := Constructor(nums);
* obj.Update(i,val);
* param_2 := obj.SumRange(i,j);
*/
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | ERROR: type should be string, got "https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1287.Element-Appearing-More-Than-In-Sorted-Array/1287. Element Appearing More Than 25% In Sorted Array.go" | leetcode/1287.Element-Appearing-More-Than-In-Sorted-Array/1287. Element Appearing More Than 25% In Sorted Array.go | package leetcode
func findSpecialInteger(arr []int) int {
n := len(arr)
for i := 0; i < n-n/4; i++ {
if arr[i] == arr[i+n/4] {
return arr[i]
}
}
return -1
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | ERROR: type should be string, got "https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1287.Element-Appearing-More-Than-In-Sorted-Array/1287. Element Appearing More Than 25% In Sorted Array_test.go" | leetcode/1287.Element-Appearing-More-Than-In-Sorted-Array/1287. Element Appearing More Than 25% In Sorted Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1287 struct {
para1287
ans1287
}
// para 是参数
// one 代表第一个参数
type para1287 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans1287 struct {
one int
}
func Test_Problem1287(t *testing.T) {
qs := []question1287{
{
para1287{[]int{1, 2, 2, 6, 6, 6, 6, 7, 10}},
ans1287{6},
},
}
fmt.Printf("------------------------Leetcode Problem 1287------------------------\n")
for _, q := range qs {
_, p := q.ans1287, q.para1287
fmt.Printf("【input】:%v 【output】:%v\n", p, findSpecialInteger(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/1752.Check-if-Array-Is-Sorted-and-Rotated/1752. Check if Array Is Sorted and Rotated.go | leetcode/1752.Check-if-Array-Is-Sorted-and-Rotated/1752. Check if Array Is Sorted and Rotated.go | package leetcode
func check(nums []int) bool {
count := 0
for i := 0; i < len(nums)-1; i++ {
if nums[i] > nums[i+1] {
count++
if count > 1 || nums[0] < nums[len(nums)-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/1752.Check-if-Array-Is-Sorted-and-Rotated/1752. Check if Array Is Sorted and Rotated_test.go | leetcode/1752.Check-if-Array-Is-Sorted-and-Rotated/1752. Check if Array Is Sorted and Rotated_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1752 struct {
para1752
ans1752
}
// para 是参数
// one 代表第一个参数
type para1752 struct {
nums []int
}
// ans 是答案
// one 代表第一个答案
type ans1752 struct {
one bool
}
func Test_Problem1752(t *testing.T) {
qs := []question1752{
{
para1752{[]int{3, 4, 5, 1, 2}},
ans1752{true},
},
{
para1752{[]int{2, 1, 3, 4}},
ans1752{false},
},
{
para1752{[]int{1, 2, 3}},
ans1752{true},
},
{
para1752{[]int{1, 1, 1}},
ans1752{true},
},
{
para1752{[]int{2, 1}},
ans1752{true},
},
{
para1752{[]int{1, 3, 2, 4}},
ans1752{false},
},
}
fmt.Printf("------------------------Leetcode Problem 1752------------------------\n")
for _, q := range qs {
_, p := q.ans1752, q.para1752
fmt.Printf("【input】:%v 【output】:%v\n", p, check(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/9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_test.go | leetcode/9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1292 struct {
para1292
ans1292
}
// para 是参数
// one 代表第一个参数
type para1292 struct {
mat [][]int
threshold int
}
// ans 是答案
// one 代表第一个答案
type ans1292 struct {
one int
}
func Test_Problem1292(t *testing.T) {
qs := []question1292{
{
para1292{[][]int{{1, 1, 3, 2, 4, 3, 2}, {1, 1, 3, 2, 4, 3, 2}, {1, 1, 3, 2, 4, 3, 2}}, 4},
ans1292{2},
},
{
para1292{[][]int{{2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 2, 2, 2, 2}}, 1},
ans1292{0},
},
}
fmt.Printf("------------------------Leetcode Problem 1292------------------------\n")
for _, q := range qs {
_, p := q.ans1292, q.para1292
fmt.Printf("【input】:%v 【output】:%v\n", p, maxSideLength(p.mat, p.threshold))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold.go | leetcode/9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold.go | package leetcode
func maxSideLength(mat [][]int, threshold int) int {
return 0
// m, n, sum := len(mat), len(mat[0]), make([][]int, len(mat[0])+1, len(mat[0])+1)
// for i := range sum {
// sum[i] = make([]int, n+1, n+1)
// }
// for i := 0; i < m; i++ {
// for j := 0; j < n; j++ {
// sum[i+1][j+1] = sum[i][j+1] + sum[i+1][j] - sum[i][j] + mat[i][j]
// }
// }
// low, high := 0, min(m, n)
// for low < high {
// mid := low + (high-low)>>1
// if !inThreshold(&sum, threshold, mid) {
// high = mid + 1
// } else {
// low = mid
// }
// }
// if inThreshold(&sum, threshold, high) {
// return high
// }
// return low
}
// func min(a int, b int) int {
// if a > b {
// return b
// }
// return a
// }
// func inThreshold(sum *[][]int, threshold int, length int) bool {
// for i := 1; i < len(*sum); i++ {
// for j := 1; j < len((*sum)[0]); j++ {
// if i < length || j < length {
// continue
// }
// if (*sum)[i][j]+(*sum)[i-length][j-length]-(*sum)[i-length][j]-(*sum)[i][j-length] <= threshold {
// return true
// }
// }
// }
// return false
// }
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1744.Can-You-Eat-Your-Favorite-Candy-on-Your-Favorite-Day/1744. Can You Eat Your Favorite Candy on Your Favorite Day.go | leetcode/1744.Can-You-Eat-Your-Favorite-Candy-on-Your-Favorite-Day/1744. Can You Eat Your Favorite Candy on Your Favorite Day.go | package leetcode
func canEat(candiesCount []int, queries [][]int) []bool {
n := len(candiesCount)
prefixSum := make([]int, n)
prefixSum[0] = candiesCount[0]
for i := 1; i < n; i++ {
prefixSum[i] = prefixSum[i-1] + candiesCount[i]
}
res := make([]bool, len(queries))
for i, q := range queries {
favoriteType, favoriteDay, dailyCap := q[0], q[1], q[2]
x1 := favoriteDay + 1
y1 := (favoriteDay + 1) * dailyCap
x2 := 1
if favoriteType > 0 {
x2 = prefixSum[favoriteType-1] + 1
}
y2 := prefixSum[favoriteType]
res[i] = !(x1 > y2 || y1 < x2)
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1744.Can-You-Eat-Your-Favorite-Candy-on-Your-Favorite-Day/1744. Can You Eat Your Favorite Candy on Your Favorite Day_test.go | leetcode/1744.Can-You-Eat-Your-Favorite-Candy-on-Your-Favorite-Day/1744. Can You Eat Your Favorite Candy on Your Favorite Day_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1744 struct {
para1744
ans1744
}
// para 是参数
// one 代表第一个参数
type para1744 struct {
candiesCount []int
queries [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1744 struct {
one []bool
}
func Test_Problem1744(t *testing.T) {
qs := []question1744{
{
para1744{[]int{7, 4, 5, 3, 8}, [][]int{{0, 2, 2}, {4, 2, 4}, {2, 13, 1000000000}}},
ans1744{[]bool{true, false, true}},
},
{
para1744{[]int{5, 2, 6, 4, 1}, [][]int{{3, 1, 2}, {4, 10, 3}, {3, 10, 100}, {4, 100, 30}, {1, 3, 1}}},
ans1744{[]bool{false, true, true, false, false}},
},
}
fmt.Printf("------------------------Leetcode Problem 1744------------------------\n")
for _, q := range qs {
_, p := q.ans1744, q.para1744
fmt.Printf("【input】:%v 【output】:%v\n", p, canEat(p.candiesCount, 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/0160.Intersection-of-Two-Linked-Lists/160. Intersection of Two Linked Lists_test.go | leetcode/0160.Intersection-of-Two-Linked-Lists/160. Intersection of Two Linked Lists_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question160 struct {
para160
ans160
}
// para 是参数
// one 代表第一个参数
type para160 struct {
one []int
another []int
}
// ans 是答案
// one 代表第一个答案
type ans160 struct {
one []int
}
func Test_Problem160(t *testing.T) {
qs := []question160{
{
para160{[]int{}, []int{}},
ans160{[]int{}},
},
{
para160{[]int{3}, []int{1, 2, 3}},
ans160{[]int{3}},
},
{
para160{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}},
ans160{[]int{1, 2, 3, 4}},
},
{
para160{[]int{4, 1, 8, 4, 5}, []int{5, 0, 1, 8, 4, 5}},
ans160{[]int{8, 4, 5}},
},
{
para160{[]int{1}, []int{9, 9, 9, 9, 9}},
ans160{[]int{}},
},
{
para160{[]int{0, 9, 1, 2, 4}, []int{3, 2, 4}},
ans160{[]int{2, 4}},
},
}
fmt.Printf("------------------------Leetcode Problem 160------------------------\n")
for _, q := range qs {
_, p := q.ans160, q.para160
fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(getIntersectionNode(structures.Ints2List(p.one), structures.Ints2List(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/0160.Intersection-of-Two-Linked-Lists/160. Intersection of Two Linked Lists.go | leetcode/0160.Intersection-of-Two-Linked-Lists/160. Intersection of Two Linked Lists.go | package leetcode
import (
"fmt"
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func getIntersectionNode(headA, headB *ListNode) *ListNode {
//boundary check
if headA == nil || headB == nil {
return nil
}
a := headA
b := headB
//if a & b have different len, then we will stop the loop after second iteration
for a != b {
//for the end of first iteration, we just reset the pointer to the head of another linkedlist
if a == nil {
a = headB
} else {
a = a.Next
}
if b == nil {
b = headA
} else {
b = b.Next
}
fmt.Printf("a = %v b = %v\n", a, 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/0324.Wiggle-Sort-II/324. Wiggle Sort II_test.go | leetcode/0324.Wiggle-Sort-II/324. Wiggle Sort II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question324 struct {
para324
ans324
}
// para 是参数
// one 代表第一个参数
type para324 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans324 struct {
one []int
}
func Test_Problem324(t *testing.T) {
qs := []question324{
{
para324{[]int{}},
ans324{[]int{}},
},
{
para324{[]int{1}},
ans324{[]int{1}},
},
{
para324{[]int{1, 5, 1, 1, 6, 4}},
ans324{[]int{1, 4, 1, 5, 1, 6}},
},
{
para324{[]int{1, 3, 2, 2, 3, 1}},
ans324{[]int{2, 3, 1, 3, 1, 2}},
},
{
para324{[]int{1, 1, 2, 1, 2, 2, 1}},
ans324{[]int{1, 2, 1, 2, 1, 2, 1}},
},
}
fmt.Printf("------------------------Leetcode Problem 324------------------------\n")
for _, q := range qs {
_, p := q.ans324, q.para324
fmt.Printf("【input】:%v ", p)
wiggleSort(p.one)
fmt.Printf("【output】:%v \n", p)
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0324.Wiggle-Sort-II/324. Wiggle Sort II.go | leetcode/0324.Wiggle-Sort-II/324. Wiggle Sort II.go | package leetcode
import (
"sort"
)
// 解法一
func wiggleSort(nums []int) {
if len(nums) < 2 {
return
}
median := findKthLargest324(nums, (len(nums)+1)/2)
n, i, left, right := len(nums), 0, 0, len(nums)-1
for i <= right {
if nums[indexMap(i, n)] > median {
nums[indexMap(left, n)], nums[indexMap(i, n)] = nums[indexMap(i, n)], nums[indexMap(left, n)]
left++
i++
} else if nums[indexMap(i, n)] < median {
nums[indexMap(right, n)], nums[indexMap(i, n)] = nums[indexMap(i, n)], nums[indexMap(right, n)]
right--
} else {
i++
}
}
}
func indexMap(index, n int) int {
return (1 + 2*index) % (n | 1)
}
func findKthLargest324(nums []int, k int) int {
if len(nums) == 0 {
return 0
}
return selection324(nums, 0, len(nums)-1, len(nums)-k)
}
func selection324(arr []int, l, r, k int) int {
if l == r {
return arr[l]
}
p := partition324(arr, l, r)
if k == p {
return arr[p]
} else if k < p {
return selection324(arr, l, p-1, k)
} else {
return selection324(arr, p+1, r, k)
}
}
func partition324(a []int, lo, hi int) int {
pivot := a[hi]
i := lo - 1
for j := lo; j < hi; j++ {
if a[j] < pivot {
i++
a[j], a[i] = a[i], a[j]
}
}
a[i+1], a[hi] = a[hi], a[i+1]
return i + 1
}
// 解法二
func wiggleSort1(nums []int) {
if len(nums) < 2 {
return
}
array := make([]int, len(nums))
copy(array, nums)
sort.Ints(array)
n := len(nums)
left := (n+1)/2 - 1 // median index
right := n - 1 // largest value index
for i := 0; i < len(nums); i++ {
// copy large values on odd indexes
if i%2 == 1 {
nums[i] = array[right]
right--
} else { // copy values decremeting from median on even indexes
nums[i] = array[left]
left--
}
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1034.Coloring-A-Border/1034.Coloring A Border.go | leetcode/1034.Coloring-A-Border/1034.Coloring A Border.go | package leetcode
type point struct {
x int
y int
}
type gridInfo struct {
m int
n int
grid [][]int
originalColor int
}
func colorBorder(grid [][]int, row, col, color int) [][]int {
m, n := len(grid), len(grid[0])
dirs := []point{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}
vis := make([][]bool, m)
for i := range vis {
vis[i] = make([]bool, n)
}
var borders []point
gInfo := gridInfo{
m: m,
n: n,
grid: grid,
originalColor: grid[row][col],
}
dfs(row, col, gInfo, dirs, vis, &borders)
for _, p := range borders {
grid[p.x][p.y] = color
}
return grid
}
func dfs(x, y int, gInfo gridInfo, dirs []point, vis [][]bool, borders *[]point) {
vis[x][y] = true
isBorder := false
for _, dir := range dirs {
nx, ny := x+dir.x, y+dir.y
if !(0 <= nx && nx < gInfo.m && 0 <= ny && ny < gInfo.n && gInfo.grid[nx][ny] == gInfo.originalColor) {
isBorder = true
} else if !vis[nx][ny] {
dfs(nx, ny, gInfo, dirs, vis, borders)
}
}
if isBorder {
*borders = append(*borders, point{x, y})
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1034.Coloring-A-Border/1034.Coloring A Border_test.go | leetcode/1034.Coloring-A-Border/1034.Coloring A Border_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1034 struct {
para1034
ans1034
}
// para 是参数
type para1034 struct {
grid [][]int
row int
col int
color int
}
// ans 是答案
type ans1034 struct {
ans [][]int
}
func Test_Problem1034(t *testing.T) {
qs := []question1034{
{
para1034{[][]int{{1, 1}, {1, 2}}, 0, 0, 3},
ans1034{[][]int{{3, 3}, {3, 2}}},
},
{
para1034{[][]int{{1, 2, 2}, {2, 3, 2}}, 0, 1, 3},
ans1034{[][]int{{1, 3, 3}, {2, 3, 3}}},
},
{
para1034{[][]int{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, 1, 1, 2},
ans1034{[][]int{{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}},
},
}
fmt.Printf("------------------------Leetcode Problem 1034------------------------\n")
for _, q := range qs {
_, p := q.ans1034, q.para1034
fmt.Printf("【input】:%v 【output】:%v\n", p, colorBorder(p.grid, p.row, p.col, p.color))
}
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/0048.Rotate-Image/48. Rotate Image.go | leetcode/0048.Rotate-Image/48. Rotate Image.go | package leetcode
// 解法一
func rotate(matrix [][]int) {
length := len(matrix)
// rotate by diagonal 对角线变换
for i := 0; i < length; i++ {
for j := i + 1; j < length; j++ {
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
}
}
// rotate by vertical centerline 竖直轴对称翻转
for i := 0; i < length; i++ {
for j := 0; j < length/2; j++ {
matrix[i][j], matrix[i][length-j-1] = matrix[i][length-j-1], matrix[i][j]
}
}
}
// 解法二
func rotate1(matrix [][]int) {
n := len(matrix)
if n == 1 {
return
}
/* rotate clock-wise = 1. transpose matrix => 2. reverse(matrix[i])
1 2 3 4 1 5 9 13 13 9 5 1
5 6 7 8 => 2 6 10 14 => 14 10 6 2
9 10 11 12 3 7 11 15 15 11 7 3
13 14 15 16 4 8 12 16 16 12 8 4
*/
for i := 0; i < n; i++ {
// transpose, i=rows, j=columns
// j = i+1, coz diagonal elements didn't change in a square matrix
for j := i + 1; j < n; j++ {
swap(matrix, i, j)
}
// reverse each row of the image
matrix[i] = reverse(matrix[i])
}
}
// swap changes original slice's i,j position
func swap(nums [][]int, i, j int) {
nums[i][j], nums[j][i] = nums[j][i], nums[i][j]
}
// reverses a row of image, matrix[i]
func reverse(nums []int) []int {
var lp, rp = 0, len(nums) - 1
for lp < rp {
nums[lp], nums[rp] = nums[rp], nums[lp]
lp++
rp--
}
return nums
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0048.Rotate-Image/48. Rotate Image_test.go | leetcode/0048.Rotate-Image/48. Rotate Image_test.go | package leetcode
import (
"fmt"
"testing"
)
type question48 struct {
para48
ans48
}
// para 是参数
// one 代表第一个参数
type para48 struct {
s [][]int
}
// ans 是答案
// one 代表第一个答案
type ans48 struct {
s [][]int
}
func Test_Problem48(t *testing.T) {
qs := []question48{
{
para48{[][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}},
ans48{[][]int{{7, 4, 1}, {8, 5, 2}, {9, 6, 3}}},
},
{
para48{[][]int{{5, 1, 9, 11}, {2, 4, 8, 10}, {13, 3, 6, 7}, {15, 14, 12, 16}}},
ans48{[][]int{{15, 13, 2, 5}, {14, 3, 4, 1}, {12, 6, 8, 9}, {16, 7, 10, 11}}},
},
}
fmt.Printf("------------------------Leetcode Problem 48------------------------\n")
for _, q := range qs {
_, p := q.ans48, q.para48
fmt.Printf("【input】:%v \n", p)
rotate(p.s)
fmt.Printf("【output】:%v\n", p)
fmt.Printf("\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/1089.Duplicate-Zeros/1089. Duplicate Zeros.go | leetcode/1089.Duplicate-Zeros/1089. Duplicate Zeros.go | package leetcode
func duplicateZeros(arr []int) {
for i := 0; i < len(arr); i++ {
if arr[i] == 0 && i+1 < len(arr) {
arr = append(arr[:i+1], arr[i:len(arr)-1]...)
i++
}
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1089.Duplicate-Zeros/1089. Duplicate Zeros_test.go | leetcode/1089.Duplicate-Zeros/1089. Duplicate Zeros_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1089 struct {
para1089
ans1089
}
// para 是参数
// one 代表第一个参数
type para1089 struct {
arr []int
}
// ans 是答案
// one 代表第一个答案
type ans1089 struct {
}
func Test_Problem1089(t *testing.T) {
qs := []question1089{
{
para1089{[]int{1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0}},
ans1089{},
},
{
para1089{[]int{1, 0, 2, 3, 0, 4, 5, 0}},
ans1089{},
},
{
para1089{[]int{1, 2, 3}},
ans1089{},
},
}
fmt.Printf("------------------------Leetcode Problem 1089------------------------\n")
for _, q := range qs {
_, p := q.ans1089, q.para1089
fmt.Printf("【input】:%v ", p)
duplicateZeros(p.arr)
fmt.Printf("【output】:%v \n", 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/1002.Find-Common-Characters/1002. Find Common Characters_test.go | leetcode/1002.Find-Common-Characters/1002. Find Common Characters_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1002 struct {
para1002
ans1002
}
// para 是参数
// one 代表第一个参数
type para1002 struct {
one []string
}
// ans 是答案
// one 代表第一个答案
type ans1002 struct {
one []string
}
func Test_Problem1002(t *testing.T) {
qs := []question1002{
{
para1002{[]string{"bella", "label", "roller"}},
ans1002{[]string{"e", "l", "l"}},
},
{
para1002{[]string{"cool", "lock", "cook"}},
ans1002{[]string{"c", "o"}},
},
}
fmt.Printf("------------------------Leetcode Problem 1002------------------------\n")
for _, q := range qs {
_, p := q.ans1002, q.para1002
fmt.Printf("【input】:%v 【output】:%v\n", p, commonChars(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/1002.Find-Common-Characters/1002. Find Common Characters.go | leetcode/1002.Find-Common-Characters/1002. Find Common Characters.go | package leetcode
import "math"
func commonChars(A []string) []string {
cnt := [26]int{}
for i := range cnt {
cnt[i] = math.MaxUint16
}
cntInWord := [26]int{}
for _, word := range A {
for _, char := range []byte(word) { // compiler trick - here we will not allocate new memory
cntInWord[char-'a']++
}
for i := 0; i < 26; i++ {
// 缩小频次,使得统计的公共频次更加准确
if cntInWord[i] < cnt[i] {
cnt[i] = cntInWord[i]
}
}
// 重置状态
for i := range cntInWord {
cntInWord[i] = 0
}
}
result := make([]string, 0)
for i := 0; i < 26; i++ {
for j := 0; j < cnt[i]; j++ {
result = append(result, string(rune(i+'a')))
}
}
return result
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0007.Reverse-Integer/7. Reverse Integer_test.go | leetcode/0007.Reverse-Integer/7. Reverse Integer_test.go | package leetcode
import (
"fmt"
"testing"
)
type question7 struct {
para7
ans7
}
// para 是参数
// one 代表第一个参数
type para7 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans7 struct {
one int
}
func Test_Problem7(t *testing.T) {
qs := []question7{
{
para7{321},
ans7{123},
},
{
para7{-123},
ans7{-321},
},
{
para7{120},
ans7{21},
},
{
para7{1534236469},
ans7{0},
},
}
fmt.Printf("------------------------Leetcode Problem 7------------------------\n")
for _, q := range qs {
_, p := q.ans7, q.para7
fmt.Printf("【input】:%v 【output】:%v\n", p.one, reverse7(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/0007.Reverse-Integer/7. Reverse Integer.go | leetcode/0007.Reverse-Integer/7. Reverse Integer.go | package leetcode
func reverse7(x int) int {
tmp := 0
for x != 0 {
tmp = tmp*10 + x%10
x = x / 10
}
if tmp > 1<<31-1 || tmp < -(1<<31) {
return 0
}
return tmp
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0927.Three-Equal-Parts/927. Three Equal Parts_test.go | leetcode/0927.Three-Equal-Parts/927. Three Equal Parts_test.go | package leetcode
import (
"fmt"
"testing"
)
type question927 struct {
para927
ans927
}
// para 是参数
// one 代表第一个参数
type para927 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans927 struct {
one []int
}
func Test_Problem927(t *testing.T) {
qs := []question927{
{
para927{[]int{1, 0, 1, 0, 1}},
ans927{[]int{0, 3}},
},
{
para927{[]int{1, 1, 0, 1, 1}},
ans927{[]int{-1, -1}},
},
}
fmt.Printf("------------------------Leetcode Problem 927------------------------\n")
for _, q := range qs {
_, p := q.ans927, q.para927
fmt.Printf("【input】:%v 【output】:%v\n", p, threeEqualParts(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/0927.Three-Equal-Parts/927. Three Equal Parts.go | leetcode/0927.Three-Equal-Parts/927. Three Equal Parts.go | package leetcode
func threeEqualParts(A []int) []int {
n, ones, i, count := len(A), 0, 0, 0
for _, a := range A {
ones += a
}
if ones == 0 {
return []int{0, n - 1}
}
if ones%3 != 0 {
return []int{-1, -1}
}
k := ones / 3
for i < n {
if A[i] == 1 {
break
}
i++
}
start, j := i, i
for j < n {
count += A[j]
if count == k+1 {
break
}
j++
}
mid := j
j, count = 0, 0
for j < n {
count += A[j]
if count == 2*k+1 {
break
}
j++
}
end := j
for end < n && A[start] == A[mid] && A[mid] == A[end] {
start++
mid++
end++
}
if end == n {
return []int{start - 1, mid}
}
return []int{-1, -1}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1688.Count-of-Matches-in-Tournament/1688. Count of Matches in Tournament.go | leetcode/1688.Count-of-Matches-in-Tournament/1688. Count of Matches in Tournament.go | package leetcode
// 解法一
func numberOfMatches(n int) int {
return n - 1
}
// 解法二 模拟
func numberOfMatches1(n int) int {
sum := 0
for n != 1 {
if n&1 == 0 {
sum += n / 2
n = n / 2
} else {
sum += (n - 1) / 2
n = (n-1)/2 + 1
}
}
return sum
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1688.Count-of-Matches-in-Tournament/1688. Count of Matches in Tournament_test.go | leetcode/1688.Count-of-Matches-in-Tournament/1688. Count of Matches in Tournament_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1688 struct {
para1688
ans1688
}
// para 是参数
// one 代表第一个参数
type para1688 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans1688 struct {
one int
}
func Test_Problem1688(t *testing.T) {
qs := []question1688{
{
para1688{7},
ans1688{6},
},
{
para1688{14},
ans1688{13},
},
}
fmt.Printf("------------------------Leetcode Problem 1688------------------------\n")
for _, q := range qs {
_, p := q.ans1688, q.para1688
fmt.Printf("【input】:%v 【output】:%v\n", p, numberOfMatches(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/0495.Teemo-Attacking/495.Teemo Attacking.go | leetcode/0495.Teemo-Attacking/495.Teemo Attacking.go | package leetcode
func findPoisonedDuration(timeSeries []int, duration int) int {
var ans int
for i := 1; i < len(timeSeries); i++ {
t := timeSeries[i-1]
end := t + duration - 1
if end < timeSeries[i] {
ans += duration
} else {
ans += timeSeries[i] - t
}
}
ans += duration
return ans
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0495.Teemo-Attacking/495.Teemo Attacking_test.go | leetcode/0495.Teemo-Attacking/495.Teemo Attacking_test.go | package leetcode
import (
"fmt"
"testing"
)
type question495 struct {
para495
ans495
}
// para 是参数
type para495 struct {
timeSeries []int
duration int
}
// ans 是答案
type ans495 struct {
ans int
}
func Test_Problem495(t *testing.T) {
qs := []question495{
{
para495{[]int{1, 4}, 2},
ans495{4},
},
{
para495{[]int{1, 2}, 2},
ans495{3},
},
}
fmt.Printf("------------------------Leetcode Problem 495------------------------\n")
for _, q := range qs {
_, p := q.ans495, q.para495
fmt.Printf("【input】:%v 【output】:%v\n", p, findPoisonedDuration(p.timeSeries, p.duration))
}
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/0121.Best-Time-to-Buy-and-Sell-Stock/121. Best Time to Buy and Sell Stock.go | leetcode/0121.Best-Time-to-Buy-and-Sell-Stock/121. Best Time to Buy and Sell Stock.go | package leetcode
// 解法一 模拟 DP
func maxProfit(prices []int) int {
if len(prices) < 1 {
return 0
}
min, maxProfit := prices[0], 0
for i := 1; i < len(prices); i++ {
if prices[i]-min > maxProfit {
maxProfit = prices[i] - min
}
if prices[i] < min {
min = prices[i]
}
}
return maxProfit
}
// 解法二 单调栈
func maxProfit1(prices []int) int {
if len(prices) == 0 {
return 0
}
stack, res := []int{prices[0]}, 0
for i := 1; i < len(prices); i++ {
if prices[i] > stack[len(stack)-1] {
stack = append(stack, prices[i])
} else {
index := len(stack) - 1
for ; index >= 0; index-- {
if stack[index] < prices[i] {
break
}
}
stack = stack[:index+1]
stack = append(stack, prices[i])
}
res = max(res, stack[len(stack)-1]-stack[0])
}
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/0121.Best-Time-to-Buy-and-Sell-Stock/121. Best Time to Buy and Sell Stock_test.go | leetcode/0121.Best-Time-to-Buy-and-Sell-Stock/121. Best Time to Buy and Sell Stock_test.go | package leetcode
import (
"fmt"
"testing"
)
type question121 struct {
para121
ans121
}
// para 是参数
// one 代表第一个参数
type para121 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans121 struct {
one int
}
func Test_Problem121(t *testing.T) {
qs := []question121{
{
para121{[]int{}},
ans121{0},
},
{
para121{[]int{7, 1, 5, 3, 6, 4}},
ans121{5},
},
{
para121{[]int{7, 6, 4, 3, 1}},
ans121{0},
},
{
para121{[]int{1, 3, 2, 8, 4, 9}},
ans121{8},
},
}
fmt.Printf("------------------------Leetcode Problem 121------------------------\n")
for _, q := range qs {
_, p := q.ans121, q.para121
fmt.Printf("【input】:%v 【output】:%v\n", p, maxProfit(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/0026.Remove-Duplicates-from-Sorted-Array/26. Remove Duplicates from Sorted Array.go | leetcode/0026.Remove-Duplicates-from-Sorted-Array/26. Remove Duplicates from Sorted Array.go | package leetcode
// 解法一
func removeDuplicates(nums []int) int {
if len(nums) == 0 {
return 0
}
last, finder := 0, 0
for last < len(nums)-1 {
for nums[finder] == nums[last] {
finder++
if finder == len(nums) {
return last + 1
}
}
nums[last+1] = nums[finder]
last++
}
return last + 1
}
// 解法二
func removeDuplicates1(nums []int) int {
if len(nums) == 0 {
return 0
}
length := len(nums)
lastNum := nums[length-1]
i := 0
for i = 0; i < length-1; i++ {
if nums[i] == lastNum {
break
}
if nums[i+1] == nums[i] {
removeElement1(nums, i+1, nums[i])
// fmt.Printf("此时 num = %v length = %v\n", nums, length)
}
}
return i + 1
}
func removeElement1(nums []int, start, val int) int {
if len(nums) == 0 {
return 0
}
j := start
for i := start; i < len(nums); i++ {
if nums[i] != val {
if i != j {
nums[i], nums[j] = nums[j], nums[i]
j++
} else {
j++
}
}
}
return j
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0026.Remove-Duplicates-from-Sorted-Array/26. Remove Duplicates from Sorted Array_test.go | leetcode/0026.Remove-Duplicates-from-Sorted-Array/26. Remove Duplicates from Sorted Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question26 struct {
para26
ans26
}
// para 是参数
// one 代表第一个参数
type para26 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans26 struct {
one int
}
func Test_Problem26(t *testing.T) {
qs := []question26{
{
para26{[]int{1, 1, 2}},
ans26{2},
},
{
para26{[]int{0, 0, 1, 1, 1, 1, 2, 3, 4, 4}},
ans26{5},
},
{
para26{[]int{0, 0, 0, 0, 0}},
ans26{1},
},
{
para26{[]int{1}},
ans26{1},
},
}
fmt.Printf("------------------------Leetcode Problem 26------------------------\n")
for _, q := range qs {
_, p := q.ans26, q.para26
fmt.Printf("【input】:%v 【output】:%v\n", p.one, removeDuplicates(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/0538.Convert-BST-to-Greater-Tree/538. Convert BST to Greater Tree.go | leetcode/0538.Convert-BST-to-Greater-Tree/538. Convert BST to Greater 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 convertBST(root *TreeNode) *TreeNode {
if root == nil {
return root
}
sum := 0
dfs538(root, &sum)
return root
}
func dfs538(root *TreeNode, sum *int) {
if root == nil {
return
}
dfs538(root.Right, sum)
root.Val += *sum
*sum = root.Val
dfs538(root.Left, sum)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0538.Convert-BST-to-Greater-Tree/538. Convert BST to Greater Tree_test.go | leetcode/0538.Convert-BST-to-Greater-Tree/538. Convert BST to Greater Tree_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question538 struct {
para538
ans538
}
// para 是参数
// one 代表第一个参数
type para538 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans538 struct {
one []int
}
func Test_Problem538(t *testing.T) {
qs := []question538{
{
para538{[]int{3, 1, structures.NULL, 0, structures.NULL, -4, structures.NULL, structures.NULL, -2}},
ans538{[]int{3, 4, structures.NULL, 4, structures.NULL, -2, structures.NULL, structures.NULL, 2}},
},
{
para538{[]int{2, 1}},
ans538{[]int{2, 3}},
},
{
para538{[]int{}},
ans538{[]int{}},
},
{
para538{[]int{4, 1, 6, 0, 2, 5, 7, structures.NULL, structures.NULL, structures.NULL, 3, structures.NULL, structures.NULL, structures.NULL, 8}},
ans538{[]int{30, 36, 21, 36, 35, 26, 15, structures.NULL, structures.NULL, structures.NULL, 33, structures.NULL, structures.NULL, structures.NULL, 8}},
},
{
para538{[]int{0, structures.NULL, 1}},
ans538{[]int{1, structures.NULL, 1}},
},
{
para538{[]int{1, 0, 2}},
ans538{[]int{3, 3, 2}},
},
{
para538{[]int{3, 2, 4, 1}},
ans538{[]int{7, 9, 4, 10}},
},
}
fmt.Printf("------------------------Leetcode Problem 538------------------------\n")
for _, q := range qs {
_, p := q.ans538, q.para538
fmt.Printf("【input】:%v ", p)
root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", structures.Tree2ints(convertBST(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/0638.Shopping-Offers/638. Shopping Offers.go | leetcode/0638.Shopping-Offers/638. Shopping Offers.go | package leetcode
func shoppingOffers(price []int, special [][]int, needs []int) int {
res := -1
dfsShoppingOffers(price, special, needs, 0, &res)
return res
}
func dfsShoppingOffers(price []int, special [][]int, needs []int, pay int, res *int) {
noNeeds := true
// 剪枝
for _, need := range needs {
if need < 0 {
return
}
if need != 0 {
noNeeds = false
}
}
if len(special) == 0 || noNeeds {
for i, p := range price {
pay += (p * needs[i])
}
if pay < *res || *res == -1 {
*res = pay
}
return
}
newNeeds := make([]int, len(needs))
copy(newNeeds, needs)
for i, n := range newNeeds {
newNeeds[i] = n - special[0][i]
}
dfsShoppingOffers(price, special, newNeeds, pay+special[0][len(price)], res)
dfsShoppingOffers(price, special[1:], newNeeds, pay+special[0][len(price)], res)
dfsShoppingOffers(price, special[1:], needs, pay, res)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0638.Shopping-Offers/638. Shopping Offers_test.go | leetcode/0638.Shopping-Offers/638. Shopping Offers_test.go | package leetcode
import (
"fmt"
"testing"
)
type question638 struct {
para638
ans638
}
// para 是参数
// one 代表第一个参数
type para638 struct {
price []int
special [][]int
needs []int
}
// ans 是答案
// one 代表第一个答案
type ans638 struct {
one int
}
func Test_Problem638(t *testing.T) {
qs := []question638{
{
para638{[]int{2, 5}, [][]int{{3, 0, 5}, {1, 2, 10}}, []int{3, 2}},
ans638{14},
},
{
para638{[]int{2, 3, 4}, [][]int{{1, 1, 0, 4}, {2, 2, 1, 9}}, []int{1, 2, 1}},
ans638{11},
},
}
fmt.Printf("------------------------Leetcode Problem 638------------------------\n")
for _, q := range qs {
_, p := q.ans638, q.para638
fmt.Printf("【input】:%v 【output】:%v\n", p, shoppingOffers(p.price, p.special, p.needs))
}
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/0518.Coin-Change-II/518. Coin Change II_test.go | leetcode/0518.Coin-Change-II/518. Coin Change II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question518 struct {
para518
ans518
}
// para 是参数
// one 代表第一个参数
type para518 struct {
amount int
coins []int
}
// ans 是答案
// one 代表第一个答案
type ans518 struct {
one int
}
func Test_Problem518(t *testing.T) {
qs := []question518{
{
para518{5, []int{1, 2, 5}},
ans518{4},
},
{
para518{3, []int{2}},
ans518{0},
},
{
para518{10, []int{10}},
ans518{1},
},
}
fmt.Printf("------------------------Leetcode Problem 518------------------------\n")
for _, q := range qs {
_, p := q.ans518, q.para518
fmt.Printf("【input】:%v 【output】:%v\n", p, change(p.amount, p.coins))
}
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/0518.Coin-Change-II/518. Coin Change II.go | leetcode/0518.Coin-Change-II/518. Coin Change II.go | package leetcode
func change(amount int, coins []int) int {
dp := make([]int, amount+1)
dp[0] = 1
for _, coin := range coins {
for i := coin; i <= amount; i++ {
dp[i] += dp[i-coin]
}
}
return dp[amount]
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0122.Best-Time-to-Buy-and-Sell-Stock-II/122. Best Time to Buy and Sell Stock II_test.go | leetcode/0122.Best-Time-to-Buy-and-Sell-Stock-II/122. Best Time to Buy and Sell Stock II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question122 struct {
para122
ans122
}
// para 是参数
// one 代表第一个参数
type para122 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans122 struct {
one int
}
func Test_Problem122(t *testing.T) {
qs := []question122{
{
para122{[]int{}},
ans122{0},
},
{
para122{[]int{7, 1, 5, 3, 6, 4}},
ans122{7},
},
{
para122{[]int{7, 6, 4, 3, 1}},
ans122{0},
},
{
para122{[]int{1, 2, 3, 4, 5}},
ans122{4},
},
{
para122{[]int{1, 2, 10, 11, 12, 15}},
ans122{14},
},
}
fmt.Printf("------------------------Leetcode Problem 122------------------------\n")
for _, q := range qs {
_, p := q.ans122, q.para122
fmt.Printf("【input】:%v 【output】:%v\n", p, maxProfit122(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/0122.Best-Time-to-Buy-and-Sell-Stock-II/122. Best Time to Buy and Sell Stock II.go | leetcode/0122.Best-Time-to-Buy-and-Sell-Stock-II/122. Best Time to Buy and Sell Stock II.go | package leetcode
func maxProfit122(prices []int) int {
profit := 0
for i := 0; i < len(prices)-1; i++ {
if prices[i+1] > prices[i] {
profit += prices[i+1] - prices[i]
}
}
return profit
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1079.Letter-Tile-Possibilities/1079. Letter Tile Possibilities_test.go | leetcode/1079.Letter-Tile-Possibilities/1079. Letter Tile Possibilities_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1079 struct {
para1079
ans1079
}
// para 是参数
// one 代表第一个参数
type para1079 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans1079 struct {
one int
}
func Test_Problem1079(t *testing.T) {
qs := []question1079{
{
para1079{"AAB"},
ans1079{8},
},
{
para1079{"AAABBC"},
ans1079{188},
},
}
fmt.Printf("------------------------Leetcode Problem 1079------------------------\n")
for _, q := range qs {
_, p := q.ans1079, q.para1079
fmt.Printf("【input】:%v 【output】:%v\n", p, numTilePossibilities(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/1079.Letter-Tile-Possibilities/1079. Letter Tile Possibilities.go | leetcode/1079.Letter-Tile-Possibilities/1079. Letter Tile Possibilities.go | package leetcode
// 解法一 DFS
func numTilePossibilities(tiles string) int {
m := make(map[byte]int)
for i := range tiles {
m[tiles[i]]++
}
arr := make([]int, 0)
for _, v := range m {
arr = append(arr, v)
}
return numTileDFS(arr)
}
func numTileDFS(arr []int) (r int) {
for i := 0; i < len(arr); i++ {
if arr[i] == 0 {
continue
}
r++
arr[i]--
r += numTileDFS(arr)
arr[i]++
}
return
}
// 解法二 DFS 暴力解法
func numTilePossibilities1(tiles string) int {
res, tmp, tMap, used := 0, []byte{}, make(map[string]string, 0), make([]bool, len(tiles))
findTile([]byte(tiles), tmp, &used, 0, &res, tMap)
return res
}
func findTile(tiles, tmp []byte, used *[]bool, index int, res *int, tMap map[string]string) {
flag := true
for _, v := range *used {
if v == false {
flag = false
break
}
}
if flag {
return
}
for i := 0; i < len(tiles); i++ {
if (*used)[i] == true {
continue
}
tmp = append(tmp, tiles[i])
(*used)[i] = true
if _, ok := tMap[string(tmp)]; !ok {
//fmt.Printf("i = %v tiles = %v 找到了结果 = %v\n", i, string(tiles), string(tmp))
*res++
}
tMap[string(tmp)] = string(tmp)
findTile([]byte(tiles), tmp, used, i+1, res, tMap)
tmp = tmp[:len(tmp)-1]
(*used)[i] = false
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1700.Number-of-Students-Unable-to-Eat-Lunch/1700. Number of Students Unable to Eat Lunch.go | leetcode/1700.Number-of-Students-Unable-to-Eat-Lunch/1700. Number of Students Unable to Eat Lunch.go | package leetcode
func countStudents(students []int, sandwiches []int) int {
tmp, n, i := [2]int{}, len(students), 0
for _, v := range students {
tmp[v]++
}
for i < n && tmp[sandwiches[i]] > 0 {
tmp[sandwiches[i]]--
i++
}
return n - i
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1700.Number-of-Students-Unable-to-Eat-Lunch/1700. Number of Students Unable to Eat Lunch_test.go | leetcode/1700.Number-of-Students-Unable-to-Eat-Lunch/1700. Number of Students Unable to Eat Lunch_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1700 struct {
para1700
ans1700
}
// para 是参数
// one 代表第一个参数
type para1700 struct {
students []int
sandwiches []int
}
// ans 是答案
// one 代表第一个答案
type ans1700 struct {
one int
}
func Test_Problem1700(t *testing.T) {
qs := []question1700{
{
para1700{[]int{1, 1, 0, 0}, []int{0, 1, 0, 1}},
ans1700{0},
},
{
para1700{[]int{1, 1, 1, 0, 0, 1}, []int{1, 0, 0, 0, 1, 1}},
ans1700{3},
},
}
fmt.Printf("------------------------Leetcode Problem 1700------------------------\n")
for _, q := range qs {
_, p := q.ans1700, q.para1700
fmt.Printf("【input】:%v 【output】:%v\n", p, countStudents(p.students, p.sandwiches))
}
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/1281.Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281. Subtract the Product and Sum of Digits of an Integer.go | leetcode/1281.Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281. Subtract the Product and Sum of Digits of an Integer.go | package leetcode
func subtractProductAndSum(n int) int {
sum, product := 0, 1
for ; n > 0; n /= 10 {
sum += n % 10
product *= n % 10
}
return product - sum
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1281.Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281. Subtract the Product and Sum of Digits of an Integer_test.go | leetcode/1281.Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281. Subtract the Product and Sum of Digits of an Integer_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1281 struct {
para1281
ans1281
}
// para 是参数
// one 代表第一个参数
type para1281 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans1281 struct {
one int
}
func Test_Problem1281(t *testing.T) {
qs := []question1281{
{
para1281{234},
ans1281{15},
},
{
para1281{4421},
ans1281{21},
},
}
fmt.Printf("------------------------Leetcode Problem 1281------------------------\n")
for _, q := range qs {
_, p := q.ans1281, q.para1281
fmt.Printf("【input】:%v 【output】:%v\n", p, subtractProductAndSum(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/0239.Sliding-Window-Maximum/239. Sliding Window Maximum.go | leetcode/0239.Sliding-Window-Maximum/239. Sliding Window Maximum.go | package leetcode
// 解法一 暴力解法 O(nk)
func maxSlidingWindow1(a []int, k int) []int {
res := make([]int, 0, k)
n := len(a)
if n == 0 {
return []int{}
}
for i := 0; i <= n-k; i++ {
max := a[i]
for j := 1; j < k; j++ {
if max < a[i+j] {
max = a[i+j]
}
}
res = append(res, max)
}
return res
}
// 解法二 双端队列 Deque
func maxSlidingWindow(nums []int, k int) []int {
if len(nums) == 0 || len(nums) < k {
return make([]int, 0)
}
window := make([]int, 0, k) // store the index of nums
result := make([]int, 0, len(nums)-k+1)
for i, v := range nums { // if the left-most index is out of window, remove it
if i >= k && window[0] <= i-k {
window = window[1:]
}
for len(window) > 0 && nums[window[len(window)-1]] < v { // maintain window
window = window[0 : len(window)-1]
}
window = append(window, i) // store the index of nums
if i >= k-1 {
result = append(result, nums[window[0]]) // the left-most is the index of max value in nums
}
}
return result
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0239.Sliding-Window-Maximum/239. Sliding Window Maximum_test.go | leetcode/0239.Sliding-Window-Maximum/239. Sliding Window Maximum_test.go | package leetcode
import (
"fmt"
"testing"
)
type question239 struct {
para239
ans239
}
// para 是参数
// one 代表第一个参数
type para239 struct {
one []int
k int
}
// ans 是答案
// one 代表第一个答案
type ans239 struct {
one []int
}
func Test_Problem239(t *testing.T) {
qs := []question239{
{
para239{[]int{1, 3, -1, -3, 5, 3, 6, 7}, 3},
ans239{[]int{3, 3, 5, 5, 6, 7}},
},
}
fmt.Printf("------------------------Leetcode Problem 239------------------------\n")
for _, q := range qs {
_, p := q.ans239, q.para239
fmt.Printf("【input】:%v 【output】:%v\n", p, maxSlidingWindow(p.one, p.k))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1021.Remove-Outermost-Parentheses/1021. Remove Outermost Parentheses.go | leetcode/1021.Remove-Outermost-Parentheses/1021. Remove Outermost Parentheses.go | package leetcode
// 解法一
func removeOuterParentheses(S string) string {
now, current, ans := 0, "", ""
for _, char := range S {
if string(char) == "(" {
now++
} else if string(char) == ")" {
now--
}
current += string(char)
if now == 0 {
ans += current[1 : len(current)-1]
current = ""
}
}
return ans
}
// 解法二
func removeOuterParentheses1(S string) string {
stack, res, counter := []byte{}, "", 0
for i := 0; i < len(S); i++ {
if counter == 0 && len(stack) == 1 && S[i] == ')' {
stack = stack[1:]
continue
}
if len(stack) == 0 && S[i] == '(' {
stack = append(stack, S[i])
continue
}
if len(stack) > 0 {
switch S[i] {
case '(':
{
counter++
res += "("
}
case ')':
{
counter--
res += ")"
}
}
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1021.Remove-Outermost-Parentheses/1021. Remove Outermost Parentheses_test.go | leetcode/1021.Remove-Outermost-Parentheses/1021. Remove Outermost Parentheses_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1021 struct {
para1021
ans1021
}
// para 是参数
// one 代表第一个参数
type para1021 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans1021 struct {
one string
}
func Test_Problem1021(t *testing.T) {
qs := []question1021{
{
para1021{"(()())(())"},
ans1021{"()()()"},
},
{
para1021{"(()())(())(()(()))"},
ans1021{"()()()()(())"},
},
{
para1021{"()()"},
ans1021{""},
},
}
fmt.Printf("------------------------Leetcode Problem 1021------------------------\n")
for _, q := range qs {
_, p := q.ans1021, q.para1021
fmt.Printf("【input】:%v 【output】:%v\n", p, removeOuterParentheses(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/0115.Distinct-Subsequences/115. Distinct Subsequences_test.go | leetcode/0115.Distinct-Subsequences/115. Distinct Subsequences_test.go | package leetcode
import (
"fmt"
"testing"
)
type question115 struct {
para115
ans115
}
// para 是参数
// one 代表第一个参数
type para115 struct {
s string
t string
}
// ans 是答案
// one 代表第一个答案
type ans115 struct {
one int
}
func Test_Problem115(t *testing.T) {
qs := []question115{
{
para115{"rabbbit", "rabbit"},
ans115{3},
},
{
para115{"babgbag", "bag"},
ans115{5},
},
}
fmt.Printf("------------------------Leetcode Problem 115------------------------\n")
for _, q := range qs {
_, p := q.ans115, q.para115
fmt.Printf("【input】:%v 【output】:%v\n", p, numDistinct(p.s, 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/0115.Distinct-Subsequences/115. Distinct Subsequences.go | leetcode/0115.Distinct-Subsequences/115. Distinct Subsequences.go | package leetcode
// 解法一 压缩版 DP
func numDistinct(s string, t string) int {
dp := make([]int, len(s)+1)
for i, curT := range t {
pre := 0
for j, curS := range s {
if i == 0 {
pre = 1
}
newDP := dp[j+1]
if curT == curS {
dp[j+1] = dp[j] + pre
} else {
dp[j+1] = dp[j]
}
pre = newDP
}
}
return dp[len(s)]
}
// 解法二 普通 DP
func numDistinct1(s, t string) int {
m, n := len(s), len(t)
if m < n {
return 0
}
dp := make([][]int, m+1)
for i := range dp {
dp[i] = make([]int, n+1)
dp[i][n] = 1
}
for i := m - 1; i >= 0; i-- {
for j := n - 1; j >= 0; j-- {
if s[i] == t[j] {
dp[i][j] = dp[i+1][j+1] + dp[i+1][j]
} else {
dp[i][j] = dp[i+1][j]
}
}
}
return dp[0][0]
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0662.Maximum-Width-of-Binary-Tree/662. Maximum Width of Binary Tree_test.go | leetcode/0662.Maximum-Width-of-Binary-Tree/662. Maximum Width of Binary Tree_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question662 struct {
para662
ans662
}
// para 是参数
// one 代表第一个参数
type para662 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans662 struct {
one int
}
func Test_Problem662(t *testing.T) {
qs := []question662{
{
para662{[]int{1, 1, 1, 1, 1, 1, 1, structures.NULL, structures.NULL, structures.NULL, 1, structures.NULL, structures.NULL, structures.NULL, structures.NULL, 2, 2, 2, 2, 2, 2, 2, structures.NULL, 2, structures.NULL, structures.NULL, 2, structures.NULL, 2}},
ans662{8},
},
{
para662{[]int{1, 1, 1, 1, structures.NULL, structures.NULL, 1, 1, structures.NULL, structures.NULL, 1}},
ans662{8},
},
{
para662{[]int{}},
ans662{0},
},
{
para662{[]int{1}},
ans662{1},
},
{
para662{[]int{1, 3, 2, 5, 3, structures.NULL, 9}},
ans662{4},
},
}
fmt.Printf("------------------------Leetcode Problem 662------------------------\n")
for _, q := range qs {
_, p := q.ans662, q.para662
fmt.Printf("【input】:%v ", p)
root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", widthOfBinaryTree(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/0662.Maximum-Width-of-Binary-Tree/662. Maximum Width of Binary Tree.go | leetcode/0662.Maximum-Width-of-Binary-Tree/662. Maximum Width 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 widthOfBinaryTree(root *TreeNode) int {
if root == nil {
return 0
}
if root.Left == nil && root.Right == nil {
return 1
}
queue, res := []*TreeNode{}, 0
queue = append(queue, &TreeNode{Val: 0, Left: root.Left, Right: root.Right})
for len(queue) != 0 {
var left, right *int
// 这里需要注意,先保存 queue 的个数,相当于拿到此层的总个数
qLen := len(queue)
// 这里循环不要写 i < len(queue),因为每次循环 queue 的长度都在变小
for i := 0; i < qLen; i++ {
node := queue[0]
queue = queue[1:]
if node.Left != nil {
// 根据满二叉树父子节点的关系,得到下一层节点在本层的编号
newVal := node.Val * 2
queue = append(queue, &TreeNode{Val: newVal, Left: node.Left.Left, Right: node.Left.Right})
if left == nil || *left > newVal {
left = &newVal
}
if right == nil || *right < newVal {
right = &newVal
}
}
if node.Right != nil {
// 根据满二叉树父子节点的关系,得到下一层节点在本层的编号
newVal := node.Val*2 + 1
queue = append(queue, &TreeNode{Val: newVal, Left: node.Right.Left, Right: node.Right.Right})
if left == nil || *left > newVal {
left = &newVal
}
if right == nil || *right < newVal {
right = &newVal
}
}
}
switch {
// 某层只有一个点,那么此层宽度为 1
case left != nil && right == nil, left == nil && right != nil:
res = max(res, 1)
// 某层只有两个点,那么此层宽度为两点之间的距离
case left != nil && right != nil:
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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.