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/0337.House-Robber-III/337. House Robber III_test.go | leetcode/0337.House-Robber-III/337. House Robber III_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question337 struct {
para337
ans337
}
// para 是参数
// one 代表第一个参数
type para337 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans337 struct {
one int
}
func Test_Problem337(t *testing.T) {
qs := []question337{
{
para337{[]int{3, 2, 3, structures.NULL, 3, structures.NULL, 1}},
ans337{7},
},
{
para337{[]int{}},
ans337{0},
},
{
para337{[]int{3, 4, 5, 1, 3, structures.NULL, 1}},
ans337{9},
},
}
fmt.Printf("------------------------Leetcode Problem 337------------------------\n")
for _, q := range qs {
_, p := q.ans337, q.para337
fmt.Printf("【input】:%v 【output】:%v\n", p, rob337(structures.Ints2TreeNode(p.one)))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0337.House-Robber-III/337. House Robber III.go | leetcode/0337.House-Robber-III/337. House Robber III.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 rob337(root *TreeNode) int {
a, b := dfsTreeRob(root)
return max(a, b)
}
func dfsTreeRob(root *TreeNode) (a, b int) {
if root == nil {
return 0, 0
}
l0, l1 := dfsTreeRob(root.Left)
r0, r1 := dfsTreeRob(root.Right)
// 当前节点没有被打劫
tmp0 := max(l0, l1) + max(r0, r1)
// 当前节点被打劫
tmp1 := root.Val + l0 + r0
return tmp0, tmp1
}
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/0179.Largest-Number/179. Largest Number.go | leetcode/0179.Largest-Number/179. Largest Number.go | package leetcode
import (
"strconv"
)
func largestNumber(nums []int) string {
if len(nums) == 0 {
return ""
}
numStrs := toStringArray(nums)
quickSortString(numStrs, 0, len(numStrs)-1)
res := ""
for _, str := range numStrs {
if res == "0" && str == "0" {
continue
}
res = res + str
}
return res
}
func toStringArray(nums []int) []string {
strs := make([]string, 0)
for _, num := range nums {
strs = append(strs, strconv.Itoa(num))
}
return strs
}
func partitionString(a []string, lo, hi int) int {
pivot := a[hi]
i := lo - 1
for j := lo; j < hi; j++ {
ajStr := a[j] + pivot
pivotStr := pivot + a[j]
if ajStr > pivotStr { // 这里的判断条件是关键
i++
a[j], a[i] = a[i], a[j]
}
}
a[i+1], a[hi] = a[hi], a[i+1]
return i + 1
}
func quickSortString(a []string, lo, hi int) {
if lo >= hi {
return
}
p := partitionString(a, lo, hi)
quickSortString(a, lo, p-1)
quickSortString(a, p+1, hi)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0179.Largest-Number/179. Largest Number_test.go | leetcode/0179.Largest-Number/179. Largest Number_test.go | package leetcode
import (
"fmt"
"testing"
)
type question179 struct {
para179
ans179
}
// para 是参数
// one 代表第一个参数
type para179 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans179 struct {
one string
}
func Test_Problem179(t *testing.T) {
qs := []question179{
{
para179{[]int{3, 6, 9, 1}},
ans179{"9631"},
},
{
para179{[]int{1}},
ans179{"1"},
},
{
para179{[]int{}},
ans179{""},
},
{
para179{[]int{2, 10}},
ans179{"210"},
},
{
para179{[]int{3, 30, 34, 5, 9}},
ans179{"9534330"},
},
{
para179{[]int{12, 128}},
ans179{"12812"},
},
{
para179{[]int{12, 121}},
ans179{"12121"},
},
{
para179{[]int{0, 0}},
ans179{"0"},
},
{
para179{[]int{1440, 7548, 4240, 6616, 733, 4712, 883, 8, 9576}},
ans179{"9576888375487336616471242401440"},
},
}
fmt.Printf("------------------------Leetcode Problem 179------------------------\n")
for _, q := range qs {
_, p := q.ans179, q.para179
fmt.Printf("【input】:%v 【output】:%v\n", p, largestNumber(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/0042.Trapping-Rain-Water/42. Trapping Rain Water.go | leetcode/0042.Trapping-Rain-Water/42. Trapping Rain Water.go | package leetcode
func trap(height []int) int {
res, left, right, maxLeft, maxRight := 0, 0, len(height)-1, 0, 0
for left <= right {
if height[left] <= height[right] {
if height[left] > maxLeft {
maxLeft = height[left]
} else {
res += maxLeft - height[left]
}
left++
} else {
if height[right] >= maxRight {
maxRight = height[right]
} else {
res += maxRight - height[right]
}
right--
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0042.Trapping-Rain-Water/42. Trapping Rain Water_test.go | leetcode/0042.Trapping-Rain-Water/42. Trapping Rain Water_test.go | package leetcode
import (
"fmt"
"testing"
)
type question42 struct {
para42
ans42
}
// para 是参数
// one 代表第一个参数
type para42 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans42 struct {
one int
}
func Test_Problem42(t *testing.T) {
qs := []question42{
{
para42{[]int{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}},
ans42{6},
},
}
fmt.Printf("------------------------Leetcode Problem 42------------------------\n")
for _, q := range qs {
_, p := q.ans42, q.para42
fmt.Printf("【input】:%v 【output】:%v\n", p, trap(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/0881.Boats-to-Save-People/881. Boats to Save People.go | leetcode/0881.Boats-to-Save-People/881. Boats to Save People.go | package leetcode
import (
"sort"
)
func numRescueBoats(people []int, limit int) int {
sort.Ints(people)
left, right, res := 0, len(people)-1, 0
for left <= right {
if left == right {
res++
return res
}
if people[left]+people[right] <= limit {
left++
right--
} else {
right--
}
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/0881.Boats-to-Save-People/881. Boats to Save People_test.go | leetcode/0881.Boats-to-Save-People/881. Boats to Save People_test.go | package leetcode
import (
"fmt"
"testing"
)
type question881 struct {
para881
ans881
}
// para 是参数
// one 代表第一个参数
type para881 struct {
s []int
k int
}
// ans 是答案
// one 代表第一个答案
type ans881 struct {
one int
}
func Test_Problem881(t *testing.T) {
qs := []question881{
{
para881{[]int{1, 2}, 3},
ans881{1},
},
{
para881{[]int{3, 2, 2, 1}, 3},
ans881{3},
},
{
para881{[]int{3, 5, 3, 4}, 5},
ans881{4},
},
{
para881{[]int{5, 1, 4, 2}, 6},
ans881{2},
},
{
para881{[]int{3, 2, 2, 1}, 3},
ans881{3},
},
}
fmt.Printf("------------------------Leetcode Problem 881------------------------\n")
for _, q := range qs {
_, p := q.ans881, q.para881
fmt.Printf("【input】:%v 【output】:%v\n", p, numRescueBoats(p.s, p.k))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0803.Bricks-Falling-When-Hit/803. Bricks Falling When Hit_test.go | leetcode/0803.Bricks-Falling-When-Hit/803. Bricks Falling When Hit_test.go | package leetcode
import (
"fmt"
"testing"
)
type question803 struct {
para803
ans803
}
// para 是参数
// one 代表第一个参数
type para803 struct {
grid [][]int
hits [][]int
}
// ans 是答案
// one 代表第一个答案
type ans803 struct {
one []int
}
func Test_Problem803(t *testing.T) {
qs := []question803{
{
para803{[][]int{{1, 1, 1, 0, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 1, 1, 1}, {1, 1, 1, 0, 0, 0, 1, 1}, {1, 1, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 0, 0}},
[][]int{{4, 6}, {3, 0}, {2, 3}, {2, 6}, {4, 1}, {5, 2}, {2, 1}}},
ans803{[]int{0, 2, 0, 0, 0, 0, 2}},
},
{
para803{[][]int{{1, 0, 1}, {1, 1, 1}}, [][]int{{0, 0}, {0, 2}, {1, 1}}},
ans803{[]int{0, 3, 0}},
},
{
para803{[][]int{{1, 0, 0, 0}, {1, 1, 1, 0}}, [][]int{{1, 0}}},
ans803{[]int{2}},
},
{
para803{[][]int{{1, 0, 0, 0}, {1, 1, 0, 0}}, [][]int{{1, 1}, {1, 0}}},
ans803{[]int{0, 0}},
},
}
fmt.Printf("------------------------Leetcode Problem 803------------------------\n")
for _, q := range qs {
_, p := q.ans803, q.para803
fmt.Printf("【input】:%v 【output】:%v\n", p, hitBricks(p.grid, p.hits))
}
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/0803.Bricks-Falling-When-Hit/803. Bricks Falling When Hit.go | leetcode/0803.Bricks-Falling-When-Hit/803. Bricks Falling When Hit.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/template"
)
var dir = [][]int{
{-1, 0},
{0, 1},
{1, 0},
{0, -1},
}
func hitBricks(grid [][]int, hits [][]int) []int {
if len(hits) == 0 {
return []int{}
}
uf, m, n, res, oriCount := template.UnionFindCount{}, len(grid), len(grid[0]), make([]int, len(hits)), 0
uf.Init(m*n + 1)
// 先将要打掉的砖块染色
for _, hit := range hits {
if grid[hit[0]][hit[1]] == 1 {
grid[hit[0]][hit[1]] = 2
}
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == 1 {
getUnionFindFromGrid(grid, i, j, uf)
}
}
}
oriCount = uf.Count()[uf.Find(m*n)]
for i := len(hits) - 1; i >= 0; i-- {
if grid[hits[i][0]][hits[i][1]] == 2 {
grid[hits[i][0]][hits[i][1]] = 1
getUnionFindFromGrid(grid, hits[i][0], hits[i][1], uf)
}
nowCount := uf.Count()[uf.Find(m*n)]
if nowCount-oriCount > 0 {
res[i] = nowCount - oriCount - 1
} else {
res[i] = 0
}
oriCount = nowCount
}
return res
}
func isInGrid(grid [][]int, x, y int) bool {
return x >= 0 && x < len(grid) && y >= 0 && y < len(grid[0])
}
func getUnionFindFromGrid(grid [][]int, x, y int, uf template.UnionFindCount) {
m, n := len(grid), len(grid[0])
if x == 0 {
uf.Union(m*n, x*n+y)
}
for i := 0; i < 4; i++ {
nx := x + dir[i][0]
ny := y + dir[i][1]
if isInGrid(grid, nx, ny) && grid[nx][ny] == 1 {
uf.Union(nx*n+ny, x*n+y)
}
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0405.Convert-a-Number-to-Hexadecimal/405. Convert a Number to Hexadecimal_test.go | leetcode/0405.Convert-a-Number-to-Hexadecimal/405. Convert a Number to Hexadecimal_test.go | package leetcode
import (
"fmt"
"testing"
)
type question405 struct {
para405
ans405
}
// para 是参数
// one 代表第一个参数
type para405 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans405 struct {
one string
}
func Test_Problem405(t *testing.T) {
qs := []question405{
{
para405{26},
ans405{"1a"},
},
{
para405{-1},
ans405{"ffffffff"},
},
}
fmt.Printf("------------------------Leetcode Problem 405------------------------\n")
for _, q := range qs {
_, p := q.ans405, q.para405
fmt.Printf("【input】:%v 【output】:%v\n", p, toHex(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/0405.Convert-a-Number-to-Hexadecimal/405. Convert a Number to Hexadecimal.go | leetcode/0405.Convert-a-Number-to-Hexadecimal/405. Convert a Number to Hexadecimal.go | package leetcode
func toHex(num int) string {
if num == 0 {
return "0"
}
if num < 0 {
num += 1 << 32
}
mp := map[int]string{
0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9",
10: "a", 11: "b", 12: "c", 13: "d", 14: "e", 15: "f",
}
var bitArr []string
for num > 0 {
bitArr = append(bitArr, mp[num%16])
num /= 16
}
str := ""
for i := len(bitArr) - 1; i >= 0; i-- {
str += bitArr[i]
}
return str
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0846.Hand-of-Straights/846.Hand of Straights_test.go | leetcode/0846.Hand-of-Straights/846.Hand of Straights_test.go | package leetcode
import (
"fmt"
"testing"
)
type question846 struct {
para846
ans846
}
// para 是参数
type para846 struct {
hand []int
groupSize int
}
// ans 是答案
type ans846 struct {
ans bool
}
func Test_Problem846(t *testing.T) {
qs := []question846{
{
para846{[]int{1, 2, 3, 6, 2, 3, 4, 7, 8}, 3},
ans846{true},
},
{
para846{[]int{1, 2, 3, 4, 5}, 4},
ans846{false},
},
}
fmt.Printf("------------------------Leetcode Problem 846------------------------\n")
for _, q := range qs {
_, p := q.ans846, q.para846
fmt.Printf("【input】:%v 【output】:%v\n", p, isNStraightHand(p.hand, p.groupSize))
}
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/0846.Hand-of-Straights/846.Hand of Straights.go | leetcode/0846.Hand-of-Straights/846.Hand of Straights.go | package leetcode
import "sort"
func isNStraightHand(hand []int, groupSize int) bool {
mp := make(map[int]int)
for _, v := range hand {
mp[v] += 1
}
sort.Ints(hand)
for _, num := range hand {
if mp[num] == 0 {
continue
}
for diff := 0; diff < groupSize; diff++ {
if mp[num+diff] == 0 {
return false
}
mp[num+diff] -= 1
}
}
return true
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0971.Flip-Binary-Tree-To-Match-Preorder-Traversal/971. Flip Binary Tree To Match Preorder Traversal.go | leetcode/0971.Flip-Binary-Tree-To-Match-Preorder-Traversal/971. Flip Binary Tree To Match Preorder 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 flipMatchVoyage(root *TreeNode, voyage []int) []int {
res, index := make([]int, 0, len(voyage)), 0
if travelTree(root, &index, voyage, &res) {
return res
}
return []int{-1}
}
func travelTree(root *TreeNode, index *int, voyage []int, res *[]int) bool {
if root == nil {
return true
}
if root.Val != voyage[*index] {
return false
}
*index++
if root.Left != nil && root.Left.Val != voyage[*index] {
*res = append(*res, root.Val)
return travelTree(root.Right, index, voyage, res) && travelTree(root.Left, index, voyage, res)
}
return travelTree(root.Left, index, voyage, res) && travelTree(root.Right, index, voyage, res)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0971.Flip-Binary-Tree-To-Match-Preorder-Traversal/971. Flip Binary Tree To Match Preorder Traversal_test.go | leetcode/0971.Flip-Binary-Tree-To-Match-Preorder-Traversal/971. Flip Binary Tree To Match Preorder Traversal_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question971 struct {
para971
ans971
}
// para 是参数
// one 代表第一个参数
type para971 struct {
one []int
voyage []int
}
// ans 是答案
// one 代表第一个答案
type ans971 struct {
one []int
}
func Test_Problem971(t *testing.T) {
qs := []question971{
{
para971{[]int{1, 2, structures.NULL}, []int{2, 1}},
ans971{[]int{-1}},
},
{
para971{[]int{1, 2, 3}, []int{1, 3, 2}},
ans971{[]int{1}},
},
{
para971{[]int{1, 2, 3}, []int{1, 2, 3}},
ans971{[]int{}},
},
}
fmt.Printf("------------------------Leetcode Problem 971------------------------\n")
for _, q := range qs {
_, p := q.ans971, q.para971
fmt.Printf("【input】:%v ", p)
rootOne := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", flipMatchVoyage(rootOne, p.voyage))
}
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/0532.K-diff-Pairs-in-an-Array/532. K-diff Pairs in an Array.go | leetcode/0532.K-diff-Pairs-in-an-Array/532. K-diff Pairs in an Array.go | package leetcode
func findPairs(nums []int, k int) int {
if k < 0 || len(nums) == 0 {
return 0
}
var count int
m := make(map[int]int, len(nums))
for _, value := range nums {
m[value]++
}
for key := range m {
if k == 0 && m[key] > 1 {
count++
continue
}
if k > 0 && m[key+k] > 0 {
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/0532.K-diff-Pairs-in-an-Array/532. K-diff Pairs in an Array_test.go | leetcode/0532.K-diff-Pairs-in-an-Array/532. K-diff Pairs in an Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question532 struct {
para532
ans532
}
// para 是参数
// one 代表第一个参数
type para532 struct {
one []int
n int
}
// ans 是答案
// one 代表第一个答案
type ans532 struct {
one int
}
func Test_Problem532(t *testing.T) {
qs := []question532{
{
para532{[]int{3, 1, 4, 1, 5}, 2},
ans532{2},
},
{
para532{[]int{1, 2, 3, 4, 5}, 1},
ans532{4},
},
{
para532{[]int{1, 3, 1, 5, 4}, 0},
ans532{1},
},
{
para532{[]int{}, 3},
ans532{0},
},
// question532{
// para532{[]int{1, 2, 3, 2, 3, 2, 3, 2}, 0},
// ans532{[]int{1, 2, 3, 2, 3, 2, 3, 2}},
// },
// question532{
// para532{[]int{1, 2, 3, 4, 5}, 5},
// ans532{[]int{1, 2, 3, 4}},
// },
// question532{
// para532{[]int{}, 5},
// ans532{[]int{}},
// },
// question532{
// para532{[]int{1, 2, 3, 4, 5}, 10},
// ans532{[]int{1, 2, 3, 4, 5}},
// },
// question532{
// para532{[]int{1}, 1},
// ans532{[]int{}},
// },
}
fmt.Printf("------------------------Leetcode Problem 532------------------------\n")
for _, q := range qs {
_, p := q.ans532, q.para532
fmt.Printf("【input】:%v 【output】:%v\n", p, findPairs(p.one, 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/1437.Check-If-All-1s-Are-at-Least-Length-K-Places-Away/1437. Check If All 1s Are at Least Length K Places Away.go | leetcode/1437.Check-If-All-1s-Are-at-Least-Length-K-Places-Away/1437. Check If All 1s Are at Least Length K Places Away.go | package leetcode
func kLengthApart(nums []int, k int) bool {
prevIndex := -1
for i, num := range nums {
if num == 1 {
if prevIndex != -1 && i-prevIndex-1 < k {
return false
}
prevIndex = i
}
}
return true
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1437.Check-If-All-1s-Are-at-Least-Length-K-Places-Away/1437. Check If All 1s Are at Least Length K Places Away_test.go | leetcode/1437.Check-If-All-1s-Are-at-Least-Length-K-Places-Away/1437. Check If All 1s Are at Least Length K Places Away_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1437 struct {
para1437
ans1437
}
// para 是参数
// one 代表第一个参数
type para1437 struct {
nums []int
k int
}
// ans 是答案
// one 代表第一个答案
type ans1437 struct {
one bool
}
func Test_Problem1437(t *testing.T) {
qs := []question1437{
{
para1437{[]int{1, 0, 0, 0, 1, 0, 0, 1}, 2},
ans1437{true},
},
{
para1437{[]int{1, 0, 0, 1, 0, 1}, 2},
ans1437{false},
},
{
para1437{[]int{1, 1, 1, 1, 1}, 0},
ans1437{true},
},
{
para1437{[]int{0, 1, 0, 1}, 1},
ans1437{true},
},
}
fmt.Printf("------------------------Leetcode Problem 1437------------------------\n")
for _, q := range qs {
_, p := q.ans1437, q.para1437
fmt.Printf("【input】:%v ", p)
fmt.Printf("【output】:%v \n", kLengthApart(p.nums, p.k))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1732.Find-the-Highest-Altitude/1732. Find the Highest Altitude.go | leetcode/1732.Find-the-Highest-Altitude/1732. Find the Highest Altitude.go | package leetcode
func largestAltitude(gain []int) int {
max, height := 0, 0
for _, g := range gain {
height += g
if height > max {
max = height
}
}
return max
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1732.Find-the-Highest-Altitude/1732. Find the Highest Altitude_test.go | leetcode/1732.Find-the-Highest-Altitude/1732. Find the Highest Altitude_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1732 struct {
para1732
ans1732
}
// para 是参数
// one 代表第一个参数
type para1732 struct {
gain []int
}
// ans 是答案
// one 代表第一个答案
type ans1732 struct {
one int
}
func Test_Problem1732(t *testing.T) {
qs := []question1732{
{
para1732{[]int{-5, 1, 5, 0, -7}},
ans1732{1},
},
{
para1732{[]int{-4, -3, -2, -1, 4, 3, 2}},
ans1732{0},
},
}
fmt.Printf("------------------------Leetcode Problem 1732------------------------\n")
for _, q := range qs {
_, p := q.ans1732, q.para1732
fmt.Printf("【input】:%v 【output】:%v\n", p, largestAltitude(p.gain))
}
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/0207.Course-Schedule/207. Course Schedule.go | leetcode/0207.Course-Schedule/207. Course Schedule.go | package leetcode
// AOV 网的拓扑排序
func canFinish(n int, pre [][]int) bool {
in := make([]int, n)
frees := make([][]int, n)
next := make([]int, 0, n)
for _, v := range pre {
in[v[0]]++
frees[v[1]] = append(frees[v[1]], v[0])
}
for i := 0; i < n; i++ {
if in[i] == 0 {
next = append(next, i)
}
}
for i := 0; i != len(next); i++ {
c := next[i]
v := frees[c]
for _, vv := range v {
in[vv]--
if in[vv] == 0 {
next = append(next, vv)
}
}
}
return len(next) == n
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0207.Course-Schedule/207. Course Schedule_test.go | leetcode/0207.Course-Schedule/207. Course Schedule_test.go | package leetcode
import (
"fmt"
"testing"
)
type question207 struct {
para207
ans207
}
// para 是参数
// one 代表第一个参数
type para207 struct {
one int
pre [][]int
}
// ans 是答案
// one 代表第一个答案
type ans207 struct {
one bool
}
func Test_Problem207(t *testing.T) {
qs := []question207{
{
para207{2, [][]int{{1, 0}}},
ans207{true},
},
{
para207{2, [][]int{{1, 0}, {0, 1}}},
ans207{false},
},
}
fmt.Printf("------------------------Leetcode Problem 207------------------------\n")
for _, q := range qs {
_, p := q.ans207, q.para207
fmt.Printf("【input】:%v 【output】:%v\n", p, canFinish(p.one, p.pre))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0986.Interval-List-Intersections/986. Interval List Intersections.go | leetcode/0986.Interval-List-Intersections/986. Interval List Intersections.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// Interval define
type Interval = structures.Interval
/**
* Definition for an interval.
* type Interval struct {
* Start int
* End int
* }
*/
func intervalIntersection(A []Interval, B []Interval) []Interval {
res := []Interval{}
for i, j := 0, 0; i < len(A) && j < len(B); {
start := max(A[i].Start, B[j].Start)
end := min(A[i].End, B[j].End)
if start <= end {
res = append(res, Interval{Start: start, End: end})
}
if A[i].End <= B[j].End {
i++
} else {
j++
}
}
return res
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func min(a int, b int) int {
if a > b {
return b
}
return a
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0986.Interval-List-Intersections/986. Interval List Intersections_test.go | leetcode/0986.Interval-List-Intersections/986. Interval List Intersections_test.go | package leetcode
import (
"fmt"
"testing"
)
type question986 struct {
para986
ans986
}
// para 是参数
// one 代表第一个参数
type para986 struct {
one []Interval
two []Interval
}
// ans 是答案
// one 代表第一个答案
type ans986 struct {
one []Interval
}
func Test_Problem986(t *testing.T) {
qs := []question986{
{
para986{[]Interval{{Start: 0, End: 2}, {Start: 5, End: 10}, {Start: 13, End: 23}, {Start: 24, End: 25}},
[]Interval{{Start: 1, End: 5}, {Start: 8, End: 12}, {Start: 15, End: 24}, {Start: 25, End: 26}}},
ans986{[]Interval{{Start: 1, End: 2}, {Start: 5, End: 5}, {Start: 8, End: 10},
{Start: 15, End: 23}, {Start: 24, End: 24}, {Start: 25, End: 25}}},
},
}
fmt.Printf("------------------------Leetcode Problem 986------------------------\n")
for _, q := range qs {
_, p := q.ans986, q.para986
fmt.Printf("【input】:%v 【output】:%v\n", p, intervalIntersection(p.one, p.two))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0091.Decode-Ways/91. Decode Ways_test.go | leetcode/0091.Decode-Ways/91. Decode Ways_test.go | package leetcode
import (
"fmt"
"testing"
)
type question91 struct {
para91
ans91
}
// para 是参数
// one 代表第一个参数
type para91 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans91 struct {
one int
}
func Test_Problem91(t *testing.T) {
qs := []question91{
{
para91{"12"},
ans91{2},
},
{
para91{"226"},
ans91{3},
},
}
fmt.Printf("------------------------Leetcode Problem 91------------------------\n")
for _, q := range qs {
_, p := q.ans91, q.para91
fmt.Printf("【input】:%v 【output】:%v\n", p, numDecodings(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/0091.Decode-Ways/91. Decode Ways.go | leetcode/0091.Decode-Ways/91. Decode Ways.go | package leetcode
func numDecodings(s string) int {
n := len(s)
dp := make([]int, n+1)
dp[0] = 1
for i := 1; i <= n; i++ {
if s[i-1] != '0' {
dp[i] += dp[i-1]
}
if i > 1 && s[i-2] != '0' && (s[i-2]-'0')*10+(s[i-1]-'0') <= 26 {
dp[i] += 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/1337.The-K-Weakest-Rows-in-a-Matrix/1337. The K Weakest Rows in a Matrix.go | leetcode/1337.The-K-Weakest-Rows-in-a-Matrix/1337. The K Weakest Rows in a Matrix.go | package leetcode
func kWeakestRows(mat [][]int, k int) []int {
res := []int{}
for j := 0; j < len(mat[0]); j++ {
for i := 0; i < len(mat); i++ {
if mat[i][j] == 0 && ((j == 0) || (mat[i][j-1] != 0)) {
res = append(res, i)
}
}
}
for i := 0; i < len(mat); i++ {
if mat[i][len(mat[0])-1] == 1 {
res = append(res, i)
}
}
return res[:k]
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1337.The-K-Weakest-Rows-in-a-Matrix/1337. The K Weakest Rows in a Matrix_test.go | leetcode/1337.The-K-Weakest-Rows-in-a-Matrix/1337. The K Weakest Rows in a Matrix_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1337 struct {
para1337
ans1337
}
// para 是参数
// one 代表第一个参数
type para1337 struct {
mat [][]int
k int
}
// ans 是答案
// one 代表第一个答案
type ans1337 struct {
one []int
}
func Test_Problem1337(t *testing.T) {
qs := []question1337{
{
para1337{[][]int{{1, 1, 0, 0, 0}, {1, 1, 1, 1, 0}, {1, 0, 0, 0, 0}, {1, 1, 0, 0, 0}, {1, 1, 1, 1, 1}}, 3},
ans1337{[]int{2, 0, 3}},
},
{
para1337{[][]int{{1, 0, 0, 0}, {1, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 0, 0}}, 2},
ans1337{[]int{0, 2}},
},
}
fmt.Printf("------------------------Leetcode Problem 1337------------------------\n")
for _, q := range qs {
_, p := q.ans1337, q.para1337
fmt.Printf("【input】:%v 【output】:%v\n", p, kWeakestRows(p.mat, 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/0297.Serialize-and-Deserialize-Binary-Tree/297.Serialize and Deserialize Binary Tree_test.go | leetcode/0297.Serialize-and-Deserialize-Binary-Tree/297.Serialize and Deserialize Binary Tree_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question297 struct {
para297
ans297
}
// para 是参数
// one 代表第一个参数
type para297 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans297 struct {
one []int
}
func Test_Problem297(t *testing.T) {
qs := []question297{
{
para297{[]int{}},
ans297{[]int{}},
},
{
para297{[]int{1, 2, 3, -1, -1, 4, 5}},
ans297{[]int{1, 2, 3, -1, -1, 4, 5}},
},
{
para297{[]int{1, 2}},
ans297{[]int{1, 2}},
},
}
fmt.Printf("------------------------Leetcode Problem 297------------------------\n")
for _, q := range qs {
_, p := q.ans297, q.para297
fmt.Printf("【input】:%v ", p)
root := structures.Ints2TreeNode(p.one)
tree297 := Constructor()
serialized := tree297.serialize(root)
fmt.Printf("【output】:%v \n", structures.Tree2Preorder(tree297.deserialize(serialized)))
}
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/0297.Serialize-and-Deserialize-Binary-Tree/297.Serialize and Deserialize Binary Tree.go | leetcode/0297.Serialize-and-Deserialize-Binary-Tree/297.Serialize and Deserialize Binary Tree.go | package leetcode
import (
"strconv"
"strings"
"github.com/halfrost/LeetCode-Go/structures"
)
type TreeNode = structures.TreeNode
type Codec struct {
builder strings.Builder
input []string
}
func Constructor() Codec {
return Codec{}
}
// Serializes a tree to a single string.
func (this *Codec) serialize(root *TreeNode) string {
if root == nil {
this.builder.WriteString("#,")
return ""
}
this.builder.WriteString(strconv.Itoa(root.Val) + ",")
this.serialize(root.Left)
this.serialize(root.Right)
return this.builder.String()
}
// Deserializes your encoded data to tree.
func (this *Codec) deserialize(data string) *TreeNode {
if len(data) == 0 {
return nil
}
this.input = strings.Split(data, ",")
return this.deserializeHelper()
}
func (this *Codec) deserializeHelper() *TreeNode {
if this.input[0] == "#" {
this.input = this.input[1:]
return nil
}
val, _ := strconv.Atoi(this.input[0])
this.input = this.input[1:]
return &TreeNode{
Val: val,
Left: this.deserializeHelper(),
Right: this.deserializeHelper(),
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0268.Missing-Number/268. Missing Number_test.go | leetcode/0268.Missing-Number/268. Missing Number_test.go | package leetcode
import (
"fmt"
"testing"
)
type question268 struct {
para268
ans268
}
// para 是参数
// one 代表第一个参数
type para268 struct {
s []int
}
// ans 是答案
// one 代表第一个答案
type ans268 struct {
one int
}
func Test_Problem268(t *testing.T) {
qs := []question268{
{
para268{[]int{3, 0, 1}},
ans268{2},
},
{
para268{[]int{9, 6, 4, 2, 3, 5, 7, 0, 1}},
ans268{8},
},
}
fmt.Printf("------------------------Leetcode Problem 268------------------------\n")
for _, q := range qs {
_, p := q.ans268, q.para268
fmt.Printf("【input】:%v 【output】:%v\n", p, missingNumber(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/0268.Missing-Number/268. Missing Number.go | leetcode/0268.Missing-Number/268. Missing Number.go | package leetcode
func missingNumber(nums []int) int {
xor, i := 0, 0
for i = 0; i < len(nums); i++ {
xor = xor ^ i ^ nums[i]
}
return xor ^ i
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1576.Replace-All-s-to-Avoid-Consecutive-Repeating-Characters/1576. Replace-All-s-to-Avoid-Consecutive-Repeating-Characters.go | leetcode/1576.Replace-All-s-to-Avoid-Consecutive-Repeating-Characters/1576. Replace-All-s-to-Avoid-Consecutive-Repeating-Characters.go | package leetcode
func modifyString(s string) string {
res := []byte(s)
for i, ch := range res {
if ch == '?' {
for b := byte('a'); b <= 'z'; b++ {
if !(i > 0 && res[i-1] == b || i < len(res)-1 && res[i+1] == b) {
res[i] = b
break
}
}
}
}
return string(res)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1576.Replace-All-s-to-Avoid-Consecutive-Repeating-Characters/1576. Replace-All-s-to-Avoid-Consecutive-Repeating-Characters_test.go | leetcode/1576.Replace-All-s-to-Avoid-Consecutive-Repeating-Characters/1576. Replace-All-s-to-Avoid-Consecutive-Repeating-Characters_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1576 struct {
para1576
ans1576
}
// para 是参数
// one 代表第一个参数
type para1576 struct {
s string
}
// ans 是答案
// one 代表第一个答案
type ans1576 struct {
one string
}
func Test_Problem1576(t *testing.T) {
qs := []question1576{
{
para1576{"?zs"},
ans1576{"azs"},
},
{
para1576{"ubv?w"},
ans1576{"ubvaw"},
},
}
fmt.Printf("------------------------Leetcode Problem 1576------------------------\n")
for _, q := range qs {
_, p := q.ans1576, q.para1576
fmt.Printf("【input】:%v 【output】:%v \n", p, modifyString(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/0735.Asteroid-Collision/735. Asteroid Collision_test.go | leetcode/0735.Asteroid-Collision/735. Asteroid Collision_test.go | package leetcode
import (
"fmt"
"testing"
)
type question735 struct {
para735
ans735
}
// para 是参数
// one 代表第一个参数
type para735 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans735 struct {
one []int
}
func Test_Problem735(t *testing.T) {
qs := []question735{
{
para735{[]int{-1, -1, 1, -2}},
ans735{[]int{-1, -1, -2}},
},
{
para735{[]int{5, 10, -5}},
ans735{[]int{5, 10}},
},
{
para735{[]int{5, 8, -8}},
ans735{[]int{5}},
},
{
para735{[]int{8, -8}},
ans735{[]int{}},
},
{
para735{[]int{10, 2, -5}},
ans735{[]int{10}},
},
{
para735{[]int{-2, -1, 1, 2}},
ans735{[]int{-2, -1, 1, 2}},
},
}
fmt.Printf("------------------------Leetcode Problem 735------------------------\n")
for _, q := range qs {
_, p := q.ans735, q.para735
fmt.Printf("【input】:%v 【output】:%v\n", p, asteroidCollision(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/0735.Asteroid-Collision/735. Asteroid Collision.go | leetcode/0735.Asteroid-Collision/735. Asteroid Collision.go | package leetcode
func asteroidCollision(asteroids []int) []int {
res := []int{}
for _, v := range asteroids {
for len(res) != 0 && res[len(res)-1] > 0 && res[len(res)-1] < -v {
res = res[:len(res)-1]
}
if len(res) == 0 || v > 0 || res[len(res)-1] < 0 {
res = append(res, v)
} else if v < 0 && res[len(res)-1] == -v {
res = res[:len(res)-1]
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1385.Find-the-Distance-Value-Between-Two-Arrays/1385. Find the Distance Value Between Two Arrays.go | leetcode/1385.Find-the-Distance-Value-Between-Two-Arrays/1385. Find the Distance Value Between Two Arrays.go | package leetcode
func findTheDistanceValue(arr1 []int, arr2 []int, d int) int {
res := 0
for i := range arr1 {
for j := range arr2 {
if abs(arr1[i]-arr2[j]) <= d {
break
}
if j == len(arr2)-1 {
res++
}
}
}
return res
}
func abs(a int) int {
if a < 0 {
return -1 * a
}
return a
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1385.Find-the-Distance-Value-Between-Two-Arrays/1385. Find the Distance Value Between Two Arrays_test.go | leetcode/1385.Find-the-Distance-Value-Between-Two-Arrays/1385. Find the Distance Value Between Two Arrays_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1385 struct {
para1385
ans1385
}
// para 是参数
// one 代表第一个参数
type para1385 struct {
arr1 []int
arr2 []int
d int
}
// ans 是答案
// one 代表第一个答案
type ans1385 struct {
one []int
}
func Test_Problem1385(t *testing.T) {
qs := []question1385{
{
para1385{[]int{4, 5, 8}, []int{10, 9, 1, 8}, 2},
ans1385{[]int{2}},
},
{
para1385{[]int{1, 4, 2, 3}, []int{-4, -3, 6, 10, 20, 30}, 3},
ans1385{[]int{2}},
},
{
para1385{[]int{2, 1, 100, 3}, []int{-5, -2, 10, -3, 7}, 6},
ans1385{[]int{1}},
},
}
fmt.Printf("------------------------Leetcode Problem 1385------------------------\n")
for _, q := range qs {
_, p := q.ans1385, q.para1385
fmt.Printf("【input】:%v ", p)
fmt.Printf("【output】:%v \n", findTheDistanceValue(p.arr1, p.arr2, p.d))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/331. Verify Preorder Serialization of a Binary Tree.go | leetcode/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/331. Verify Preorder Serialization of a Binary Tree.go | package leetcode
import "strings"
func isValidSerialization(preorder string) bool {
nodes, diff := strings.Split(preorder, ","), 1
for _, node := range nodes {
diff--
if diff < 0 {
return false
}
if node != "#" {
diff += 2
}
}
return diff == 0
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/331. Verify Preorder Serialization of a Binary Tree_test.go | leetcode/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/331. Verify Preorder Serialization of a Binary Tree_test.go | package leetcode
import (
"fmt"
"testing"
)
type question331 struct {
para331
ans331
}
// para 是参数
// one 代表第一个参数
type para331 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans331 struct {
one bool
}
func Test_Problem331(t *testing.T) {
qs := []question331{
{
para331{"9,3,4,#,#,1,#,#,2,#,6,#,#"},
ans331{true},
},
{
para331{"1,#"},
ans331{false},
},
{
para331{"9,#,#,1"},
ans331{false},
},
}
fmt.Printf("------------------------Leetcode Problem 331------------------------\n")
for _, q := range qs {
_, p := q.ans331, q.para331
fmt.Printf("【input】:%v 【output】:%v\n", p, isValidSerialization(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/0630.Course-Schedule-III/630. Course Schedule III_test.go | leetcode/0630.Course-Schedule-III/630. Course Schedule III_test.go | package leetcode
import (
"fmt"
"testing"
)
type question630 struct {
para630
ans630
}
// para 是参数
// one 代表第一个参数
type para630 struct {
courses [][]int
}
// ans 是答案
// one 代表第一个答案
type ans630 struct {
one int
}
func Test_Problem630(t *testing.T) {
qs := []question630{
{
para630{[][]int{{100, 200}, {200, 1300}, {1000, 1250}, {2000, 3200}}},
ans630{3},
},
}
fmt.Printf("------------------------Leetcode Problem 630------------------------\n")
for _, q := range qs {
_, p := q.ans630, q.para630
fmt.Printf("【input】:%v 【output】:%v\n", p, scheduleCourse(p.courses))
}
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/0630.Course-Schedule-III/630. Course Schedule III.go | leetcode/0630.Course-Schedule-III/630. Course Schedule III.go | package leetcode
import (
"container/heap"
"sort"
)
func scheduleCourse(courses [][]int) int {
sort.Slice(courses, func(i, j int) bool {
return courses[i][1] < courses[j][1]
})
maxHeap, time := &Schedule{}, 0
heap.Init(maxHeap)
for _, c := range courses {
if time+c[0] <= c[1] {
time += c[0]
heap.Push(maxHeap, c[0])
} else if (*maxHeap).Len() > 0 && (*maxHeap)[0] > c[0] {
time -= heap.Pop(maxHeap).(int) - c[0]
heap.Push(maxHeap, c[0])
}
}
return (*maxHeap).Len()
}
type Schedule []int
func (s Schedule) Len() int { return len(s) }
func (s Schedule) Less(i, j int) bool { return s[i] > s[j] }
func (s Schedule) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s *Schedule) Pop() interface{} {
n := len(*s)
t := (*s)[n-1]
*s = (*s)[:n-1]
return t
}
func (s *Schedule) Push(x interface{}) {
*s = append(*s, x.(int))
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1201.Ugly-Number-III/1201. Ugly Number III_test.go | leetcode/1201.Ugly-Number-III/1201. Ugly Number III_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1201 struct {
para1201
ans1201
}
// para 是参数
// one 代表第一个参数
type para1201 struct {
n int
a int
b int
c int
}
// ans 是答案
// one 代表第一个答案
type ans1201 struct {
one int
}
func Test_Problem1201(t *testing.T) {
qs := []question1201{
{
para1201{3, 2, 3, 5},
ans1201{4},
},
{
para1201{4, 2, 3, 4},
ans1201{6},
},
{
para1201{5, 2, 11, 13},
ans1201{10},
},
{
para1201{1000000000, 2, 217983653, 336916467},
ans1201{1999999984},
},
}
fmt.Printf("------------------------Leetcode Problem 1201------------------------\n")
for _, q := range qs {
_, p := q.ans1201, q.para1201
fmt.Printf("【input】:%v 【output】:%v\n", p, nthUglyNumber(p.n, p.a, p.b, p.c))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1201.Ugly-Number-III/1201. Ugly Number III.go | leetcode/1201.Ugly-Number-III/1201. Ugly Number III.go | package leetcode
func nthUglyNumber(n int, a int, b int, c int) int {
low, high := int64(0), int64(2*1e9)
for low < high {
mid := low + (high-low)>>1
if calNthCount(mid, int64(a), int64(b), int64(c)) < int64(n) {
low = mid + 1
} else {
high = mid
}
}
return int(low)
}
func calNthCount(num, a, b, c int64) int64 {
ab, bc, ac := a*b/gcd(a, b), b*c/gcd(b, c), a*c/gcd(a, c)
abc := a * bc / gcd(a, bc)
return num/a + num/b + num/c - num/ab - num/bc - num/ac + num/abc
}
func gcd(a, b int64) int64 {
for b != 0 {
a, b = b, 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/1614.Maximum-Nesting-Depth-of-the-Parentheses/1614. Maximum Nesting Depth of the Parentheses.go | leetcode/1614.Maximum-Nesting-Depth-of-the-Parentheses/1614. Maximum Nesting Depth of the Parentheses.go | package leetcode
func maxDepth(s string) int {
res, cur := 0, 0
for _, c := range s {
if c == '(' {
cur++
res = max(res, cur)
} else if c == ')' {
cur--
}
}
return res
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1614.Maximum-Nesting-Depth-of-the-Parentheses/1614. Maximum Nesting Depth of the Parentheses_test.go | leetcode/1614.Maximum-Nesting-Depth-of-the-Parentheses/1614. Maximum Nesting Depth of the Parentheses_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1614 struct {
para1614
ans1614
}
// para 是参数
// one 代表第一个参数
type para1614 struct {
p string
}
// ans 是答案
// one 代表第一个答案
type ans1614 struct {
one int
}
func Test_Problem1614(t *testing.T) {
qs := []question1614{
{
para1614{"(1+(2*3)+((8)/4))+1"},
ans1614{3},
},
{
para1614{"(1)+((2))+(((3)))"},
ans1614{3},
},
{
para1614{"1+(2*3)/(2-1)"},
ans1614{1},
},
{
para1614{"1"},
ans1614{0},
},
}
fmt.Printf("------------------------Leetcode Problem 1614------------------------\n")
for _, q := range qs {
_, p := q.ans1614, q.para1614
fmt.Printf("【input】:%v 【output】:%v \n", p, maxDepth(p.p))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0393.UTF-8-Validation/393. UTF-8 Validation.go | leetcode/0393.UTF-8-Validation/393. UTF-8 Validation.go | package leetcode
func validUtf8(data []int) bool {
count := 0
for _, d := range data {
if count == 0 {
if d >= 248 { // 11111000 = 248
return false
} else if d >= 240 { // 11110000 = 240
count = 3
} else if d >= 224 { // 11100000 = 224
count = 2
} else if d >= 192 { // 11000000 = 192
count = 1
} else if d > 127 { // 01111111 = 127
return false
}
} else {
if d <= 127 || d >= 192 {
return false
}
count--
}
}
return count == 0
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0393.UTF-8-Validation/393. UTF-8 Validation_test.go | leetcode/0393.UTF-8-Validation/393. UTF-8 Validation_test.go | package leetcode
import (
"fmt"
"testing"
)
type question393 struct {
para393
ans393
}
// para 是参数
// one 代表第一个参数
type para393 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans393 struct {
one bool
}
func Test_Problem393(t *testing.T) {
qs := []question393{
{
para393{[]int{197, 130, 1}},
ans393{true},
},
{
para393{[]int{235, 140, 4}},
ans393{false},
},
}
fmt.Printf("------------------------Leetcode Problem 393------------------------\n")
for _, q := range qs {
_, p := q.ans393, q.para393
fmt.Printf("【input】:%v 【output】:%v\n", p, validUtf8(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/1603.Design-Parking-System/1603. Design Parking System.go | leetcode/1603.Design-Parking-System/1603. Design Parking System.go | package leetcode
type ParkingSystem struct {
Big int
Medium int
Small int
}
func Constructor(big int, medium int, small int) ParkingSystem {
return ParkingSystem{
Big: big,
Medium: medium,
Small: small,
}
}
func (this *ParkingSystem) AddCar(carType int) bool {
switch carType {
case 1:
{
if this.Big > 0 {
this.Big--
return true
}
return false
}
case 2:
{
if this.Medium > 0 {
this.Medium--
return true
}
return false
}
case 3:
{
if this.Small > 0 {
this.Small--
return true
}
return false
}
}
return false
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* obj := Constructor(big, medium, small);
* param_1 := obj.AddCar(carType);
*/
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1603.Design-Parking-System/1603. Design Parking System_test.go | leetcode/1603.Design-Parking-System/1603. Design Parking System_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem1603(t *testing.T) {
obj := Constructor(1, 1, 0)
fmt.Printf("obj = %v\n", obj)
fmt.Printf("obj = %v\n", obj.AddCar(1))
fmt.Printf("obj = %v\n", obj.AddCar(2))
fmt.Printf("obj = %v\n", obj.AddCar(3))
fmt.Printf("obj = %v\n", obj.AddCar(1))
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0152.Maximum-Product-Subarray/152. Maximum Product Subarray.go | leetcode/0152.Maximum-Product-Subarray/152. Maximum Product Subarray.go | package leetcode
func maxProduct(nums []int) int {
minimum, maximum, res := nums[0], nums[0], nums[0]
for i := 1; i < len(nums); i++ {
if nums[i] < 0 {
maximum, minimum = minimum, maximum
}
maximum = max(nums[i], maximum*nums[i])
minimum = min(nums[i], minimum*nums[i])
res = max(res, maximum)
}
return res
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func min(a int, b int) int {
if a > b {
return b
}
return a
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0152.Maximum-Product-Subarray/152. Maximum Product Subarray_test.go | leetcode/0152.Maximum-Product-Subarray/152. Maximum Product Subarray_test.go | package leetcode
import (
"fmt"
"testing"
)
type question152 struct {
para152
ans152
}
// para 是参数
// one 代表第一个参数
type para152 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans152 struct {
one int
}
func Test_Problem152(t *testing.T) {
qs := []question152{
{
para152{[]int{-2}},
ans152{-2},
},
{
para152{[]int{3, -1, 4}},
ans152{4},
},
{
para152{[]int{0}},
ans152{0},
},
{
para152{[]int{2, 3, -2, 4}},
ans152{6},
},
{
para152{[]int{-2, 0, -1}},
ans152{0},
},
}
fmt.Printf("------------------------Leetcode Problem 152------------------------\n")
for _, q := range qs {
_, p := q.ans152, q.para152
fmt.Printf("【input】:%v 【output】:%v\n", p, maxProduct(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/0416.Partition-Equal-Subset-Sum/416. Partition Equal Subset Sum.go | leetcode/0416.Partition-Equal-Subset-Sum/416. Partition Equal Subset Sum.go | package leetcode
func canPartition(nums []int) bool {
sum := 0
for _, v := range nums {
sum += v
}
if sum%2 != 0 {
return false
}
// C = half sum
n, C, dp := len(nums), sum/2, make([]bool, sum/2+1)
for i := 0; i <= C; i++ {
dp[i] = (nums[0] == i)
}
for i := 1; i < n; i++ {
for j := C; j >= nums[i]; j-- {
dp[j] = dp[j] || dp[j-nums[i]]
}
}
return dp[C]
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0416.Partition-Equal-Subset-Sum/416. Partition Equal Subset Sum_test.go | leetcode/0416.Partition-Equal-Subset-Sum/416. Partition Equal Subset Sum_test.go | package leetcode
import (
"fmt"
"testing"
)
type question416 struct {
para416
ans416
}
// para 是参数
// one 代表第一个参数
type para416 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans416 struct {
one bool
}
func Test_Problem416(t *testing.T) {
qs := []question416{
{
para416{[]int{1, 5, 11, 5}},
ans416{true},
},
{
para416{[]int{1, 2, 3, 5}},
ans416{false},
},
{
para416{[]int{1, 2, 5}},
ans416{false},
},
}
fmt.Printf("------------------------Leetcode Problem 416------------------------\n")
for _, q := range qs {
_, p := q.ans416, q.para416
fmt.Printf("【input】:%v 【output】:%v\n", p, canPartition(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/1275.Find-Winner-on-a-Tic-Tac-Toe-Game/1275. Find Winner on a Tic Tac Toe Game_test.go | leetcode/1275.Find-Winner-on-a-Tic-Tac-Toe-Game/1275. Find Winner on a Tic Tac Toe Game_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1275 struct {
para1275
ans1275
}
// para 是参数
// one 代表第一个参数
type para1275 struct {
one [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1275 struct {
one string
}
func Test_Problem1275(t *testing.T) {
qs := []question1275{
{
para1275{[][]int{{0, 0}, {2, 0}, {1, 1}, {2, 1}, {2, 2}}},
ans1275{"A"},
},
{
para1275{[][]int{{0, 0}, {1, 1}, {0, 1}, {0, 2}, {1, 0}, {2, 0}}},
ans1275{"B"},
},
{
para1275{[][]int{{0, 0}, {1, 1}, {2, 0}, {1, 0}, {1, 2}, {2, 1}, {0, 1}, {0, 2}, {2, 2}}},
ans1275{"Draw"},
},
{
para1275{[][]int{{0, 0}, {1, 1}}},
ans1275{"Pending"},
},
}
fmt.Printf("------------------------Leetcode Problem 1275------------------------\n")
for _, q := range qs {
_, p := q.ans1275, q.para1275
fmt.Printf("【input】:%v 【output】:%v\n", p, tictactoe(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/1275.Find-Winner-on-a-Tic-Tac-Toe-Game/1275. Find Winner on a Tic Tac Toe Game.go | leetcode/1275.Find-Winner-on-a-Tic-Tac-Toe-Game/1275. Find Winner on a Tic Tac Toe Game.go | package leetcode
func tictactoe(moves [][]int) string {
board := [3][3]byte{}
for i := 0; i < len(moves); i++ {
if i%2 == 0 {
board[moves[i][0]][moves[i][1]] = 'X'
} else {
board[moves[i][0]][moves[i][1]] = 'O'
}
}
for i := 0; i < 3; i++ {
if board[i][0] == 'X' && board[i][1] == 'X' && board[i][2] == 'X' {
return "A"
}
if board[i][0] == 'O' && board[i][1] == 'O' && board[i][2] == 'O' {
return "B"
}
if board[0][i] == 'X' && board[1][i] == 'X' && board[2][i] == 'X' {
return "A"
}
if board[0][i] == 'O' && board[1][i] == 'O' && board[2][i] == 'O' {
return "B"
}
}
if board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' {
return "A"
}
if board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' {
return "B"
}
if board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X' {
return "A"
}
if board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O' {
return "B"
}
if len(moves) < 9 {
return "Pending"
}
return "Draw"
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0507.Perfect-Number/507. Perfect Number_test.go | leetcode/0507.Perfect-Number/507. Perfect Number_test.go | package leetcode
import (
"fmt"
"testing"
)
type question507 struct {
para507
ans507
}
// para 是参数
// one 代表第一个参数
type para507 struct {
num int
}
// ans 是答案
// one 代表第一个答案
type ans507 struct {
one bool
}
func Test_Problem507(t *testing.T) {
qs := []question507{
{
para507{28},
ans507{true},
},
{
para507{496},
ans507{true},
},
{
para507{500},
ans507{false},
},
// 如需多个测试,可以复制上方元素。
}
fmt.Printf("------------------------Leetcode Problem 507------------------------\n")
for _, q := range qs {
_, p := q.ans507, q.para507
fmt.Printf("【input】:%v 【output】:%v\n", p, checkPerfectNumber(p.num))
}
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/0507.Perfect-Number/507. Perfect Number.go | leetcode/0507.Perfect-Number/507. Perfect Number.go | package leetcode
import "math"
// 方法一
func checkPerfectNumber(num int) bool {
if num <= 1 {
return false
}
sum, bound := 1, int(math.Sqrt(float64(num)))+1
for i := 2; i < bound; i++ {
if num%i != 0 {
continue
}
corrDiv := num / i
sum += corrDiv + i
}
return sum == num
}
// 方法二 打表
func checkPerfectNumber_(num int) bool {
return num == 6 || num == 28 || num == 496 || num == 8128 || num == 33550336
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0234.Palindrome-Linked-List/234. Palindrome Linked List_test.go | leetcode/0234.Palindrome-Linked-List/234. Palindrome Linked List_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question234 struct {
para234
ans234
}
// para 是参数
// one 代表第一个参数
type para234 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans234 struct {
one bool
}
func Test_Problem234(t *testing.T) {
qs := []question234{
{
para234{[]int{1, 1, 2, 2, 3, 4, 4, 4}},
ans234{false},
},
{
para234{[]int{1, 1, 1, 1, 1, 1}},
ans234{true},
},
{
para234{[]int{1, 2, 2, 1, 3}},
ans234{false},
},
{
para234{[]int{1}},
ans234{true},
},
{
para234{[]int{}},
ans234{true},
},
{
para234{[]int{1, 2, 2, 2, 2, 1}},
ans234{true},
},
{
para234{[]int{1, 2, 2, 3, 3, 3, 3, 2, 2, 1}},
ans234{true},
},
{
para234{[]int{1, 2}},
ans234{false},
},
{
para234{[]int{1, 0, 1}},
ans234{true},
},
{
para234{[]int{1, 1, 2, 1}},
ans234{false},
},
}
fmt.Printf("------------------------Leetcode Problem 234------------------------\n")
for _, q := range qs {
_, p := q.ans234, q.para234
fmt.Printf("【input】:%v 【output】:%v\n", p, isPalindrome(structures.Ints2List(p.one)))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0234.Palindrome-Linked-List/234. Palindrome Linked List.go | leetcode/0234.Palindrome-Linked-List/234. Palindrome Linked List.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
// 解法一
func isPalindrome(head *ListNode) bool {
slice := []int{}
for head != nil {
slice = append(slice, head.Val)
head = head.Next
}
for i, j := 0, len(slice)-1; i < j; {
if slice[i] != slice[j] {
return false
}
i++
j--
}
return true
}
// 解法二
// 此题和 143 题 Reorder List 思路基本一致
func isPalindrome1(head *ListNode) bool {
if head == nil || head.Next == nil {
return true
}
res := true
// 寻找中间结点
p1 := head
p2 := head
for p2.Next != nil && p2.Next.Next != nil {
p1 = p1.Next
p2 = p2.Next.Next
}
// 反转链表后半部分 1->2->3->4->5->6 to 1->2->3->6->5->4
preMiddle := p1
preCurrent := p1.Next
for preCurrent.Next != nil {
current := preCurrent.Next
preCurrent.Next = current.Next
current.Next = preMiddle.Next
preMiddle.Next = current
}
// 扫描表,判断是否是回文
p1 = head
p2 = preMiddle.Next
// fmt.Printf("p1 = %v p2 = %v preMiddle = %v head = %v\n", p1.Val, p2.Val, preMiddle.Val, L2ss(head))
for p1 != preMiddle {
// fmt.Printf("*****p1 = %v p2 = %v preMiddle = %v head = %v\n", p1, p2, preMiddle, L2ss(head))
if p1.Val == p2.Val {
p1 = p1.Next
p2 = p2.Next
// fmt.Printf("-------p1 = %v p2 = %v preMiddle = %v head = %v\n", p1, p2, preMiddle, L2ss(head))
} else {
res = false
break
}
}
if p1 == preMiddle {
if p2 != nil && p1.Val != p2.Val {
return false
}
}
return res
}
// L2ss define
func L2ss(head *ListNode) []int {
res := []int{}
for head != nil {
res = append(res, head.Val)
head = head.Next
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0034.Find-First-and-Last-Position-of-Element-in-Sorted-Array/34. Find First and Last Position of Element in Sorted Array_test.go | leetcode/0034.Find-First-and-Last-Position-of-Element-in-Sorted-Array/34. Find First and Last Position of Element in Sorted Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question34 struct {
para34
ans34
}
// para 是参数
// one 代表第一个参数
type para34 struct {
nums []int
target int
}
// ans 是答案
// one 代表第一个答案
type ans34 struct {
one []int
}
func Test_Problem34(t *testing.T) {
qs := []question34{
{
para34{[]int{5, 7, 7, 8, 8, 10}, 8},
ans34{[]int{3, 4}},
},
{
para34{[]int{5, 7, 7, 8, 8, 10}, 6},
ans34{[]int{-1, -1}},
},
}
fmt.Printf("------------------------Leetcode Problem 34------------------------\n")
for _, q := range qs {
_, p := q.ans34, q.para34
fmt.Printf("【input】:%v 【output】:%v\n", p, searchRange(p.nums, 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/0034.Find-First-and-Last-Position-of-Element-in-Sorted-Array/34. Find First and Last Position of Element in Sorted Array.go | leetcode/0034.Find-First-and-Last-Position-of-Element-in-Sorted-Array/34. Find First and Last Position of Element in Sorted Array.go | package leetcode
func searchRange(nums []int, target int) []int {
return []int{searchFirstEqualElement(nums, target), searchLastEqualElement(nums, target)}
}
// 二分查找第一个与 target 相等的元素,时间复杂度 O(logn)
func searchFirstEqualElement(nums []int, target int) int {
low, high := 0, len(nums)-1
for low <= high {
mid := low + ((high - low) >> 1)
if nums[mid] > target {
high = mid - 1
} else if nums[mid] < target {
low = mid + 1
} else {
if (mid == 0) || (nums[mid-1] != target) { // 找到第一个与 target 相等的元素
return mid
}
high = mid - 1
}
}
return -1
}
// 二分查找最后一个与 target 相等的元素,时间复杂度 O(logn)
func searchLastEqualElement(nums []int, target int) int {
low, high := 0, len(nums)-1
for low <= high {
mid := low + ((high - low) >> 1)
if nums[mid] > target {
high = mid - 1
} else if nums[mid] < target {
low = mid + 1
} else {
if (mid == len(nums)-1) || (nums[mid+1] != target) { // 找到最后一个与 target 相等的元素
return mid
}
low = mid + 1
}
}
return -1
}
// 二分查找第一个大于等于 target 的元素,时间复杂度 O(logn)
func searchFirstGreaterElement(nums []int, target int) int {
low, high := 0, len(nums)-1
for low <= high {
mid := low + ((high - low) >> 1)
if nums[mid] >= target {
if (mid == 0) || (nums[mid-1] < target) { // 找到第一个大于等于 target 的元素
return mid
}
high = mid - 1
} else {
low = mid + 1
}
}
return -1
}
// 二分查找最后一个小于等于 target 的元素,时间复杂度 O(logn)
func searchLastLessElement(nums []int, target int) int {
low, high := 0, len(nums)-1
for low <= high {
mid := low + ((high - low) >> 1)
if nums[mid] <= target {
if (mid == len(nums)-1) || (nums[mid+1] > target) { // 找到最后一个小于等于 target 的元素
return mid
}
low = mid + 1
} else {
high = mid - 1
}
}
return -1
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0709.To-Lower-Case/709. To Lower Case_test.go | leetcode/0709.To-Lower-Case/709. To Lower Case_test.go | package leetcode
import (
"fmt"
"testing"
)
type question709 struct {
para709
ans709
}
// para 是参数
// one 代表第一个参数
type para709 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans709 struct {
one string
}
func Test_Problem709(t *testing.T) {
qs := []question709{
{
para709{"Hello"},
ans709{"hello"},
},
{
para709{"here"},
ans709{"here"},
},
{
para709{"LOVELY"},
ans709{"lovely"},
},
}
fmt.Printf("------------------------Leetcode Problem 709------------------------\n")
for _, q := range qs {
_, p := q.ans709, q.para709
fmt.Printf("【input】:%v 【output】:%v\n", p, toLowerCase(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/0709.To-Lower-Case/709. To Lower Case.go | leetcode/0709.To-Lower-Case/709. To Lower Case.go | package leetcode
func toLowerCase(s string) string {
runes := []rune(s)
diff := 'a' - 'A'
for i := 0; i < len(s); i++ {
if runes[i] >= 'A' && runes[i] <= 'Z' {
runes[i] += diff
}
}
return string(runes)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1353.Maximum-Number-of-Events-That-Can-Be-Attended/1353. Maximum Number of Events That Can Be Attended.go | leetcode/1353.Maximum-Number-of-Events-That-Can-Be-Attended/1353. Maximum Number of Events That Can Be Attended.go | package leetcode
import (
"sort"
)
func maxEvents(events [][]int) int {
sort.Slice(events, func(i, j int) bool {
if events[i][0] == events[j][0] {
return events[i][1] < events[j][1]
}
return events[i][0] < events[j][0]
})
attended, current := 1, events[0]
for i := 1; i < len(events); i++ {
prev, event := events[i-1], events[i]
if event[0] == prev[0] && event[1] == prev[1] && event[1] == event[0] {
continue
}
start, end := max(current[0], event[0]-1), max(current[1], event[1])
if end-start > 0 {
current[0] = start + 1
current[1] = end
attended++
}
}
return attended
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1353.Maximum-Number-of-Events-That-Can-Be-Attended/1353. Maximum Number of Events That Can Be Attended_test.go | leetcode/1353.Maximum-Number-of-Events-That-Can-Be-Attended/1353. Maximum Number of Events That Can Be Attended_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1353 struct {
para1353
ans1353
}
// para 是参数
// one 代表第一个参数
type para1353 struct {
events [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1353 struct {
one int
}
func Test_Problem1353(t *testing.T) {
qs := []question1353{
{
para1353{[][]int{{1, 2}, {2, 3}, {3, 4}}},
ans1353{3},
},
{
para1353{[][]int{{1, 4}, {4, 4}, {2, 2}, {3, 4}, {1, 1}}},
ans1353{4},
},
{
para1353{[][]int{{1, 100000}}},
ans1353{1},
},
{
para1353{[][]int{{1, 1}, {2, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}}},
ans1353{7},
},
{
para1353{[][]int{{1, 2}, {2, 2}, {3, 3}, {3, 4}, {3, 4}}},
ans1353{4},
},
{
para1353{[][]int{{1, 10}, {2, 2}, {2, 2}, {2, 2}, {2, 2}}},
ans1353{2},
},
}
fmt.Printf("------------------------Leetcode Problem 1353------------------------\n")
for _, q := range qs {
_, p := q.ans1353, q.para1353
fmt.Printf("【input】:%v 【output】:%v\n", p, maxEvents(p.events))
}
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/0832.Flipping-an-Image/832. Flipping an Image_test.go | leetcode/0832.Flipping-an-Image/832. Flipping an Image_test.go | package leetcode
import (
"fmt"
"testing"
)
type question832 struct {
para832
ans832
}
// para 是参数
// one 代表第一个参数
type para832 struct {
A [][]int
}
// ans 是答案
// one 代表第一个答案
type ans832 struct {
one [][]int
}
func Test_Problem832(t *testing.T) {
qs := []question832{
{
para832{[][]int{{1, 1, 0}, {1, 0, 1}, {0, 0, 0}}},
ans832{[][]int{{1, 0, 0}, {0, 1, 0}, {1, 1, 1}}},
},
{
para832{[][]int{{1, 1, 0, 0}, {1, 0, 0, 1}, {0, 1, 1, 1}, {1, 0, 1, 0}}},
ans832{[][]int{{1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 1}, {1, 0, 1, 0}}},
},
{
para832{[][]int{{1, 1, 1}, {1, 1, 1}, {0, 0, 0}}},
ans832{[][]int{{0, 0, 0}, {0, 0, 0}, {1, 1, 1}}},
},
}
fmt.Printf("------------------------Leetcode Problem 832------------------------\n")
for _, q := range qs {
_, p := q.ans832, q.para832
fmt.Printf("【input】:%v 【output】:%v\n", p, flipAndInvertImage(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/0832.Flipping-an-Image/832. Flipping an Image.go | leetcode/0832.Flipping-an-Image/832. Flipping an Image.go | package leetcode
func flipAndInvertImage(A [][]int) [][]int {
for i := 0; i < len(A); i++ {
for a, b := 0, len(A[i])-1; a < b; a, b = a+1, b-1 {
A[i][a], A[i][b] = A[i][b], A[i][a]
}
for a := 0; a < len(A[i]); a++ {
A[i][a] = (A[i][a] + 1) % 2
}
}
return A
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/1464. Maximum Product of Two Elements in an Array_test.go | leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/1464. Maximum Product of Two Elements in an Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1464 struct {
para1464
ans1464
}
// para 是参数
// one 代表第一个参数
type para1464 struct {
nums []int
}
// ans 是答案
// one 代表第一个答案
type ans1464 struct {
one int
}
func Test_Problem1464(t *testing.T) {
qs := []question1464{
{
para1464{[]int{3, 4, 5, 2}},
ans1464{12},
},
{
para1464{[]int{1, 5, 4, 5}},
ans1464{16},
},
{
para1464{[]int{3, 7}},
ans1464{12},
},
{
para1464{[]int{1}},
ans1464{0},
},
}
fmt.Printf("------------------------Leetcode Problem 1464------------------------\n")
for _, q := range qs {
_, p := q.ans1464, q.para1464
fmt.Printf("【input】:%v ", p)
fmt.Printf("【output】:%v \n", maxProduct(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/1464.Maximum-Product-of-Two-Elements-in-an-Array/1464. Maximum Product of Two Elements in an Array.go | leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/1464. Maximum Product of Two Elements in an Array.go | package leetcode
func maxProduct(nums []int) int {
max1, max2 := 0, 0
for _, num := range nums {
if num >= max1 {
max2 = max1
max1 = num
} else if num <= max1 && num >= max2 {
max2 = num
}
}
return (max1 - 1) * (max2 - 1)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0093.Restore-IP-Addresses/93. Restore IP Addresses_test.go | leetcode/0093.Restore-IP-Addresses/93. Restore IP Addresses_test.go | package leetcode
import (
"fmt"
"testing"
)
type question93 struct {
para93
ans93
}
// para 是参数
// one 代表第一个参数
type para93 struct {
s string
}
// ans 是答案
// one 代表第一个答案
type ans93 struct {
one []string
}
func Test_Problem93(t *testing.T) {
qs := []question93{
{
para93{"25525511135"},
ans93{[]string{"255.255.11.135", "255.255.111.35"}},
},
{
para93{"0000"},
ans93{[]string{"0.0.0.0"}},
},
{
para93{"010010"},
ans93{[]string{"0.10.0.10", "0.100.1.0"}},
},
}
fmt.Printf("------------------------Leetcode Problem 93------------------------\n")
for _, q := range qs {
_, p := q.ans93, q.para93
fmt.Printf("【input】:%v 【output】:%v\n", p, restoreIPAddresses(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/0093.Restore-IP-Addresses/93. Restore IP Addresses.go | leetcode/0093.Restore-IP-Addresses/93. Restore IP Addresses.go | package leetcode
import (
"strconv"
)
func restoreIPAddresses(s string) []string {
if s == "" {
return []string{}
}
res, ip := []string{}, []int{}
dfs(s, 0, ip, &res)
return res
}
func dfs(s string, index int, ip []int, res *[]string) {
if index == len(s) {
if len(ip) == 4 {
*res = append(*res, getString(ip))
}
return
}
if index == 0 {
num, _ := strconv.Atoi(string(s[0]))
ip = append(ip, num)
dfs(s, index+1, ip, res)
} else {
num, _ := strconv.Atoi(string(s[index]))
next := ip[len(ip)-1]*10 + num
if next <= 255 && ip[len(ip)-1] != 0 {
ip[len(ip)-1] = next
dfs(s, index+1, ip, res)
ip[len(ip)-1] /= 10
}
if len(ip) < 4 {
ip = append(ip, num)
dfs(s, index+1, ip, res)
ip = ip[:len(ip)-1]
}
}
}
func getString(ip []int) string {
res := strconv.Itoa(ip[0])
for i := 1; i < len(ip); i++ {
res += "." + strconv.Itoa(ip[i])
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0901.Online-Stock-Span/901. Online Stock Span.go | leetcode/0901.Online-Stock-Span/901. Online Stock Span.go | package leetcode
import "fmt"
// node pair
type Node struct {
Val int
res int
}
// slice
type StockSpanner struct {
Item []Node
}
func Constructor901() StockSpanner {
stockSpanner := StockSpanner{make([]Node, 0)}
return stockSpanner
}
// need refactor later
func (this *StockSpanner) Next(price int) int {
res := 1
if len(this.Item) == 0 {
this.Item = append(this.Item, Node{price, res})
return res
}
for len(this.Item) > 0 && this.Item[len(this.Item)-1].Val <= price {
res = res + this.Item[len(this.Item)-1].res
this.Item = this.Item[:len(this.Item)-1]
}
this.Item = append(this.Item, Node{price, res})
fmt.Printf("this.Item = %v\n", this.Item)
return res
}
/**
* Your StockSpanner object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Next(price);
*/
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0901.Online-Stock-Span/901. Online Stock Span_test.go | leetcode/0901.Online-Stock-Span/901. Online Stock Span_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem901(t *testing.T) {
obj := Constructor901()
fmt.Printf("obj = %v\n", obj)
param1 := obj.Next(100)
fmt.Printf("param_1 = %v\n", param1)
param2 := obj.Next(80)
fmt.Printf("param_2 = %v\n", param2)
param3 := obj.Next(60)
fmt.Printf("param_3 = %v\n", param3)
param4 := obj.Next(70)
fmt.Printf("param_4 = %v\n", param4)
param5 := obj.Next(60)
fmt.Printf("param_5 = %v\n", param5)
param6 := obj.Next(75)
fmt.Printf("param_6 = %v\n", param6)
param7 := obj.Next(85)
fmt.Printf("param_7 = %v\n", param7)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0019.Remove-Nth-Node-From-End-of-List/19. Remove Nth Node From End of List_test.go | leetcode/0019.Remove-Nth-Node-From-End-of-List/19. Remove Nth Node From End of List_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question19 struct {
para19
ans19
}
// para 是参数
// one 代表第一个参数
type para19 struct {
one []int
n int
}
// ans 是答案
// one 代表第一个答案
type ans19 struct {
one []int
}
func Test_Problem19(t *testing.T) {
qs := []question19{
{
para19{[]int{1}, 3},
ans19{[]int{1}},
},
{
para19{[]int{1, 2}, 2},
ans19{[]int{2}},
},
{
para19{[]int{1}, 1},
ans19{[]int{}},
},
{
para19{[]int{1, 2, 3, 4, 5}, 10},
ans19{[]int{1, 2, 3, 4, 5}},
},
{
para19{[]int{1, 2, 3, 4, 5}, 1},
ans19{[]int{1, 2, 3, 4}},
},
{
para19{[]int{1, 2, 3, 4, 5}, 2},
ans19{[]int{1, 2, 3, 5}},
},
{
para19{[]int{1, 2, 3, 4, 5}, 3},
ans19{[]int{1, 2, 4, 5}},
},
{
para19{[]int{1, 2, 3, 4, 5}, 4},
ans19{[]int{1, 3, 4, 5}},
},
{
para19{[]int{1, 2, 3, 4, 5}, 5},
ans19{[]int{2, 3, 4, 5}},
},
}
fmt.Printf("------------------------Leetcode Problem 19------------------------\n")
for _, q := range qs {
_, p := q.ans19, q.para19
fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(removeNthFromEnd(structures.Ints2List(p.one), 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/0019.Remove-Nth-Node-From-End-of-List/19. Remove Nth Node From End of List.go | leetcode/0019.Remove-Nth-Node-From-End-of-List/19. Remove Nth Node From End of List.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
// 解法一
func removeNthFromEnd(head *ListNode, n int) *ListNode {
dummyHead := &ListNode{Next: head}
preSlow, slow, fast := dummyHead, head, head
for fast != nil {
if n <= 0 {
preSlow = slow
slow = slow.Next
}
n--
fast = fast.Next
}
preSlow.Next = slow.Next
return dummyHead.Next
}
// 解法二
func removeNthFromEnd1(head *ListNode, n int) *ListNode {
if head == nil {
return nil
}
if n <= 0 {
return head
}
current := head
len := 0
for current != nil {
len++
current = current.Next
}
if n > len {
return head
}
if n == len {
current := head
head = head.Next
current.Next = nil
return head
}
current = head
i := 0
for current != nil {
if i == len-n-1 {
deleteNode := current.Next
current.Next = current.Next.Next
deleteNode.Next = nil
break
}
i++
current = current.Next
}
return head
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0389.Find-the-Difference/389. Find the Difference.go | leetcode/0389.Find-the-Difference/389. Find the Difference.go | package leetcode
func findTheDifference(s string, t string) byte {
n, ch := len(t), t[len(t)-1]
for i := 0; i < n-1; i++ {
ch ^= s[i]
ch ^= t[i]
}
return ch
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0389.Find-the-Difference/389. Find the Difference_test.go | leetcode/0389.Find-the-Difference/389. Find the Difference_test.go | package leetcode
import (
"fmt"
"testing"
)
type question389 struct {
para389
ans389
}
// para 是参数
// one 代表第一个参数
type para389 struct {
s string
t string
}
// ans 是答案
// one 代表第一个答案
type ans389 struct {
one byte
}
func Test_Problem389(t *testing.T) {
qs := []question389{
{
para389{"abcd", "abcde"},
ans389{'e'},
},
// 如需多个测试,可以复制上方元素。
}
fmt.Printf("------------------------Leetcode Problem 389------------------------\n")
for _, q := range qs {
_, p := q.ans389, q.para389
fmt.Printf("【input】:%v 【output】:%v\n", p, findTheDifference(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/1234.Replace-the-Substring-for-Balanced-String/1234. Replace the Substring for Balanced String_test.go | leetcode/1234.Replace-the-Substring-for-Balanced-String/1234. Replace the Substring for Balanced String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1234 struct {
para1234
ans1234
}
// para 是参数
// one 代表第一个参数
type para1234 struct {
s string
}
// ans 是答案
// one 代表第一个答案
type ans1234 struct {
one int
}
func Test_Problem1234(t *testing.T) {
qs := []question1234{
{
para1234{"QWER"},
ans1234{0},
},
{
para1234{"QQWE"},
ans1234{1},
},
{
para1234{"QQQW"},
ans1234{2},
},
{
para1234{"QQQQ"},
ans1234{3},
},
{
para1234{"WQWRQQQW"},
ans1234{3},
},
}
fmt.Printf("------------------------Leetcode Problem 1234------------------------\n")
for _, q := range qs {
_, p := q.ans1234, q.para1234
fmt.Printf("【input】:%v 【output】:%v\n", p, balancedString(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/1234.Replace-the-Substring-for-Balanced-String/1234. Replace the Substring for Balanced String.go | leetcode/1234.Replace-the-Substring-for-Balanced-String/1234. Replace the Substring for Balanced String.go | package leetcode
func balancedString(s string) int {
count, k := make([]int, 128), len(s)/4
for _, v := range s {
count[int(v)]++
}
left, right, res := 0, -1, len(s)
for left < len(s) {
if count['Q'] > k || count['W'] > k || count['E'] > k || count['R'] > k {
if right+1 < len(s) {
right++
count[s[right]]--
} else {
break
}
} else {
res = min(res, right-left+1)
count[s[left]]++
left++
}
}
return res
}
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/1439.Find-the-Kth-Smallest-Sum-of-a-Matrix-With-Sorted-Rows/1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows_test.go | leetcode/1439.Find-the-Kth-Smallest-Sum-of-a-Matrix-With-Sorted-Rows/1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1439 struct {
para1439
ans1439
}
// para 是参数
// one 代表第一个参数
type para1439 struct {
mat [][]int
k int
}
// ans 是答案
// one 代表第一个答案
type ans1439 struct {
one int
}
func Test_Problem1439(t *testing.T) {
qs := []question1439{
{
para1439{[][]int{{1, 3, 11}, {2, 4, 6}}, 5},
ans1439{7},
},
{
para1439{[][]int{{1, 3, 11}, {2, 4, 6}}, 9},
ans1439{17},
},
{
para1439{[][]int{{1, 10, 10}, {1, 4, 5}, {2, 3, 6}}, 7},
ans1439{9},
},
{
para1439{[][]int{{1, 1, 10}, {2, 2, 9}}, 7},
ans1439{12},
},
}
fmt.Printf("------------------------Leetcode Problem 1439------------------------\n")
for _, q := range qs {
_, p := q.ans1439, q.para1439
fmt.Printf("【input】:%v 【output】:%v\n", p, kthSmallest(p.mat, 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/1439.Find-the-Kth-Smallest-Sum-of-a-Matrix-With-Sorted-Rows/1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows.go | leetcode/1439.Find-the-Kth-Smallest-Sum-of-a-Matrix-With-Sorted-Rows/1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows.go | package leetcode
import "container/heap"
func kthSmallest(mat [][]int, k int) int {
if len(mat) == 0 || len(mat[0]) == 0 || k == 0 {
return 0
}
prev := mat[0]
for i := 1; i < len(mat); i++ {
prev = kSmallestPairs(prev, mat[i], k)
}
if k < len(prev) {
return -1
}
return prev[k-1]
}
func kSmallestPairs(nums1 []int, nums2 []int, k int) []int {
res := []int{}
if len(nums2) == 0 {
return res
}
pq := newPriorityQueue()
for i := 0; i < len(nums1) && i < k; i++ {
heap.Push(pq, &pddata{
n1: nums1[i],
n2: nums2[0],
n2Idx: 0,
})
}
for pq.Len() > 0 {
i := heap.Pop(pq)
data := i.(*pddata)
res = append(res, data.n1+data.n2)
k--
if k <= 0 {
break
}
idx := data.n2Idx
idx++
if idx >= len(nums2) {
continue
}
heap.Push(pq, &pddata{
n1: data.n1,
n2: nums2[idx],
n2Idx: idx,
})
}
return res
}
type pddata struct {
n1 int
n2 int
n2Idx int
}
type priorityQueue []*pddata
func newPriorityQueue() *priorityQueue {
pq := priorityQueue([]*pddata{})
heap.Init(&pq)
return &pq
}
func (pq priorityQueue) Len() int { return len(pq) }
func (pq priorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
func (pq priorityQueue) Less(i, j int) bool { return pq[i].n1+pq[i].n2 < pq[j].n1+pq[j].n2 }
func (pq *priorityQueue) Pop() interface{} {
old := *pq
val := old[len(old)-1]
old[len(old)-1] = nil
*pq = old[0 : len(old)-1]
return val
}
func (pq *priorityQueue) Push(i interface{}) {
val := i.(*pddata)
*pq = append(*pq, val)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0895.Maximum-Frequency-Stack/895. Maximum Frequency Stack.go | leetcode/0895.Maximum-Frequency-Stack/895. Maximum Frequency Stack.go | package leetcode
type FreqStack struct {
freq map[int]int
group map[int][]int
maxfreq int
}
func Constructor895() FreqStack {
hash := make(map[int]int)
maxHash := make(map[int][]int)
return FreqStack{freq: hash, group: maxHash}
}
func (this *FreqStack) Push(x int) {
if _, ok := this.freq[x]; ok {
this.freq[x]++
} else {
this.freq[x] = 1
}
f := this.freq[x]
if f > this.maxfreq {
this.maxfreq = f
}
this.group[f] = append(this.group[f], x)
}
func (this *FreqStack) Pop() int {
tmp := this.group[this.maxfreq]
x := tmp[len(tmp)-1]
this.group[this.maxfreq] = this.group[this.maxfreq][:len(this.group[this.maxfreq])-1]
this.freq[x]--
if len(this.group[this.maxfreq]) == 0 {
this.maxfreq--
}
return x
}
/**
* Your FreqStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* param_2 := obj.Pop();
*/
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0895.Maximum-Frequency-Stack/895. Maximum Frequency Stack_test.go | leetcode/0895.Maximum-Frequency-Stack/895. Maximum Frequency Stack_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem895(t *testing.T) {
obj := Constructor895()
fmt.Printf("obj = %v\n", obj)
obj.Push(5)
fmt.Printf("obj = %v\n", obj)
obj.Push(7)
fmt.Printf("obj = %v\n", obj)
obj.Push(5)
fmt.Printf("obj = %v\n", obj)
obj.Push(7)
fmt.Printf("obj = %v\n", obj)
obj.Push(4)
fmt.Printf("obj = %v\n", obj)
obj.Push(5)
fmt.Printf("obj = %v\n", obj)
param1 := obj.Pop()
fmt.Printf("param_1 = %v\n", param1)
param1 = obj.Pop()
fmt.Printf("param_1 = %v\n", param1)
param1 = obj.Pop()
fmt.Printf("param_1 = %v\n", param1)
param1 = obj.Pop()
fmt.Printf("param_1 = %v\n", param1)
param1 = obj.Pop()
fmt.Printf("param_1 = %v\n", param1)
param1 = obj.Pop()
fmt.Printf("param_1 = %v\n", param1)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1691.Maximum-Height-by-Stacking-Cuboids/1691. Maximum Height by Stacking Cuboids_test.go | leetcode/1691.Maximum-Height-by-Stacking-Cuboids/1691. Maximum Height by Stacking Cuboids_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1691 struct {
para1691
ans1691
}
// para 是参数
// one 代表第一个参数
type para1691 struct {
cuboids [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1691 struct {
one int
}
func Test_Problem1691(t *testing.T) {
qs := []question1691{
{
para1691{[][]int{{50, 45, 20}, {95, 37, 53}, {45, 23, 12}}},
ans1691{190},
},
{
para1691{[][]int{{38, 25, 45}, {76, 35, 3}}},
ans1691{76},
},
{
para1691{[][]int{{7, 11, 17}, {7, 17, 11}, {11, 7, 17}, {11, 17, 7}, {17, 7, 11}, {17, 11, 7}}},
ans1691{102},
},
}
fmt.Printf("------------------------Leetcode Problem 1691------------------------\n")
for _, q := range qs {
_, p := q.ans1691, q.para1691
fmt.Printf("【input】:%v 【output】:%v\n", p, maxHeight(p.cuboids))
}
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/1691.Maximum-Height-by-Stacking-Cuboids/1691. Maximum Height by Stacking Cuboids.go | leetcode/1691.Maximum-Height-by-Stacking-Cuboids/1691. Maximum Height by Stacking Cuboids.go | package leetcode
import "sort"
func maxHeight(cuboids [][]int) int {
n := len(cuboids)
for i := 0; i < n; i++ {
sort.Ints(cuboids[i]) // 立方体三边内部排序
}
// 立方体排序,先按最短边,再到最长边
sort.Slice(cuboids, func(i, j int) bool {
if cuboids[i][0] != cuboids[j][0] {
return cuboids[i][0] < cuboids[j][0]
}
if cuboids[i][1] != cuboids[j][1] {
return cuboids[i][1] < cuboids[j][1]
}
return cuboids[i][2] < cuboids[j][2]
})
res := 0
dp := make([]int, n)
for i := 0; i < n; i++ {
dp[i] = cuboids[i][2]
res = max(res, dp[i])
}
for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
if cuboids[j][0] <= cuboids[i][0] && cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2] {
dp[i] = max(dp[i], dp[j]+cuboids[i][2])
}
}
res = max(res, dp[i])
}
return res
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0665.Non-decreasing-Array/665. Non-decreasing Array_test.go | leetcode/0665.Non-decreasing-Array/665. Non-decreasing Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question665 struct {
para665
ans665
}
// para 是参数
// one 代表第一个参数
type para665 struct {
nums []int
}
// ans 是答案
// one 代表第一个答案
type ans665 struct {
one bool
}
func Test_Problem665(t *testing.T) {
qs := []question665{
{
para665{[]int{4, 2, 3}},
ans665{true},
},
{
para665{[]int{4, 2, 1}},
ans665{false},
},
}
fmt.Printf("------------------------Leetcode Problem 665------------------------\n")
for _, q := range qs {
_, p := q.ans665, q.para665
fmt.Printf("【input】:%v 【output】:%v\n", p, checkPossibility(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/0665.Non-decreasing-Array/665. Non-decreasing Array.go | leetcode/0665.Non-decreasing-Array/665. Non-decreasing Array.go | package leetcode
func checkPossibility(nums []int) bool {
count := 0
for i := 0; i < len(nums)-1; i++ {
if nums[i] > nums[i+1] {
count++
if count > 1 {
return false
}
if i > 0 && nums[i+1] < nums[i-1] {
nums[i+1] = nums[i]
}
}
}
return true
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0263.Ugly-Number/263. Ugly Number_test.go | leetcode/0263.Ugly-Number/263. Ugly Number_test.go | package leetcode
import (
"fmt"
"testing"
)
type question263 struct {
para263
ans263
}
// para 是参数
// one 代表第一个参数
type para263 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans263 struct {
one bool
}
func Test_Problem263(t *testing.T) {
qs := []question263{
{
para263{6},
ans263{true},
},
{
para263{8},
ans263{true},
},
{
para263{14},
ans263{false},
},
}
fmt.Printf("------------------------Leetcode Problem 263------------------------\n")
for _, q := range qs {
_, p := q.ans263, q.para263
fmt.Printf("【input】:%v 【output】:%v\n", p, isUgly(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/0263.Ugly-Number/263. Ugly Number.go | leetcode/0263.Ugly-Number/263. Ugly Number.go | package leetcode
func isUgly(num int) bool {
if num > 0 {
for _, i := range []int{2, 3, 5} {
for num%i == 0 {
num /= i
}
}
}
return num == 1
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0766.Toeplitz-Matrix/766. Toeplitz Matrix_test.go | leetcode/0766.Toeplitz-Matrix/766. Toeplitz Matrix_test.go | package leetcode
import (
"fmt"
"testing"
)
type question766 struct {
para766
ans766
}
// para 是参数
// one 代表第一个参数
type para766 struct {
A [][]int
}
// ans 是答案
// one 代表第一个答案
type ans766 struct {
B bool
}
func Test_Problem766(t *testing.T) {
qs := []question766{
{
para766{[][]int{{1, 2, 3, 4}, {5, 1, 2, 3}, {9, 5, 1, 2}}},
ans766{true},
},
{
para766{[][]int{{1, 2}, {2, 2}}},
ans766{false},
},
}
fmt.Printf("------------------------Leetcode Problem 766------------------------\n")
for _, q := range qs {
_, p := q.ans766, q.para766
fmt.Printf("【input】:%v 【output】:%v\n", p, isToeplitzMatrix(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/0766.Toeplitz-Matrix/766. Toeplitz Matrix.go | leetcode/0766.Toeplitz-Matrix/766. Toeplitz Matrix.go | package leetcode
func isToeplitzMatrix(matrix [][]int) bool {
rows, columns := len(matrix), len(matrix[0])
for i := 1; i < rows; i++ {
for j := 1; j < columns; j++ {
if matrix[i-1][j-1] != matrix[i][j] {
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/1551.Minimum-Operations-to-Make-Array-Equal/1551. Minimum Operations to Make Array Equal.go | leetcode/1551.Minimum-Operations-to-Make-Array-Equal/1551. Minimum Operations to Make Array Equal.go | package leetcode
func minOperations(n int) int {
return n * n / 4
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1551.Minimum-Operations-to-Make-Array-Equal/1551. Minimum Operations to Make Array Equal_test.go | leetcode/1551.Minimum-Operations-to-Make-Array-Equal/1551. Minimum Operations to Make Array Equal_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1551 struct {
para1551
ans1551
}
// para 是参数
// one 代表第一个参数
type para1551 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans1551 struct {
one int
}
func Test_Problem1551(t *testing.T) {
qs := []question1551{
{
para1551{3},
ans1551{2},
},
{
para1551{6},
ans1551{9},
},
{
para1551{534},
ans1551{71289},
},
}
fmt.Printf("------------------------Leetcode Problem 1551------------------------\n")
for _, q := range qs {
_, p := q.ans1551, q.para1551
fmt.Printf("【input】:%v 【output】:%v \n", p, minOperations(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/1725.Number-Of-Rectangles-That-Can-Form-The-Largest-Square/1725. Number Of Rectangles That Can Form The Largest Square_test.go | leetcode/1725.Number-Of-Rectangles-That-Can-Form-The-Largest-Square/1725. Number Of Rectangles That Can Form The Largest Square_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1725 struct {
para1725
ans1725
}
// para 是参数
// one 代表第一个参数
type para1725 struct {
rectangles [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1725 struct {
one int
}
func Test_Problem1725(t *testing.T) {
qs := []question1725{
{
para1725{[][]int{{5, 8}, {3, 9}, {5, 12}, {16, 5}}},
ans1725{3},
},
{
para1725{[][]int{{2, 3}, {3, 7}, {4, 3}, {3, 7}}},
ans1725{3},
},
}
fmt.Printf("------------------------Leetcode Problem 1725------------------------\n")
for _, q := range qs {
_, p := q.ans1725, q.para1725
fmt.Printf("【input】:%v 【output】:%v\n", p, countGoodRectangles(p.rectangles))
}
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/1725.Number-Of-Rectangles-That-Can-Form-The-Largest-Square/1725. Number Of Rectangles That Can Form The Largest Square.go | leetcode/1725.Number-Of-Rectangles-That-Can-Form-The-Largest-Square/1725. Number Of Rectangles That Can Form The Largest Square.go | package leetcode
func countGoodRectangles(rectangles [][]int) int {
minLength, count := 0, 0
for i := range rectangles {
minSide := 0
if rectangles[i][0] <= rectangles[i][1] {
minSide = rectangles[i][0]
} else {
minSide = rectangles[i][1]
}
if minSide > minLength {
minLength = minSide
count = 1
} else if minSide == minLength {
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/0434.Number-of-Segments-in-a-String/434.Number of Segments in a String_test.go | leetcode/0434.Number-of-Segments-in-a-String/434.Number of Segments in a String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question434 struct {
para434
ans434
}
// s 是参数
type para434 struct {
s string
}
// ans 是答案
type ans434 struct {
ans int
}
func Test_Problem434(t *testing.T) {
qs := []question434{
{
para434{"Hello, my name is John"},
ans434{5},
},
{
para434{"Hello"},
ans434{1},
},
{
para434{"love live! mu'sic forever"},
ans434{4},
},
{
para434{""},
ans434{0},
},
}
fmt.Printf("------------------------Leetcode Problem 434------------------------\n")
for _, q := range qs {
_, p := q.ans434, q.para434
fmt.Printf("【input】:%v 【output】:%v\n", p, countSegments(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/0434.Number-of-Segments-in-a-String/434.Number of Segments in a String.go | leetcode/0434.Number-of-Segments-in-a-String/434.Number of Segments in a String.go | package leetcode
func countSegments(s string) int {
segments := false
cnt := 0
for _, v := range s {
if v == ' ' && segments {
segments = false
cnt += 1
} else if v != ' ' {
segments = true
}
}
if segments {
cnt++
}
return cnt
}
| 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.