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/0054.Spiral-Matrix/54. Spiral Matrix_test.go | leetcode/0054.Spiral-Matrix/54. Spiral Matrix_test.go | package leetcode
import (
"fmt"
"testing"
)
type question54 struct {
para54
ans54
}
// para 是参数
// one 代表第一个参数
type para54 struct {
one [][]int
}
// ans 是答案
// one 代表第一个答案
type ans54 struct {
one []int
}
func Test_Problem54(t *testing.T) {
qs := []question54{
{
para54{[][]int{{3}, {2}}},
ans54{[]i... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0054.Spiral-Matrix/54. Spiral Matrix.go | leetcode/0054.Spiral-Matrix/54. Spiral Matrix.go | package leetcode
// 解法 1
func spiralOrder(matrix [][]int) []int {
if len(matrix) == 0 {
return []int{}
}
res := []int{}
if len(matrix) == 1 {
for i := 0; i < len(matrix[0]); i++ {
res = append(res, matrix[0][i])
}
return res
}
if len(matrix[0]) == 1 {
for i := 0; i < len(matrix); i++ {
res = appe... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0299.Bulls-and-Cows/299.Bulls and Cows.go | leetcode/0299.Bulls-and-Cows/299.Bulls and Cows.go | package leetcode
import "strconv"
func getHint(secret string, guess string) string {
cntA, cntB := 0, 0
mpS := make(map[byte]int)
var strG []byte
n := len(secret)
var ans string
for i := 0; i < n; i++ {
if secret[i] == guess[i] {
cntA++
} else {
mpS[secret[i]] += 1
strG = append(strG, guess[i])
}... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0299.Bulls-and-Cows/299.Bulls and Cows_test.go | leetcode/0299.Bulls-and-Cows/299.Bulls and Cows_test.go | package leetcode
import (
"fmt"
"testing"
)
type question299 struct {
para299
ans299
}
// para 是参数
type para299 struct {
secret string
guess string
}
// ans 是答案
type ans299 struct {
ans string
}
func Test_Problem299(t *testing.T) {
qs := []question299{
{
para299{"1807", "7810"},
ans299{"1A3B"},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0047.Permutations-II/47. Permutations II_test.go | leetcode/0047.Permutations-II/47. Permutations II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question47 struct {
para47
ans47
}
// para 是参数
// one 代表第一个参数
type para47 struct {
s []int
}
// ans 是答案
// one 代表第一个答案
type ans47 struct {
one [][]int
}
func Test_Problem47(t *testing.T) {
qs := []question47{
{
para47{[]int{1, 1, 2}},
ans47{[][]int{... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0047.Permutations-II/47. Permutations II.go | leetcode/0047.Permutations-II/47. Permutations II.go | package leetcode
import "sort"
func permuteUnique(nums []int) [][]int {
if len(nums) == 0 {
return [][]int{}
}
used, p, res := make([]bool, len(nums)), []int{}, [][]int{}
sort.Ints(nums) // 这里是去重的关键逻辑
generatePermutation47(nums, 0, p, &res, &used)
return res
}
func generatePermutation47(nums []int, index int... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1461.Check-If-a-String-Contains-All-Binary-Codes-of-Size-K/1461. Check If a String Contains All Binary Codes of Size K_test.go | leetcode/1461.Check-If-a-String-Contains-All-Binary-Codes-of-Size-K/1461. Check If a String Contains All Binary Codes of Size K_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1461 struct {
para1461
ans1461
}
// para 是参数
// one 代表第一个参数
type para1461 struct {
s string
k int
}
// ans 是答案
// one 代表第一个答案
type ans1461 struct {
one bool
}
func Test_Problem1461(t *testing.T) {
qs := []question1461{
{
para1461{"00110110", 2... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1461.Check-If-a-String-Contains-All-Binary-Codes-of-Size-K/1461. Check If a String Contains All Binary Codes of Size K.go | leetcode/1461.Check-If-a-String-Contains-All-Binary-Codes-of-Size-K/1461. Check If a String Contains All Binary Codes of Size K.go | package leetcode
import "math"
func hasAllCodes(s string, k int) bool {
need := int(math.Pow(2.0, float64(k)))
visited, mask, curr := make([]bool, need), (1<<k)-1, 0
for i := 0; i < len(s); i++ {
curr = ((curr << 1) | int(s[i]-'0')) & mask
if i >= k-1 { // mask 有效位达到了 k 位
if !visited[curr] {
need--
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0719.Find-K-th-Smallest-Pair-Distance/719. Find K-th Smallest Pair Distance_test.go | leetcode/0719.Find-K-th-Smallest-Pair-Distance/719. Find K-th Smallest Pair Distance_test.go | package leetcode
import (
"fmt"
"testing"
)
type question719 struct {
para719
ans719
}
// para 是参数
// one 代表第一个参数
type para719 struct {
num []int
k int
}
// ans 是答案
// one 代表第一个答案
type ans719 struct {
one int
}
func Test_Problem719(t *testing.T) {
qs := []question719{
{
para719{[]int{1, 3, 1}, 1},... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0719.Find-K-th-Smallest-Pair-Distance/719. Find K-th Smallest Pair Distance.go | leetcode/0719.Find-K-th-Smallest-Pair-Distance/719. Find K-th Smallest Pair Distance.go | package leetcode
import (
"sort"
)
func smallestDistancePair(nums []int, k int) int {
sort.Ints(nums)
low, high := 0, nums[len(nums)-1]-nums[0]
for low < high {
mid := low + (high-low)>>1
tmp := findDistanceCount(nums, mid)
if tmp >= k {
high = mid
} else {
low = mid + 1
}
}
return low
}
// 解法一... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/2022.Convert-1D-Array-Into-2D-Array/2022. Convert 1D Array Into 2D Array_test.go | leetcode/2022.Convert-1D-Array-Into-2D-Array/2022. Convert 1D Array Into 2D Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question2022 struct {
para2022
ans2022
}
// para 是参数
// one 代表第一个参数
type para2022 struct {
original []int
m int
n int
}
// ans 是答案
// one 代表第一个答案
type ans2022 struct {
one [][]int
}
func Test_Problem2022(t *testing.T) {
qs := []question2022{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/2022.Convert-1D-Array-Into-2D-Array/2022. Convert 1D Array Into 2D Array.go | leetcode/2022.Convert-1D-Array-Into-2D-Array/2022. Convert 1D Array Into 2D Array.go | package leetcode
func construct2DArray(original []int, m int, n int) [][]int {
if m*n != len(original) {
return [][]int{}
}
res := make([][]int, m)
for i := 0; i < m; i++ {
res[i] = original[n*i : n*(i+1)]
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0145.Binary-Tree-Postorder-Traversal/145. Binary Tree Postorder Traversal.go | leetcode/0145.Binary-Tree-Postorder-Traversal/145. Binary Tree Postorder Traversal.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 postorderTraversal(root *TreeNode) []int {
v... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0145.Binary-Tree-Postorder-Traversal/145. Binary Tree Postorder Traversal_test.go | leetcode/0145.Binary-Tree-Postorder-Traversal/145. Binary Tree Postorder Traversal_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question145 struct {
para145
ans145
}
// para 是参数
// one 代表第一个参数
type para145 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans145 struct {
one []int
}
func Test_Problem145(t *testing.T) {
qs := []question... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1207.Unique-Number-of-Occurrences/1207. Unique Number of Occurrences_test.go | leetcode/1207.Unique-Number-of-Occurrences/1207. Unique Number of Occurrences_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1207 struct {
para1207
ans1207
}
// para 是参数
// one 代表第一个参数
type para1207 struct {
arr []int
}
// ans 是答案
// one 代表第一个答案
type ans1207 struct {
one bool
}
func Test_Problem1207(t *testing.T) {
qs := []question1207{
{
para1207{[]int{1, 2, 2, 1, 1... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1207.Unique-Number-of-Occurrences/1207. Unique Number of Occurrences.go | leetcode/1207.Unique-Number-of-Occurrences/1207. Unique Number of Occurrences.go | package leetcode
func uniqueOccurrences(arr []int) bool {
freq, m := map[int]int{}, map[int]bool{}
for _, v := range arr {
freq[v]++
}
for _, v := range freq {
if _, ok := m[v]; !ok {
m[v] = true
} else {
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/1128.Number-of-Equivalent-Domino-Pairs/1128. Number of Equivalent Domino Pairs.go | leetcode/1128.Number-of-Equivalent-Domino-Pairs/1128. Number of Equivalent Domino Pairs.go | package leetcode
func numEquivDominoPairs(dominoes [][]int) int {
if dominoes == nil || len(dominoes) == 0 {
return 0
}
result, buckets := 0, [100]int{}
for _, dominoe := range dominoes {
key, rotatedKey := dominoe[0]*10+dominoe[1], dominoe[1]*10+dominoe[0]
if dominoe[0] != dominoe[1] {
if buckets[rotated... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1128.Number-of-Equivalent-Domino-Pairs/1128. Number of Equivalent Domino Pairs_test.go | leetcode/1128.Number-of-Equivalent-Domino-Pairs/1128. Number of Equivalent Domino Pairs_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1128 struct {
para1128
ans1128
}
// para 是参数
// one 代表第一个参数
type para1128 struct {
dominoes [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1128 struct {
one int
}
func Test_Problem1128(t *testing.T) {
qs := []question1128{
{
para1128{[][]int{{1, 2... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1539.Kth-Missing-Positive-Number/1539. Kth Missing Positive Number_test.go | leetcode/1539.Kth-Missing-Positive-Number/1539. Kth Missing Positive Number_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1539 struct {
para1539
ans1539
}
// para 是参数
// one 代表第一个参数
type para1539 struct {
arr []int
k int
}
// ans 是答案
// one 代表第一个答案
type ans1539 struct {
one int
}
func Test_Problem1539(t *testing.T) {
qs := []question1539{
{
para1539{[]int{2, 3,... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1539.Kth-Missing-Positive-Number/1539. Kth Missing Positive Number.go | leetcode/1539.Kth-Missing-Positive-Number/1539. Kth Missing Positive Number.go | package leetcode
func findKthPositive(arr []int, k int) int {
positive, index := 1, 0
for index < len(arr) {
if arr[index] != positive {
k--
} else {
index++
}
if k == 0 {
break
}
positive++
}
if k != 0 {
positive += k - 1
}
return positive
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0106.Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal/106. Construct Binary Tree from Inorder and Postorder Traversal_test.go | leetcode/0106.Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal/106. Construct Binary Tree from Inorder and Postorder Traversal_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question106 struct {
para106
ans106
}
// para 是参数
// one 代表第一个参数
type para106 struct {
inorder []int
postorder []int
}
// ans 是答案
// one 代表第一个答案
type ans106 struct {
one []int
}
func Test_Problem106(t *testing.... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0106.Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal/106. Construct Binary Tree from Inorder and Postorder Traversal.go | leetcode/0106.Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal/106. Construct Binary Tree from Inorder and Postorder Traversal.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
* }
*/
// 解法一, 直接传入需要的 slice 范围作为输入, 可以避免申请对应 inorder 索引的... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0274.H-Index/274. H-Index.go | leetcode/0274.H-Index/274. H-Index.go | package leetcode
// 解法一
func hIndex(citations []int) int {
n := len(citations)
buckets := make([]int, n+1)
for _, c := range citations {
if c >= n {
buckets[n]++
} else {
buckets[c]++
}
}
count := 0
for i := n; i >= 0; i-- {
count += buckets[i]
if count >= i {
return i
}
}
return 0
}
// 解... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0274.H-Index/274. H-Index_test.go | leetcode/0274.H-Index/274. H-Index_test.go | package leetcode
import (
"fmt"
"testing"
)
type question274 struct {
para274
ans274
}
// para 是参数
// one 代表第一个参数
type para274 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans274 struct {
one int
}
func Test_Problem274(t *testing.T) {
qs := []question274{
{
para274{[]int{3, 6, 9, 1}},
ans27... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1716.Calculate-Money-in-Leetcode-Bank/1716. Calculate Money in Leetcode Bank_test.go | leetcode/1716.Calculate-Money-in-Leetcode-Bank/1716. Calculate Money in Leetcode Bank_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1716 struct {
para1716
ans1716
}
// para 是参数
// one 代表第一个参数
type para1716 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans1716 struct {
one int
}
func Test_Problem1716(t *testing.T) {
qs := []question1716{
{
para1716{4},
ans1716{10},
},... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1716.Calculate-Money-in-Leetcode-Bank/1716. Calculate Money in Leetcode Bank.go | leetcode/1716.Calculate-Money-in-Leetcode-Bank/1716. Calculate Money in Leetcode Bank.go | package leetcode
func totalMoney(n int) int {
res := 0
for tmp, count := 1, 7; n > 0; tmp, count = tmp+1, 7 {
for m := tmp; n > 0 && count > 0; m++ {
res += m
n--
count--
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0999.Available-Captures-for-Rook/999. Available Captures for Rook.go | leetcode/0999.Available-Captures-for-Rook/999. Available Captures for Rook.go | package leetcode
func numRookCaptures(board [][]byte) int {
num := 0
for i := 0; i < len(board); i++ {
for j := 0; j < len(board[i]); j++ {
if board[i][j] == 'R' {
num += caputure(board, i-1, j, -1, 0) // Up
num += caputure(board, i+1, j, 1, 0) // Down
num += caputure(board, i, j-1, 0, -1) // Left
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0999.Available-Captures-for-Rook/999. Available Captures for Rook_test.go | leetcode/0999.Available-Captures-for-Rook/999. Available Captures for Rook_test.go | package leetcode
import (
"fmt"
"testing"
)
type question999 struct {
para999
ans999
}
// para 是参数
// one 代表第一个参数
type para999 struct {
one [][]byte
}
// ans 是答案
// one 代表第一个答案
type ans999 struct {
one int
}
func Test_Problem999(t *testing.T) {
qs := []question999{
{
para999{[][]byte{
{'.', '.', ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0387.First-Unique-Character-in-a-String/387. First Unique Character in a String.go | leetcode/0387.First-Unique-Character-in-a-String/387. First Unique Character in a String.go | package leetcode
// 解法一
func firstUniqChar(s string) int {
result := make([]int, 26)
for i := 0; i < len(s); i++ {
result[s[i]-'a']++
}
for i := 0; i < len(s); i++ {
if result[s[i]-'a'] == 1 {
return i
}
}
return -1
}
// 解法二
// 执行用时: 8 ms
// 内存消耗: 5.2 MB
func firstUniqChar1(s string) int {
charMap := ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0387.First-Unique-Character-in-a-String/387. First Unique Character in a String_test.go | leetcode/0387.First-Unique-Character-in-a-String/387. First Unique Character in a String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question387 struct {
para387
ans387
}
// para 是参数
// one 代表第一个参数
type para387 struct {
n string
}
// ans 是答案
// one 代表第一个答案
type ans387 struct {
one int
}
func Test_Problem387(t *testing.T) {
qs := []question387{
{
para387{"leetcode"},
ans387{0},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1268.Search-Suggestions-System/1268. Search Suggestions System.go | leetcode/1268.Search-Suggestions-System/1268. Search Suggestions System.go | package leetcode
import (
"sort"
)
func suggestedProducts(products []string, searchWord string) [][]string {
sort.Strings(products)
searchWordBytes, result := []byte(searchWord), make([][]string, 0, len(searchWord))
for i := 1; i <= len(searchWord); i++ {
searchWordBytes[i-1]++
products = products[:sort.Searc... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1268.Search-Suggestions-System/1268. Search Suggestions System_test.go | leetcode/1268.Search-Suggestions-System/1268. Search Suggestions System_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1268 struct {
para1268
ans1268
}
// para 是参数
// one 代表第一个参数
type para1268 struct {
products []string
searchWord string
}
// ans 是答案
// one 代表第一个答案
type ans1268 struct {
one [][]string
}
func Test_Problem1268(t *testing.T) {
qs := []question1268{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0748.Shortest-Completing-Word/748. Shortest Completing Word.go | leetcode/0748.Shortest-Completing-Word/748. Shortest Completing Word.go | package leetcode
import "unicode"
func shortestCompletingWord(licensePlate string, words []string) string {
lp := genCnter(licensePlate)
var ret string
for _, w := range words {
if match(lp, w) {
if len(w) < len(ret) || ret == "" {
ret = w
}
}
}
return ret
}
func genCnter(lp string) [26]int {
cnt... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0748.Shortest-Completing-Word/748. Shortest Completing Word_test.go | leetcode/0748.Shortest-Completing-Word/748. Shortest Completing Word_test.go | package leetcode
import (
"fmt"
"testing"
)
type question748 struct {
para748
ans748
}
// para 是参数
// one 代表第一个参数
type para748 struct {
c string
w []string
}
// ans 是答案
// one 代表第一个答案
type ans748 struct {
one string
}
func Test_Problem748(t *testing.T) {
qs := []question748{
{
para748{"1s3 PSt", []s... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0872.Leaf-Similar-Trees/872. Leaf-Similar Trees.go | leetcode/0872.Leaf-Similar-Trees/872. Leaf-Similar 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 leafSimilar(root1 *TreeNode, root2 *TreeNode) ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0872.Leaf-Similar-Trees/872. Leaf-Similar Trees_test.go | leetcode/0872.Leaf-Similar-Trees/872. Leaf-Similar Trees_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question872 struct {
para872
ans872
}
// para 是参数
// one 代表第一个参数
type para872 struct {
one []int
two []int
}
// ans 是答案
// one 代表第一个答案
type ans872 struct {
one bool
}
func Test_Problem872(t *testing.T) {
qs := ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1710.Maximum-Units-on-a-Truck/1710. Maximum Units on a Truck_test.go | leetcode/1710.Maximum-Units-on-a-Truck/1710. Maximum Units on a Truck_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1710 struct {
para1710
ans1710
}
// para 是参数
// one 代表第一个参数
type para1710 struct {
boxTypes [][]int
truckSize int
}
// ans 是答案
// one 代表第一个答案
type ans1710 struct {
one int
}
func Test_Problem1710(t *testing.T) {
qs := []question1710{
{
para17... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1710.Maximum-Units-on-a-Truck/1710. Maximum Units on a Truck.go | leetcode/1710.Maximum-Units-on-a-Truck/1710. Maximum Units on a Truck.go | package leetcode
import "sort"
func maximumUnits(boxTypes [][]int, truckSize int) int {
sort.Slice(boxTypes, func(i, j int) bool {
return boxTypes[i][1] > boxTypes[j][1]
})
res := 0
for i := 0; truckSize > 0 && i < len(boxTypes); i++ {
if truckSize >= boxTypes[i][0] {
truckSize -= boxTypes[i][0]
res += ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1221.Split-a-String-in-Balanced-Strings/1221. Split a String in Balanced Strings.go | leetcode/1221.Split-a-String-in-Balanced-Strings/1221. Split a String in Balanced Strings.go | package leetcode
func balancedStringSplit(s string) int {
count, res := 0, 0
for _, r := range s {
if r == 'R' {
count++
} else {
count--
}
if count == 0 {
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/1221.Split-a-String-in-Balanced-Strings/1221. Split a String in Balanced Strings_test.go | leetcode/1221.Split-a-String-in-Balanced-Strings/1221. Split a String in Balanced Strings_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1221 struct {
para1221
ans1221
}
// para 是参数
// one 代表第一个参数
type para1221 struct {
s string
}
// ans 是答案
// one 代表第一个答案
type ans1221 struct {
one int
}
func Test_Problem1221(t *testing.T) {
qs := []question1221{
{
para1221{"RLRRLLRLRL"},
ans... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0172.Factorial-Trailing-Zeroes/172. Factorial Trailing Zeroes_test.go | leetcode/0172.Factorial-Trailing-Zeroes/172. Factorial Trailing Zeroes_test.go | package leetcode
import (
"fmt"
"testing"
)
type question172 struct {
para172
ans172
}
// para 是参数
// one 代表第一个参数
type para172 struct {
s int
}
// ans 是答案
// one 代表第一个答案
type ans172 struct {
one int
}
func Test_Problem172(t *testing.T) {
qs := []question172{
{
para172{3},
ans172{0},
},
{
p... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0172.Factorial-Trailing-Zeroes/172. Factorial Trailing Zeroes.go | leetcode/0172.Factorial-Trailing-Zeroes/172. Factorial Trailing Zeroes.go | package leetcode
func trailingZeroes(n int) int {
if n/5 == 0 {
return 0
}
return n/5 + trailingZeroes(n/5)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0928.Minimize-Malware-Spread-II/928. Minimize Malware Spread II.go | leetcode/0928.Minimize-Malware-Spread-II/928. Minimize Malware Spread II.go | package leetcode
import (
"math"
"github.com/halfrost/LeetCode-Go/template"
)
func minMalwareSpread2(graph [][]int, initial []int) int {
if len(initial) == 0 {
return 0
}
uf, minIndex, count, countMap, malwareMap, infectMap := template.UnionFind{}, initial[0], math.MinInt64, map[int]int{}, map[int]int{}, map[... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0928.Minimize-Malware-Spread-II/928. Minimize Malware Spread II_test.go | leetcode/0928.Minimize-Malware-Spread-II/928. Minimize Malware Spread II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question928 struct {
para928
ans928
}
// para 是参数
// one 代表第一个参数
type para928 struct {
graph [][]int
initial []int
}
// ans 是答案
// one 代表第一个答案
type ans928 struct {
one int
}
func Test_Problem928(t *testing.T) {
qs := []question928{
{
para928{[][]int... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0082.Remove-Duplicates-from-Sorted-List-II/82. Remove Duplicates from Sorted List II.go | leetcode/0082.Remove-Duplicates-from-Sorted-List-II/82. Remove Duplicates from Sorted List II.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteDuplicates1(head *ListNode) *ListNode {
if head == nil {
ret... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0082.Remove-Duplicates-from-Sorted-List-II/82. Remove Duplicates from Sorted List II_test.go | leetcode/0082.Remove-Duplicates-from-Sorted-List-II/82. Remove Duplicates from Sorted List II_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question82 struct {
para82
ans82
}
// para 是参数
// one 代表第一个参数
type para82 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans82 struct {
one []int
}
func Test_Problem82(t *testing.T) {
qs := []question82{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/2165.Smallest-Value-of-the-Rearranged-Number/2165. Smallest Value of the Rearranged Number.go | leetcode/2165.Smallest-Value-of-the-Rearranged-Number/2165. Smallest Value of the Rearranged Number.go | package leetcode
import "sort"
func smallestNumber(num int64) int64 {
pos := true
if num < 0 {
pos = false
num *= -1
}
nums, m, res := []int{}, map[int]int{}, 0
for num != 0 {
tmp := int(num % 10)
m[tmp]++
num = num / 10
}
for k := range m {
nums = append(nums, k)
}
if pos {
sort.Ints(nums)
}... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/2165.Smallest-Value-of-the-Rearranged-Number/2165. Smallest Value of the Rearranged Number_test.go | leetcode/2165.Smallest-Value-of-the-Rearranged-Number/2165. Smallest Value of the Rearranged Number_test.go | package leetcode
import (
"fmt"
"testing"
)
type question2165 struct {
para2165
ans2165
}
// para 是参数
// one 代表第一个参数
type para2165 struct {
nums int64
}
// ans 是答案
// one 代表第一个答案
type ans2165 struct {
one int64
}
func Test_Problem1(t *testing.T) {
qs := []question2165{
{
para2165{310},
ans2165{103... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0094.Binary-Tree-Inorder-Traversal/94. Binary Tree Inorder Traversal_test.go | leetcode/0094.Binary-Tree-Inorder-Traversal/94. Binary Tree Inorder Traversal_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question94 struct {
para94
ans94
}
// para 是参数
// one 代表第一个参数
type para94 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans94 struct {
one []int
}
func Test_Problem94(t *testing.T) {
qs := []question94{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0094.Binary-Tree-Inorder-Traversal/94. Binary Tree Inorder Traversal.go | leetcode/0094.Binary-Tree-Inorder-Traversal/94. Binary Tree Inorder Traversal.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 inorderTraversal(root *TreeNode) []int {
var... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0070.Climbing-Stairs/70. Climbing Stairs_test.go | leetcode/0070.Climbing-Stairs/70. Climbing Stairs_test.go | package leetcode
import (
"fmt"
"testing"
)
type question70 struct {
para70
ans70
}
// para 是参数
// one 代表第一个参数
type para70 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans70 struct {
one int
}
func Test_Problem70(t *testing.T) {
qs := []question70{
{
para70{2},
ans70{2},
},
{
para70{3},... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0070.Climbing-Stairs/70. Climbing Stairs.go | leetcode/0070.Climbing-Stairs/70. Climbing Stairs.go | package leetcode
func climbStairs(n int) int {
dp := make([]int, n+1)
dp[0], dp[1] = 1, 1
for i := 2; i <= n; i++ {
dp[i] = dp[i-1] + dp[i-2]
}
return dp[n]
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0476.Number-Complement/476. Number Complement.go | leetcode/0476.Number-Complement/476. Number Complement.go | package leetcode
// 解法一
func findComplement(num int) int {
xx := ^0 // ^0 = 1111111111111111111111
for xx&num > 0 {
xx <<= 1 // 构造出来的 xx = 1111111…000000,0 的个数就是 num 的长度
}
return ^xx ^ num // xx ^ num,结果是前面的 0 全是 1 的num,再取反即是答案
}
// 解法二
func findComplement1(num int) int {
temp := 1
for temp <= num {
temp <<... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0476.Number-Complement/476. Number Complement_test.go | leetcode/0476.Number-Complement/476. Number Complement_test.go | package leetcode
import (
"fmt"
"testing"
)
type question476 struct {
para476
ans476
}
// para 是参数
// one 代表第一个参数
type para476 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans476 struct {
one int
}
func Test_Problem476(t *testing.T) {
qs := []question476{
{
para476{5},
ans476{2},
},
{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0383.Ransom-Note/383.Ransom Note_test.go | leetcode/0383.Ransom-Note/383.Ransom Note_test.go | package leetcode
import (
"fmt"
"testing"
)
type question383 struct {
para383
ans383
}
// para 是参数
type para383 struct {
ransomNote string
magazine string
}
// ans 是答案
type ans383 struct {
ans bool
}
func Test_Problem383(t *testing.T) {
qs := []question383{
{
para383{"a", "b"},
ans383{false},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0383.Ransom-Note/383.Ransom Note.go | leetcode/0383.Ransom-Note/383.Ransom Note.go | package leetcode
func canConstruct(ransomNote string, magazine string) bool {
if len(ransomNote) > len(magazine) {
return false
}
var cnt [26]int
for _, v := range magazine {
cnt[v-'a']++
}
for _, v := range ransomNote {
cnt[v-'a']--
if cnt[v-'a'] < 0 {
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/0856.Score-of-Parentheses/856. Score of Parentheses_test.go | leetcode/0856.Score-of-Parentheses/856. Score of Parentheses_test.go | package leetcode
import (
"fmt"
"testing"
)
type question856 struct {
para856
ans856
}
// para 是参数
// one 代表第一个参数
type para856 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans856 struct {
one int
}
func Test_Problem856(t *testing.T) {
qs := []question856{
{
para856{"()"},
ans856{1},
},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0856.Score-of-Parentheses/856. Score of Parentheses.go | leetcode/0856.Score-of-Parentheses/856. Score of Parentheses.go | package leetcode
func scoreOfParentheses(S string) int {
res, stack, top, temp := 0, []int{}, -1, 0
for _, s := range S {
if s == '(' {
stack = append(stack, -1)
top++
} else {
temp = 0
for stack[top] != -1 {
temp += stack[top]
stack = stack[:len(stack)-1]
top--
}
stack = stack[:len... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0460.LFU-Cache/460. LFU Cache_test.go | leetcode/0460.LFU-Cache/460. LFU Cache_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem460(t *testing.T) {
obj := Constructor(5)
fmt.Printf("obj.list = %v obj.map = %v obj.min = %v\n", MLists2Ints(&obj), MList2Ints(&obj), obj.min)
obj.Put(1, 1)
fmt.Printf("obj.list = %v obj.map = %v obj.min = %v\n", MLists2Ints(&obj), MList2Ints(&obj), ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0460.LFU-Cache/460. LFU Cache.go | leetcode/0460.LFU-Cache/460. LFU Cache.go | package leetcode
import "container/list"
type LFUCache struct {
nodes map[int]*list.Element
lists map[int]*list.List
capacity int
min int
}
type node struct {
key int
value int
frequency int
}
func Constructor(capacity int) LFUCache {
return LFUCache{nodes: make(map[int]*list.Element),
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0859.Buddy-Strings/859.Buddy Strings_test.go | leetcode/0859.Buddy-Strings/859.Buddy Strings_test.go | package leetcode
import (
"fmt"
"testing"
)
type question859 struct {
para859
ans859
}
// para 是参数
type para859 struct {
s string
goal string
}
// ans 是答案
type ans859 struct {
ans bool
}
func Test_Problem859(t *testing.T) {
qs := []question859{
{
para859{"ab", "ba"},
ans859{true},
},
{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0859.Buddy-Strings/859.Buddy Strings.go | leetcode/0859.Buddy-Strings/859.Buddy Strings.go | package leetcode
func buddyStrings(s string, goal string) bool {
if len(s) != len(goal) || len(s) <= 1 {
return false
}
mp := make(map[byte]int)
if s == goal {
for i := 0; i < len(s); i++ {
if _, ok := mp[s[i]]; ok {
return true
}
mp[s[i]]++
}
return false
}
first, second := -1, -1
for i :=... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0976.Largest-Perimeter-Triangle/976. Largest Perimeter Triangle.go | leetcode/0976.Largest-Perimeter-Triangle/976. Largest Perimeter Triangle.go | package leetcode
func largestPerimeter(A []int) int {
if len(A) < 3 {
return 0
}
quickSort164(A, 0, len(A)-1)
for i := len(A) - 1; i >= 2; i-- {
if (A[i]+A[i-1] > A[i-2]) && (A[i]+A[i-2] > A[i-1]) && (A[i-2]+A[i-1] > A[i]) {
return A[i] + A[i-1] + A[i-2]
}
}
return 0
}
func quickSort164(a []int, lo, hi... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0976.Largest-Perimeter-Triangle/976. Largest Perimeter Triangle_test.go | leetcode/0976.Largest-Perimeter-Triangle/976. Largest Perimeter Triangle_test.go | package leetcode
import (
"fmt"
"testing"
)
type question976 struct {
para976
ans976
}
// para 是参数
// one 代表第一个参数
type para976 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans976 struct {
one int
}
func Test_Problem976(t *testing.T) {
qs := []question976{
{
para976{[]int{1, 2}},
ans976{0},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0836.Rectangle-Overlap/836. Rectangle Overlap.go | leetcode/0836.Rectangle-Overlap/836. Rectangle Overlap.go | package leetcode
func isRectangleOverlap(rec1 []int, rec2 []int) bool {
return rec1[0] < rec2[2] && rec2[0] < rec1[2] && rec1[1] < rec2[3] && rec2[1] < rec1[3]
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0836.Rectangle-Overlap/836. Rectangle Overlap_test.go | leetcode/0836.Rectangle-Overlap/836. Rectangle Overlap_test.go | package leetcode
import (
"fmt"
"testing"
)
type question836 struct {
para836
ans836
}
// para 是参数
// one 代表第一个参数
type para836 struct {
rec1 []int
rec2 []int
}
// ans 是答案
// one 代表第一个答案
type ans836 struct {
one bool
}
func Test_Problem836(t *testing.T) {
qs := []question836{
{
para836{[]int{0, 0, 2,... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0794.Valid-Tic-Tac-Toe-State/794.Valid Tic-Tac-Toe State_test.go | leetcode/0794.Valid-Tic-Tac-Toe-State/794.Valid Tic-Tac-Toe State_test.go | package leetcode
import (
"fmt"
"testing"
)
type question794 struct {
para794
ans794
}
// para 是参数
type para794 struct {
board []string
}
// ans 是答案
type ans794 struct {
ans bool
}
func Test_Problem794(t *testing.T) {
qs := []question794{
{
para794{[]string{"O ", " ", " "}},
ans794{false},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0794.Valid-Tic-Tac-Toe-State/794.Valid Tic-Tac-Toe State.go | leetcode/0794.Valid-Tic-Tac-Toe-State/794.Valid Tic-Tac-Toe State.go | package leetcode
func validTicTacToe(board []string) bool {
cntX, cntO := 0, 0
for i := range board {
for j := range board[i] {
if board[i][j] == 'X' {
cntX++
} else if board[i][j] == 'O' {
cntO++
}
}
}
if cntX < cntO || cntX > cntO+1 {
return false
}
if cntX == cntO {
return process(boa... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/2181.Merge-Nodes-in-Between-Zeros/2181. Merge Nodes in Between Zeros_test.go | leetcode/2181.Merge-Nodes-in-Between-Zeros/2181. Merge Nodes in Between Zeros_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question2181 struct {
para2181
ans2181
}
// para 是参数
// one 代表第一个参数
type para2181 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans2181 struct {
one []int
}
func Test_Problem2181(t *testing.T) {
qs := []qu... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/2181.Merge-Nodes-in-Between-Zeros/2181. Merge Nodes in Between Zeros.go | leetcode/2181.Merge-Nodes-in-Between-Zeros/2181. Merge Nodes in Between Zeros.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 mergeNodes(head *ListNode) *ListNode {
res := &ListNode{}
h := res
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0519.Random-Flip-Matrix/519.Random Flip Matrix_test.go | leetcode/0519.Random-Flip-Matrix/519.Random Flip Matrix_test.go | package leetcode
import (
"fmt"
"testing"
)
type question519 struct {
para519
ans519
}
// para 是参数
type para519 struct {
para []string
val [][]int
}
// ans 是答案
type ans519 struct {
ans [][]int
}
func Test_Problem519(t *testing.T) {
qs := []question519{
{
para519{[]string{"Solution", "flip", "flip",... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0519.Random-Flip-Matrix/519.Random Flip Matrix.go | leetcode/0519.Random-Flip-Matrix/519.Random Flip Matrix.go | package leetcode
import (
"math/rand"
)
type Solution struct {
r int
c int
total int
mp map[int]int
}
func Constructor(m int, n int) Solution {
return Solution{
r: m,
c: n,
total: m * n,
mp: map[int]int{},
}
}
func (this *Solution) Flip() []int {
k := rand.Intn(this.total)
val... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0888.Fair-Candy-Swap/888. Fair Candy Swap_test.go | leetcode/0888.Fair-Candy-Swap/888. Fair Candy Swap_test.go | package leetcode
import (
"fmt"
"testing"
)
type question888 struct {
para888
ans888
}
// para 是参数
// one 代表第一个参数
type para888 struct {
one []int
two []int
}
// ans 是答案
// one 代表第一个答案
type ans888 struct {
one []int
}
func Test_Problem888(t *testing.T) {
qs := []question888{
{
para888{[]int{}, []int{... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0888.Fair-Candy-Swap/888. Fair Candy Swap.go | leetcode/0888.Fair-Candy-Swap/888. Fair Candy Swap.go | package leetcode
func fairCandySwap(A []int, B []int) []int {
hDiff, aMap := diff(A, B)/2, make(map[int]int, len(A))
for _, a := range A {
aMap[a] = a
}
for _, b := range B {
if a, ok := aMap[hDiff+b]; ok {
return []int{a, b}
}
}
return nil
}
func diff(A []int, B []int) int {
diff, maxLen := 0, max(le... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0386.Lexicographical-Numbers/386. Lexicographical Numbers.go | leetcode/0386.Lexicographical-Numbers/386. Lexicographical Numbers.go | package leetcode
func lexicalOrder(n int) []int {
res := make([]int, 0, n)
dfs386(1, n, &res)
return res
}
func dfs386(x, n int, res *[]int) {
limit := (x + 10) / 10 * 10
for x <= n && x < limit {
*res = append(*res, x)
if x*10 <= n {
dfs386(x*10, n, res)
}
x++
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0386.Lexicographical-Numbers/386. Lexicographical Numbers_test.go | leetcode/0386.Lexicographical-Numbers/386. Lexicographical Numbers_test.go | package leetcode
import (
"fmt"
"testing"
)
type question386 struct {
para386
ans386
}
// para 是参数
// one 代表第一个参数
type para386 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans386 struct {
one []int
}
func Test_Problem386(t *testing.T) {
qs := []question386{
{
para386{13},
ans386{[]int{1, 10, 11... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0151.Reverse-Words-in-a-String/151. Reverse Words in a String.go | leetcode/0151.Reverse-Words-in-a-String/151. Reverse Words in a String.go | package leetcode
import "strings"
func reverseWords151(s string) string {
ss := strings.Fields(s)
reverse151(&ss, 0, len(ss)-1)
return strings.Join(ss, " ")
}
func reverse151(m *[]string, i int, j int) {
for i <= j {
(*m)[i], (*m)[j] = (*m)[j], (*m)[i]
i++
j--
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0151.Reverse-Words-in-a-String/151. Reverse Words in a String_test.go | leetcode/0151.Reverse-Words-in-a-String/151. Reverse Words in a String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question151 struct {
para151
ans151
}
// para 是参数
// one 代表第一个参数
type para151 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans151 struct {
one string
}
func Test_Problem151(t *testing.T) {
qs := []question151{
{
para151{"the sky is blue"},
a... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0547.Number-of-Provinces/547. Number of Provinces_test.go | leetcode/0547.Number-of-Provinces/547. Number of Provinces_test.go | package leetcode
import (
"fmt"
"testing"
)
type question547 struct {
para547
ans547
}
// para 是参数
// one 代表第一个参数
type para547 struct {
one [][]int
}
// ans 是答案
// one 代表第一个答案
type ans547 struct {
one int
}
func Test_Problem547(t *testing.T) {
qs := []question547{
{
para547{[][]int{{0, 0, 0}, {0, 1, ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0547.Number-of-Provinces/547. Number of Provinces.go | leetcode/0547.Number-of-Provinces/547. Number of Provinces.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/template"
)
// 解法一 并查集
func findCircleNum(M [][]int) int {
n := len(M)
if n == 0 {
return 0
}
uf := template.UnionFind{}
uf.Init(n)
for i := 0; i < n; i++ {
for j := 0; j <= i; j++ {
if M[i][j] == 1 {
uf.Union(i, j)
}
}
}
return uf.T... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1254.Number-of-Closed-Islands/1254. Number of Closed Islands_test.go | leetcode/1254.Number-of-Closed-Islands/1254. Number of Closed Islands_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1254 struct {
para1254
ans1254
}
// para 是参数
// one 代表第一个参数
type para1254 struct {
one [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1254 struct {
one int
}
func Test_Problem1254(t *testing.T) {
qs := []question1254{
{
para1254{[][]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/1254.Number-of-Closed-Islands/1254. Number of Closed Islands.go | leetcode/1254.Number-of-Closed-Islands/1254. Number of Closed Islands.go | package leetcode
var dir = [][]int{
{-1, 0},
{0, 1},
{1, 0},
{0, -1},
}
func closedIsland(grid [][]int) int {
m := len(grid)
if m == 0 {
return 0
}
n := len(grid[0])
if n == 0 {
return 0
}
res, visited := 0, make([][]bool, m)
for i := 0; i < m; i++ {
visited[i] = make([]bool, n)
}
for i := 0; i < ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0506.Relative-Ranks/506.Relative Ranks_test.go | leetcode/0506.Relative-Ranks/506.Relative Ranks_test.go | package leetcode
import (
"fmt"
"testing"
)
type question506 struct {
para506
ans506
}
// para 是参数
type para506 struct {
score []int
}
// ans 是答案
type ans506 struct {
ans []string
}
func Test_Problem506(t *testing.T) {
qs := []question506{
{
para506{[]int{5, 4, 3, 2, 1}},
ans506{[]string{"Gold Med... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0506.Relative-Ranks/506.Relative Ranks.go | leetcode/0506.Relative-Ranks/506.Relative Ranks.go | package leetcode
import (
"sort"
"strconv"
)
func findRelativeRanks(score []int) []string {
mp := make(map[int]int)
for i, v := range score {
mp[v] = i
}
sort.Slice(score, func(i, j int) bool {
return score[i] > score[j]
})
ans := make([]string, len(score))
for i, v := range score {
if i == 0 {
ans[... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0851.Loud-and-Rich/851. Loud and Rich.go | leetcode/0851.Loud-and-Rich/851. Loud and Rich.go | package leetcode
func loudAndRich(richer [][]int, quiet []int) []int {
edges := make([][]int, len(quiet))
for i := range edges {
edges[i] = []int{}
}
indegrees := make([]int, len(quiet))
for _, edge := range richer {
n1, n2 := edge[0], edge[1]
edges[n1] = append(edges[n1], n2)
indegrees[n2]++
}
res := m... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0851.Loud-and-Rich/851. Loud and Rich_test.go | leetcode/0851.Loud-and-Rich/851. Loud and Rich_test.go | package leetcode
import (
"fmt"
"testing"
)
type question851 struct {
para851
ans851
}
// para 是参数
// one 代表第一个参数
type para851 struct {
richer [][]int
quiet []int
}
// ans 是答案
// one 代表第一个答案
type ans851 struct {
one []int
}
func Test_Problem851(t *testing.T) {
qs := []question851{
{
para851{[][]int... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0033.Search-in-Rotated-Sorted-Array/33. Search in Rotated Sorted Array_test.go | leetcode/0033.Search-in-Rotated-Sorted-Array/33. Search in Rotated Sorted Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question33 struct {
para33
ans33
}
// para 是参数
// one 代表第一个参数
type para33 struct {
nums []int
target int
}
// ans 是答案
// one 代表第一个答案
type ans33 struct {
one int
}
func Test_Problem33(t *testing.T) {
qs := []question33{
{
para33{[]int{3, 1}, 1},
a... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0033.Search-in-Rotated-Sorted-Array/33. Search in Rotated Sorted Array.go | leetcode/0033.Search-in-Rotated-Sorted-Array/33. Search in Rotated Sorted Array.go | package leetcode
func search33(nums []int, target int) int {
if len(nums) == 0 {
return -1
}
low, high := 0, len(nums)-1
for low <= high {
mid := low + (high-low)>>1
if nums[mid] == target {
return mid
} else if nums[mid] > nums[low] { // 在数值大的一部分区间里
if nums[low] <= target && target < nums[mid] {
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0024.Swap-Nodes-in-Pairs/24. Swap Nodes in Pairs_test.go | leetcode/0024.Swap-Nodes-in-Pairs/24. Swap Nodes in Pairs_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question24 struct {
para24
ans24
}
// para 是参数
// one 代表第一个参数
type para24 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans24 struct {
one []int
}
func Test_Problem24(t *testing.T) {
qs := []question24{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0024.Swap-Nodes-in-Pairs/24. Swap Nodes in Pairs.go | leetcode/0024.Swap-Nodes-in-Pairs/24. Swap Nodes in Pairs.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 swapPairs(head *ListNode) *ListNode {
dummy := &ListNode{Next: head... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0198.House-Robber/198. House Robber.go | leetcode/0198.House-Robber/198. House Robber.go | package leetcode
// 解法一 DP
func rob198(nums []int) int {
n := len(nums)
if n == 0 {
return 0
}
if n == 1 {
return nums[0]
}
// dp[i] 代表抢 nums[0...i] 房子的最大价值
dp := make([]int, n)
dp[0], dp[1] = nums[0], max(nums[1], nums[0])
for i := 2; i < n; i++ {
dp[i] = max(dp[i-1], nums[i]+dp[i-2])
}
return dp[n-1... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0198.House-Robber/198. House Robber_test.go | leetcode/0198.House-Robber/198. House Robber_test.go | package leetcode
import (
"fmt"
"testing"
)
type question198 struct {
para198
ans198
}
// para 是参数
// one 代表第一个参数
type para198 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans198 struct {
one int
}
func Test_Problem198(t *testing.T) {
qs := []question198{
{
para198{[]int{1, 2}},
ans198{2},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1313.Decompress-Run-Length-Encoded-List/1313. Decompress Run-Length Encoded List.go | leetcode/1313.Decompress-Run-Length-Encoded-List/1313. Decompress Run-Length Encoded List.go | package leetcode
func decompressRLElist(nums []int) []int {
res := []int{}
for i := 0; i < len(nums); i += 2 {
for j := 0; j < nums[i]; j++ {
res = append(res, nums[i+1])
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1313.Decompress-Run-Length-Encoded-List/1313. Decompress Run-Length Encoded List_test.go | leetcode/1313.Decompress-Run-Length-Encoded-List/1313. Decompress Run-Length Encoded List_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1313 struct {
para1313
ans1313
}
// para 是参数
// one 代表第一个参数
type para1313 struct {
nums []int
}
// ans 是答案
// one 代表第一个答案
type ans1313 struct {
one []int
}
func Test_Problem1313(t *testing.T) {
qs := []question1313{
{
para1313{[]int{1, 2, 3, 4}... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0120.Triangle/120. Triangle.go | leetcode/0120.Triangle/120. Triangle.go | package leetcode
import (
"math"
)
// 解法一 倒序 DP,无辅助空间
func minimumTotal(triangle [][]int) int {
if triangle == nil {
return 0
}
for row := len(triangle) - 2; row >= 0; row-- {
for col := 0; col < len(triangle[row]); col++ {
triangle[row][col] += min(triangle[row+1][col], triangle[row+1][col+1])
}
}
ret... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0120.Triangle/120. Triangle_test.go | leetcode/0120.Triangle/120. Triangle_test.go | package leetcode
import (
"fmt"
"testing"
)
type question120 struct {
para120
ans120
}
// para 是参数
// one 代表第一个参数
type para120 struct {
one [][]int
}
// ans 是答案
// one 代表第一个答案
type ans120 struct {
one int
}
func Test_Problem120(t *testing.T) {
qs := []question120{
{
para120{[][]int{{2}, {3, 4}, {6, 5,... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0453.Minimum-Moves-to-Equal-Array-Elements/453. Minimum Moves to Equal Array Elements_test.go | leetcode/0453.Minimum-Moves-to-Equal-Array-Elements/453. Minimum Moves to Equal Array Elements_test.go | package leetcode
import (
"fmt"
"testing"
)
type question453 struct {
para453
ans453
}
// para 是参数
// one 代表第一个参数
type para453 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans453 struct {
one int
}
func Test_Problem453(t *testing.T) {
qs := []question453{
{
para453{[]int{4, 3, 2, 7, 8, 2, 3, 1... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0453.Minimum-Moves-to-Equal-Array-Elements/453. Minimum Moves to Equal Array Elements.go | leetcode/0453.Minimum-Moves-to-Equal-Array-Elements/453. Minimum Moves to Equal Array Elements.go | package leetcode
import "math"
func minMoves(nums []int) int {
sum, min, l := 0, math.MaxInt32, len(nums)
for _, v := range nums {
sum += v
if min > v {
min = v
}
}
return sum - min*l
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0113.Path-Sum-II/113. Path Sum II.go | leetcode/0113.Path-Sum-II/113. Path Sum II.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 pathSum(root *TreeNode, sum int) [][]i... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0113.Path-Sum-II/113. Path Sum II_test.go | leetcode/0113.Path-Sum-II/113. Path Sum II_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question113 struct {
para113
ans113
}
// para 是参数
// one 代表第一个参数
type para113 struct {
one []int
sum int
}
// ans 是答案
// one 代表第一个答案
type ans113 struct {
one [][]int
}
func Test_Problem113(t *testing.T) {
qs :=... | 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.