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/0258.Add-Digits/258. Add Digits_test.go | leetcode/0258.Add-Digits/258. Add Digits_test.go | package leetcode
import (
"fmt"
"testing"
)
type question258 struct {
para258
ans258
}
// para 是参数
// one 代表第一个参数
type para258 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans258 struct {
one int
}
func Test_Problem258(t *testing.T) {
qs := []question258{
{
para258{38},
ans258{2},
},
{
para258{88},
ans258{7},
},
{
para258{96},
ans258{6},
},
}
fmt.Printf("------------------------Leetcode Problem 258------------------------\n")
for _, q := range qs {
_, p := q.ans258, q.para258
fmt.Printf("【input】:%v 【output】:%v\n", p, addDigits(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/0258.Add-Digits/258. Add Digits.go | leetcode/0258.Add-Digits/258. Add Digits.go | package leetcode
func addDigits(num int) int {
for num > 9 {
cur := 0
for num != 0 {
cur += num % 10
num /= 10
}
num = cur
}
return num
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0493.Reverse-Pairs/493. Reverse Pairs_test.go | leetcode/0493.Reverse-Pairs/493. Reverse Pairs_test.go | package leetcode
import (
"fmt"
"testing"
)
type question493 struct {
para493
ans493
}
// para 是参数
// one 代表第一个参数
type para493 struct {
nums []int
}
// ans 是答案
// one 代表第一个答案
type ans493 struct {
one int
}
func Test_Problem493(t *testing.T) {
qs := []question493{
{
para493{[]int{1, 3, 2, 3, 1}},
ans493{2},
},
{
para493{[]int{9, 8, 7, 4, 7, 2, 3, 8, 7, 0}},
ans493{18},
},
{
para493{[]int{2, 4, 3, 5, 1}},
ans493{3},
},
{
para493{[]int{-5, -5}},
ans493{1},
},
{
para493{[]int{2147483647, 2147483647, -2147483647, -2147483647, -2147483647, 2147483647}},
ans493{9},
},
}
fmt.Printf("------------------------Leetcode Problem 493------------------------\n")
for _, q := range qs {
_, p := q.ans493, q.para493
fmt.Printf("【input】:%v 【output】:%v\n", p, reversePairs(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/0493.Reverse-Pairs/493. Reverse Pairs.go | leetcode/0493.Reverse-Pairs/493. Reverse Pairs.go | package leetcode
import (
"sort"
"github.com/halfrost/LeetCode-Go/template"
)
// 解法一 归并排序 mergesort,时间复杂度 O(n log n)
func reversePairs(nums []int) int {
buf := make([]int, len(nums))
return mergesortCount(nums, buf)
}
func mergesortCount(nums, buf []int) int {
if len(nums) <= 1 {
return 0
}
mid := (len(nums) - 1) / 2
cnt := mergesortCount(nums[:mid+1], buf)
cnt += mergesortCount(nums[mid+1:], buf)
for i, j := 0, mid+1; i < mid+1; i++ { // Note!!! j is increasing.
for ; j < len(nums) && nums[i] <= 2*nums[j]; j++ {
}
cnt += len(nums) - j
}
copy(buf, nums)
for i, j, k := 0, mid+1, 0; k < len(nums); {
if j >= len(nums) || i < mid+1 && buf[i] > buf[j] {
nums[k] = buf[i]
i++
} else {
nums[k] = buf[j]
j++
}
k++
}
return cnt
}
// 解法二 树状数组,时间复杂度 O(n log n)
func reversePairs1(nums []int) (cnt int) {
n := len(nums)
if n <= 1 {
return
}
// 离散化所有下面统计时会出现的元素
allNums := make([]int, 0, 2*n)
for _, v := range nums {
allNums = append(allNums, v, 2*v)
}
sort.Ints(allNums)
k := 1
kth := map[int]int{allNums[0]: k}
for i := 1; i < 2*n; i++ {
if allNums[i] != allNums[i-1] {
k++
kth[allNums[i]] = k
}
}
bit := template.BinaryIndexedTree{}
bit.Init(k)
for i, v := range nums {
cnt += i - bit.Query(kth[2*v])
bit.Add(kth[v], 1)
}
return
}
// 解法三 线段树,时间复杂度 O(n log n)
func reversePairs2(nums []int) int {
if len(nums) < 2 {
return 0
}
st, numsMap, indexMap, numsArray, res := template.SegmentCountTree{}, make(map[int]int, 0), make(map[int]int, 0), []int{}, 0
numsMap[nums[0]] = nums[0]
for _, num := range nums {
numsMap[num] = num
numsMap[2*num+1] = 2*num + 1
}
// numsArray 是 prefixSum 去重之后的版本,利用 numsMap 去重
for _, v := range numsMap {
numsArray = append(numsArray, v)
}
// 排序是为了使得线段树中的区间 left <= right,如果此处不排序,线段树中的区间有很多不合法。
sort.Ints(numsArray)
// 离散化,构建映射关系
for i, n := range numsArray {
indexMap[n] = i
}
numsArray = []int{}
// 离散化,此题如果不离散化,MaxInt32 的数据会使得数字越界。
for i := 0; i < len(indexMap); i++ {
numsArray = append(numsArray, i)
}
// 初始化线段树,节点内的值都赋值为 0,即计数为 0
st.Init(numsArray, func(i, j int) int {
return 0
})
for _, num := range nums {
res += st.Query(indexMap[num*2+1], len(indexMap)-1)
st.UpdateCount(indexMap[num])
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0344.Reverse-String/344. Reverse String.go | leetcode/0344.Reverse-String/344. Reverse String.go | package leetcode
func reverseString(s []byte) {
for i, j := 0, len(s)-1; i < j; {
s[i], s[j] = s[j], s[i]
i++
j--
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0344.Reverse-String/344. Reverse String_test.go | leetcode/0344.Reverse-String/344. Reverse String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question344 struct {
para344
ans344
}
// para 是参数
// one 代表第一个参数
type para344 struct {
one []byte
}
// ans 是答案
// one 代表第一个答案
type ans344 struct {
one []byte
}
func Test_Problem344(t *testing.T) {
qs := []question344{
{
para344{[]byte{'h', 'e', 'l', 'l', 'o'}},
ans344{[]byte{'o', 'l', 'l', 'e', 'h'}},
},
{
para344{[]byte{'H', 'a', 'n', 'n', 'a', 'h'}},
ans344{[]byte{'h', 'a', 'n', 'n', 'a', 'H'}},
},
}
fmt.Printf("------------------------Leetcode Problem 344------------------------\n")
for _, q := range qs {
_, p := q.ans344, q.para344
fmt.Printf("【input】:%v ", p.one)
reverseString(p.one)
fmt.Printf("【output】:%v\n", 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/0947.Most-Stones-Removed-with-Same-Row-or-Column/947. Most Stones Removed with Same Row or Column.go | leetcode/0947.Most-Stones-Removed-with-Same-Row-or-Column/947. Most Stones Removed with Same Row or Column.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/template"
)
func removeStones(stones [][]int) int {
if len(stones) <= 1 {
return 0
}
uf, rowMap, colMap := template.UnionFind{}, map[int]int{}, map[int]int{}
uf.Init(len(stones))
for i := 0; i < len(stones); i++ {
if _, ok := rowMap[stones[i][0]]; ok {
uf.Union(rowMap[stones[i][0]], i)
} else {
rowMap[stones[i][0]] = i
}
if _, ok := colMap[stones[i][1]]; ok {
uf.Union(colMap[stones[i][1]], i)
} else {
colMap[stones[i][1]] = i
}
}
return len(stones) - uf.TotalCount()
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0947.Most-Stones-Removed-with-Same-Row-or-Column/947. Most Stones Removed with Same Row or Column_test.go | leetcode/0947.Most-Stones-Removed-with-Same-Row-or-Column/947. Most Stones Removed with Same Row or Column_test.go | package leetcode
import (
"fmt"
"testing"
)
type question947 struct {
para947
ans947
}
// para 是参数
// one 代表第一个参数
type para947 struct {
stones [][]int
}
// ans 是答案
// one 代表第一个答案
type ans947 struct {
one int
}
func Test_Problem947(t *testing.T) {
qs := []question947{
{
para947{[][]int{{0, 0}, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 2}}},
ans947{5},
},
{
para947{[][]int{{0, 0}, {0, 2}, {1, 1}, {2, 0}, {2, 2}}},
ans947{3},
},
{
para947{[][]int{{0, 0}}},
ans947{0},
},
}
fmt.Printf("------------------------Leetcode Problem 947------------------------\n")
for _, q := range qs {
_, p := q.ans947, q.para947
fmt.Printf("【input】:%v 【output】:%v\n", p, removeStones(p.stones))
}
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/0435.Non-overlapping-Intervals/435. Non-overlapping Intervals_test.go | leetcode/0435.Non-overlapping-Intervals/435. Non-overlapping Intervals_test.go | package leetcode
import (
"fmt"
"testing"
)
type question435 struct {
para435
ans435
}
// para 是参数
// one 代表第一个参数
type para435 struct {
one [][]int
}
// ans 是答案
// one 代表第一个答案
type ans435 struct {
one int
}
func Test_Problem435(t *testing.T) {
qs := []question435{
{
para435{[][]int{{1, 2}, {2, 3}, {3, 4}, {1, 3}}},
ans435{1},
},
{
para435{[][]int{{1, 2}, {1, 2}, {1, 2}}},
ans435{2},
},
{
para435{[][]int{{1, 2}, {2, 3}}},
ans435{0},
},
}
fmt.Printf("------------------------Leetcode Problem 435------------------------\n")
for _, q := range qs {
_, p := q.ans435, q.para435
fmt.Printf("【input】:%v 【output】:%v\n", p, eraseOverlapIntervals1(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/0435.Non-overlapping-Intervals/435. Non-overlapping Intervals.go | leetcode/0435.Non-overlapping-Intervals/435. Non-overlapping Intervals.go | package leetcode
import (
"sort"
)
// 解法一 DP O(n^2) 思路是仿造最长上升子序列的思路
func eraseOverlapIntervals(intervals [][]int) int {
if len(intervals) == 0 {
return 0
}
sort.Sort(Intervals(intervals))
dp, res := make([]int, len(intervals)), 0
for i := range dp {
dp[i] = 1
}
for i := 1; i < len(intervals); i++ {
for j := 0; j < i; j++ {
if intervals[i][0] >= intervals[j][1] {
dp[i] = max(dp[i], 1+dp[j])
}
}
}
for _, v := range dp {
res = max(res, v)
}
return len(intervals) - res
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
// Intervals define
type Intervals [][]int
func (a Intervals) Len() int {
return len(a)
}
func (a Intervals) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a Intervals) Less(i, j int) bool {
for k := 0; k < len(a[i]); k++ {
if a[i][k] < a[j][k] {
return true
} else if a[i][k] == a[j][k] {
continue
} else {
return false
}
}
return true
}
// 解法二 贪心 O(n)
func eraseOverlapIntervals1(intervals [][]int) int {
if len(intervals) == 0 {
return 0
}
sort.Sort(Intervals(intervals))
pre, res := 0, 1
for i := 1; i < len(intervals); i++ {
if intervals[i][0] >= intervals[pre][1] {
res++
pre = i
} else if intervals[i][1] < intervals[pre][1] {
pre = i
}
}
return len(intervals) - res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1028.Recover-a-Tree-From-Preorder-Traversal/1028. Recover a Tree From Preorder Traversal_test.go | leetcode/1028.Recover-a-Tree-From-Preorder-Traversal/1028. Recover a Tree From Preorder Traversal_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question1028 struct {
para1028
ans1028
}
// para 是参数
// one 代表第一个参数
type para1028 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans1028 struct {
one []int
}
func Test_Problem1028(t *testing.T) {
qs := []question1028{
{
para1028{"1-2--3--4-5--6--7"},
ans1028{[]int{1, 2, 5, 3, 4, 6, 7}},
},
{
para1028{"1-2--3---4-5--6---7"},
ans1028{[]int{1, 2, 5, 3, structures.NULL, 6, structures.NULL, 4, structures.NULL, 7}},
},
{
para1028{"1-401--349---90--88"},
ans1028{[]int{1, 401, structures.NULL, 349, 88, 90}},
},
}
fmt.Printf("------------------------Leetcode Problem 1028------------------------\n")
for _, q := range qs {
_, p := q.ans1028, q.para1028
fmt.Printf("【input】:%v 【output】:%v\n", p, structures.Tree2ints(recoverFromPreorder(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/1028.Recover-a-Tree-From-Preorder-Traversal/1028. Recover a Tree From Preorder Traversal.go | leetcode/1028.Recover-a-Tree-From-Preorder-Traversal/1028. Recover a Tree From Preorder Traversal.go | package leetcode
import (
"strconv"
"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 recoverFromPreorder(S string) *TreeNode {
if len(S) == 0 {
return &TreeNode{}
}
root, index, level := &TreeNode{}, 0, 0
cur := root
dfsBuildPreorderTree(S, &index, &level, cur)
return root.Right
}
func dfsBuildPreorderTree(S string, index, level *int, cur *TreeNode) (newIndex *int) {
if *index == len(S) {
return index
}
if *index == 0 && *level == 0 {
i := 0
for i = *index; i < len(S); i++ {
if !isDigital(S[i]) {
break
}
}
num, _ := strconv.Atoi(S[*index:i])
tmp := &TreeNode{Val: num, Left: nil, Right: nil}
cur.Right = tmp
nLevel := *level + 1
index = dfsBuildPreorderTree(S, &i, &nLevel, tmp)
index = dfsBuildPreorderTree(S, index, &nLevel, tmp)
}
i := 0
for i = *index; i < len(S); i++ {
if isDigital(S[i]) {
break
}
}
if *level == i-*index {
j := 0
for j = i; j < len(S); j++ {
if !isDigital(S[j]) {
break
}
}
num, _ := strconv.Atoi(S[i:j])
tmp := &TreeNode{Val: num, Left: nil, Right: nil}
if cur.Left == nil {
cur.Left = tmp
nLevel := *level + 1
index = dfsBuildPreorderTree(S, &j, &nLevel, tmp)
index = dfsBuildPreorderTree(S, index, level, cur)
} else if cur.Right == nil {
cur.Right = tmp
nLevel := *level + 1
index = dfsBuildPreorderTree(S, &j, &nLevel, tmp)
index = dfsBuildPreorderTree(S, index, level, cur)
}
}
return index
}
func isDigital(v byte) bool {
if v >= '0' && v <= '9' {
return true
}
return false
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0071.Simplify-Path/71. Simplify Path_test.go | leetcode/0071.Simplify-Path/71. Simplify Path_test.go | package leetcode
import (
"fmt"
"testing"
)
type question71 struct {
para71
ans71
}
// para 是参数
// one 代表第一个参数
type para71 struct {
s string
}
// ans 是答案
// one 代表第一个答案
type ans71 struct {
one string
}
func Test_Problem71(t *testing.T) {
qs := []question71{
{
para71{"/.hidden"},
ans71{"/.hidden"},
},
{
para71{"/..hidden"},
ans71{"/..hidden"},
},
{
para71{"/abc/..."},
ans71{"/abc/..."},
},
{
para71{"/home/"},
ans71{"/home"},
},
{
para71{"/..."},
ans71{"/..."},
},
{
para71{"/../"},
ans71{"/"},
},
{
para71{"/home//foo/"},
ans71{"/home/foo"},
},
{
para71{"/a/./b/../../c/"},
ans71{"/c"},
},
}
fmt.Printf("------------------------Leetcode Problem 71------------------------\n")
for _, q := range qs {
_, p := q.ans71, q.para71
fmt.Printf("【input】:%v 【output】:%v\n", p, simplifyPath(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/0071.Simplify-Path/71. Simplify Path.go | leetcode/0071.Simplify-Path/71. Simplify Path.go | package leetcode
import (
"path/filepath"
"strings"
)
// 解法一
func simplifyPath(path string) string {
arr := strings.Split(path, "/")
stack := make([]string, 0)
var res string
for i := 0; i < len(arr); i++ {
cur := arr[i]
//cur := strings.TrimSpace(arr[i]) 更加严谨的做法应该还要去掉末尾的空格
if cur == ".." {
if len(stack) > 0 {
stack = stack[:len(stack)-1]
}
} else if cur != "." && len(cur) > 0 {
stack = append(stack, arr[i])
}
}
if len(stack) == 0 {
return "/"
}
res = strings.Join(stack, "/")
return "/" + res
}
// 解法二 golang 的官方库 API
func simplifyPath1(path string) string {
return filepath.Clean(path)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0869.Reordered-Power-of-2/869. Reordered Power of 2_test.go | leetcode/0869.Reordered-Power-of-2/869. Reordered Power of 2_test.go | package leetcode
import (
"fmt"
"testing"
)
type question869 struct {
para869
ans869
}
// para 是参数
// one 代表第一个参数
type para869 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans869 struct {
one bool
}
func Test_Problem869(t *testing.T) {
qs := []question869{
{
para869{1},
ans869{true},
},
{
para869{10},
ans869{false},
},
{
para869{16},
ans869{true},
},
{
para869{24},
ans869{false},
},
{
para869{46},
ans869{true},
},
{
para869{100},
ans869{false},
},
{
para869{123453242},
ans869{false},
},
}
fmt.Printf("------------------------Leetcode Problem 869------------------------\n")
for _, q := range qs {
_, p := q.ans869, q.para869
fmt.Printf("【input】:%v 【output】:%v\n", p, reorderedPowerOf2(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/0869.Reordered-Power-of-2/869. Reordered Power of 2.go | leetcode/0869.Reordered-Power-of-2/869. Reordered Power of 2.go | package leetcode
import "fmt"
func reorderedPowerOf2(n int) bool {
sample, i := fmt.Sprintf("%v", n), 1
for len(fmt.Sprintf("%v", i)) <= len(sample) {
t := fmt.Sprintf("%v", i)
if len(t) == len(sample) && isSame(t, sample) {
return true
}
i = i << 1
}
return false
}
func isSame(t, s string) bool {
m := make(map[rune]int)
for _, v := range t {
m[v]++
}
for _, v := range s {
m[v]--
if m[v] < 0 {
return false
}
if m[v] == 0 {
delete(m, v)
}
}
return len(m) == 0
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0560.Subarray-Sum-Equals-K/560. Subarray Sum Equals K_test.go | leetcode/0560.Subarray-Sum-Equals-K/560. Subarray Sum Equals K_test.go | package leetcode
import (
"fmt"
"testing"
)
type question560 struct {
para560
ans560
}
// para 是参数
// one 代表第一个参数
type para560 struct {
nums []int
k int
}
// ans 是答案
// one 代表第一个答案
type ans560 struct {
one int
}
func Test_Problem560(t *testing.T) {
qs := []question560{
{
para560{[]int{1, 1, 1}, 2},
ans560{2},
},
{
para560{[]int{1, 2, 3}, 3},
ans560{2},
},
{
para560{[]int{1}, 0},
ans560{0},
},
{
para560{[]int{-1, -1, 1}, 0},
ans560{1},
},
{
para560{[]int{1, -1, 0}, 0},
ans560{3},
},
}
fmt.Printf("------------------------Leetcode Problem 560------------------------\n")
for _, q := range qs {
_, p := q.ans560, q.para560
fmt.Printf("【input】:%v 【output】:%v\n", p, subarraySum(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/0560.Subarray-Sum-Equals-K/560. Subarray Sum Equals K.go | leetcode/0560.Subarray-Sum-Equals-K/560. Subarray Sum Equals K.go | package leetcode
func subarraySum(nums []int, k int) int {
count, pre := 0, 0
m := map[int]int{}
m[0] = 1
for i := 0; i < len(nums); i++ {
pre += nums[i]
if _, ok := m[pre-k]; ok {
count += m[pre-k]
}
m[pre] += 1
}
return count
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0318.Maximum-Product-of-Word-Lengths/318. Maximum Product of Word Lengths_test.go | leetcode/0318.Maximum-Product-of-Word-Lengths/318. Maximum Product of Word Lengths_test.go | package leetcode
import (
"fmt"
"testing"
)
type question318 struct {
para318
ans318
}
// para 是参数
// one 代表第一个参数
type para318 struct {
one []string
}
// ans 是答案
// one 代表第一个答案
type ans318 struct {
one int
}
func Test_Problem318(t *testing.T) {
qs := []question318{
{
para318{[]string{"abcw", "baz", "foo", "bar", "xtfn", "abcdef"}},
ans318{16},
},
{
para318{[]string{"a", "ab", "abc", "d", "cd", "bcd", "abcd"}},
ans318{4},
},
{
para318{[]string{"a", "aa", "aaa", "aaaa"}},
ans318{0},
},
}
fmt.Printf("------------------------Leetcode Problem 318------------------------\n")
for _, q := range qs {
_, p := q.ans318, q.para318
fmt.Printf("【input】:%v 【output】:%v\n", p, maxProduct318(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/0318.Maximum-Product-of-Word-Lengths/318. Maximum Product of Word Lengths.go | leetcode/0318.Maximum-Product-of-Word-Lengths/318. Maximum Product of Word Lengths.go | package leetcode
func maxProduct318(words []string) int {
if words == nil || len(words) == 0 {
return 0
}
length, value, maxProduct := len(words), make([]int, len(words)), 0
for i := 0; i < length; i++ {
tmp := words[i]
value[i] = 0
for j := 0; j < len(tmp); j++ {
value[i] |= 1 << (tmp[j] - 'a')
}
}
for i := 0; i < length; i++ {
for j := i + 1; j < length; j++ {
if (value[i]&value[j]) == 0 && (len(words[i])*len(words[j]) > maxProduct) {
maxProduct = len(words[i]) * len(words[j])
}
}
}
return maxProduct
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0396.Rotate-Function/396. Rotate Function_test.go | leetcode/0396.Rotate-Function/396. Rotate Function_test.go | package leetcode
import (
"fmt"
"testing"
)
type question396 struct {
para396
ans396
}
// para 是参数
// one 代表第一个参数
type para396 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans396 struct {
one int
}
func Test_Problem396(t *testing.T) {
qs := []question396{
{
para396{[]int{4, 3, 2, 6}},
ans396{26},
},
{
para396{[]int{100}},
ans396{0},
},
}
fmt.Printf("------------------------Leetcode Problem 396------------------------\n")
for _, q := range qs {
_, p := q.ans396, q.para396
fmt.Printf("【input】:%v 【output】:%v\n", p, maxRotateFunction(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/0396.Rotate-Function/396. Rotate Function.go | leetcode/0396.Rotate-Function/396. Rotate Function.go | package leetcode
func maxRotateFunction(nums []int) int {
n := len(nums)
var sum, f int
for i, num := range nums {
sum += num
f += i * num // F(0)
}
ans := f
for i := 1; i < n; i++ {
f += sum - n*nums[n-i] // F(i) = F(i-1) + sum - n*nums[n-i]
if f > ans {
ans = f
}
}
return ans
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0066.Plus-One/66. Plus One.go | leetcode/0066.Plus-One/66. Plus One.go | package leetcode
func plusOne(digits []int) []int {
for i := len(digits) - 1; i >= 0; i-- {
if digits[i] != 9 {
digits[i]++
return digits
}
digits[i] = 0
}
return append([]int{1}, digits...)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0066.Plus-One/66. Plus One_test.go | leetcode/0066.Plus-One/66. Plus One_test.go | package leetcode
import (
"fmt"
"testing"
)
type question66 struct {
para66
ans66
}
// para 是参数
// one 代表第一个参数
type para66 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans66 struct {
one []int
}
func Test_Problem66(t *testing.T) {
qs := []question66{
{
para66{[]int{1, 2, 3}},
ans66{[]int{1, 2, 4}},
},
{
para66{[]int{4, 3, 2, 1}},
ans66{[]int{4, 3, 2, 2}},
},
{
para66{[]int{9, 9}},
ans66{[]int{1, 0, 0}},
},
{
para66{[]int{0}},
ans66{[]int{0}},
},
}
fmt.Printf("------------------------Leetcode Problem 66------------------------\n")
for _, q := range qs {
_, p := q.ans66, q.para66
fmt.Printf("【input】:%v 【output】:%v\n", p, plusOne(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/1091.Shortest-Path-in-Binary-Matrix/1091. Shortest Path in Binary Matrix.go | leetcode/1091.Shortest-Path-in-Binary-Matrix/1091. Shortest Path in Binary Matrix.go | package leetcode
var dir = [][]int{
{-1, -1},
{-1, 0},
{-1, 1},
{0, 1},
{0, -1},
{1, -1},
{1, 0},
{1, 1},
}
func shortestPathBinaryMatrix(grid [][]int) int {
visited := make([][]bool, 0)
for range make([]int, len(grid)) {
visited = append(visited, make([]bool, len(grid[0])))
}
dis := make([][]int, 0)
for range make([]int, len(grid)) {
dis = append(dis, make([]int, len(grid[0])))
}
if grid[0][0] == 1 {
return -1
}
if len(grid) == 1 && len(grid[0]) == 1 {
return 1
}
queue := []int{0}
visited[0][0], dis[0][0] = true, 1
for len(queue) > 0 {
cur := queue[0]
queue = queue[1:]
curx, cury := cur/len(grid[0]), cur%len(grid[0])
for d := 0; d < 8; d++ {
nextx := curx + dir[d][0]
nexty := cury + dir[d][1]
if isInBoard(grid, nextx, nexty) && !visited[nextx][nexty] && grid[nextx][nexty] == 0 {
queue = append(queue, nextx*len(grid[0])+nexty)
visited[nextx][nexty] = true
dis[nextx][nexty] = dis[curx][cury] + 1
if nextx == len(grid)-1 && nexty == len(grid[0])-1 {
return dis[nextx][nexty]
}
}
}
}
return -1
}
func isInBoard(board [][]int, x, y int) bool {
return x >= 0 && x < len(board) && y >= 0 && y < len(board[0])
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1091.Shortest-Path-in-Binary-Matrix/1091. Shortest Path in Binary Matrix_test.go | leetcode/1091.Shortest-Path-in-Binary-Matrix/1091. Shortest Path in Binary Matrix_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1091 struct {
para1091
ans1091
}
// para 是参数
// one 代表第一个参数
type para1091 struct {
grid [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1091 struct {
one int
}
func Test_Problem1091(t *testing.T) {
qs := []question1091{
{
para1091{[][]int{{0, 1}, {1, 0}}},
ans1091{2},
},
{
para1091{[][]int{{0, 0, 0}, {1, 1, 0}, {1, 1, 0}}},
ans1091{4},
},
}
fmt.Printf("------------------------Leetcode Problem 1091------------------------\n")
for _, q := range qs {
_, p := q.ans1091, q.para1091
fmt.Printf("【input】:%v 【output】:%v\n", p, shortestPathBinaryMatrix(p.grid))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0009.Palindrome-Number/9. Palindrome Number.go | leetcode/0009.Palindrome-Number/9. Palindrome Number.go | package leetcode
import "strconv"
// 解法一
func isPalindrome(x int) bool {
if x < 0 {
return false
}
if x == 0 {
return true
}
if x%10 == 0 {
return false
}
arr := make([]int, 0, 32)
for x > 0 {
arr = append(arr, x%10)
x = x / 10
}
sz := len(arr)
for i, j := 0, sz-1; i <= j; i, j = i+1, j-1 {
if arr[i] != arr[j] {
return false
}
}
return true
}
// 解法二 数字转字符串
func isPalindrome1(x int) bool {
if x < 0 {
return false
}
if x < 10 {
return true
}
s := strconv.Itoa(x)
length := len(s)
for i := 0; i <= length/2; i++ {
if s[i] != s[length-1-i] {
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/0009.Palindrome-Number/9. Palindrome Number_test.go | leetcode/0009.Palindrome-Number/9. Palindrome Number_test.go | package leetcode
import (
"fmt"
"testing"
)
type question9 struct {
para9
ans9
}
// para 是参数
// one 代表第一个参数
type para9 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans9 struct {
one bool
}
func Test_Problem9(t *testing.T) {
qs := []question9{
{
para9{121},
ans9{true},
},
{
para9{-121},
ans9{false},
},
{
para9{10},
ans9{false},
},
{
para9{321},
ans9{false},
},
{
para9{-123},
ans9{false},
},
{
para9{120},
ans9{false},
},
{
para9{1534236469},
ans9{false},
},
}
fmt.Printf("------------------------Leetcode Problem 9------------------------\n")
for _, q := range qs {
_, p := q.ans9, q.para9
fmt.Printf("【input】:%v 【output】:%v\n", p.one, isPalindrome(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/1006.Clumsy-Factorial/1006. Clumsy Factorial_test.go | leetcode/1006.Clumsy-Factorial/1006. Clumsy Factorial_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1006 struct {
para1006
ans1006
}
// para 是参数
// one 代表第一个参数
type para1006 struct {
N int
}
// ans 是答案
// one 代表第一个答案
type ans1006 struct {
one int
}
func Test_Problem1006(t *testing.T) {
qs := []question1006{
{
para1006{4},
ans1006{7},
},
{
para1006{10},
ans1006{12},
},
{
para1006{100},
ans1006{101},
},
}
fmt.Printf("------------------------Leetcode Problem 1006------------------------\n")
for _, q := range qs {
_, p := q.ans1006, q.para1006
fmt.Printf("【input】:%v 【output】:%v\n", p, clumsy(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/1006.Clumsy-Factorial/1006. Clumsy Factorial.go | leetcode/1006.Clumsy-Factorial/1006. Clumsy Factorial.go | package leetcode
func clumsy(N int) int {
res, count, tmp, flag := 0, 1, N, false
for i := N - 1; i > 0; i-- {
count = count % 4
switch count {
case 1:
tmp = tmp * i
case 2:
tmp = tmp / i
case 3:
res = res + tmp
flag = true
tmp = -1
res = res + i
case 0:
flag = false
tmp = tmp * (i)
}
count++
}
if !flag {
res = res + tmp
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0279.Perfect-Squares/279. Perfect Squares_test.go | leetcode/0279.Perfect-Squares/279. Perfect Squares_test.go | package leetcode
import (
"fmt"
"testing"
)
type question279 struct {
para279
ans279
}
// para 是参数
// one 代表第一个参数
type para279 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans279 struct {
one int
}
func Test_Problem279(t *testing.T) {
qs := []question279{
{
para279{13},
ans279{2},
},
{
para279{12},
ans279{3},
},
}
fmt.Printf("------------------------Leetcode Problem 279------------------------\n")
for _, q := range qs {
_, p := q.ans279, q.para279
fmt.Printf("【input】:%v 【output】:%v\n", p, numSquares(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/0279.Perfect-Squares/279. Perfect Squares.go | leetcode/0279.Perfect-Squares/279. Perfect Squares.go | package leetcode
import "math"
func numSquares(n int) int {
if isPerfectSquare(n) {
return 1
}
if checkAnswer4(n) {
return 4
}
for i := 1; i*i <= n; i++ {
j := n - i*i
if isPerfectSquare(j) {
return 2
}
}
return 3
}
// 判断是否为完全平方数
func isPerfectSquare(n int) bool {
sq := int(math.Floor(math.Sqrt(float64(n))))
return sq*sq == n
}
// 判断是否能表示为 4^k*(8m+7)
func checkAnswer4(x int) bool {
for x%4 == 0 {
x /= 4
}
return x%8 == 7
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1631.Path-With-Minimum-Effort/1631. Path With Minimum Effort.go | leetcode/1631.Path-With-Minimum-Effort/1631. Path With Minimum Effort.go | package leetcode
import (
"sort"
"github.com/halfrost/LeetCode-Go/template"
)
var dir = [4][2]int{
{0, 1},
{1, 0},
{0, -1},
{-1, 0},
}
// 解法一 DFS + 二分
func minimumEffortPath(heights [][]int) int {
n, m := len(heights), len(heights[0])
visited := make([][]bool, n)
for i := range visited {
visited[i] = make([]bool, m)
}
low, high := 0, 1000000
for low < high {
threshold := low + (high-low)>>1
if !hasPath(heights, visited, 0, 0, threshold) {
low = threshold + 1
} else {
high = threshold
}
for i := range visited {
for j := range visited[i] {
visited[i][j] = false
}
}
}
return low
}
func hasPath(heights [][]int, visited [][]bool, i, j, threshold int) bool {
n, m := len(heights), len(heights[0])
if i == n-1 && j == m-1 {
return true
}
visited[i][j] = true
res := false
for _, d := range dir {
ni, nj := i+d[0], j+d[1]
if ni < 0 || ni >= n || nj < 0 || nj >= m || visited[ni][nj] || res {
continue
}
diff := abs(heights[i][j] - heights[ni][nj])
if diff <= threshold && hasPath(heights, visited, ni, nj, threshold) {
res = true
}
}
return res
}
func abs(a int) int {
if a < 0 {
a = -a
}
return a
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a < b {
return b
}
return a
}
// 解法二 并查集
func minimumEffortPath1(heights [][]int) int {
n, m, edges, uf := len(heights), len(heights[0]), []edge{}, template.UnionFind{}
uf.Init(n * m)
for i, row := range heights {
for j, h := range row {
id := i*m + j
if i > 0 {
edges = append(edges, edge{id - m, id, abs(h - heights[i-1][j])})
}
if j > 0 {
edges = append(edges, edge{id - 1, id, abs(h - heights[i][j-1])})
}
}
}
sort.Slice(edges, func(i, j int) bool { return edges[i].diff < edges[j].diff })
for _, e := range edges {
uf.Union(e.v, e.w)
if uf.Find(0) == uf.Find(n*m-1) {
return e.diff
}
}
return 0
}
type edge struct {
v, w, diff int
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1631.Path-With-Minimum-Effort/1631. Path With Minimum Effort_test.go | leetcode/1631.Path-With-Minimum-Effort/1631. Path With Minimum Effort_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1631 struct {
para1631
ans1631
}
// para 是参数
// one 代表第一个参数
type para1631 struct {
heights [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1631 struct {
one int
}
func Test_Problem1631(t *testing.T) {
qs := []question1631{
{
para1631{[][]int{{1, 2, 2}, {3, 8, 2}, {5, 3, 5}}},
ans1631{2},
},
{
para1631{[][]int{{1, 2, 3}, {3, 8, 4}, {5, 3, 5}}},
ans1631{1},
},
{
para1631{[][]int{{1, 2, 1, 1, 1}, {1, 2, 1, 2, 1}, {1, 2, 1, 2, 1}, {1, 2, 1, 2, 1}, {1, 1, 1, 2, 1}}},
ans1631{0},
},
}
fmt.Printf("------------------------Leetcode Problem 1631------------------------\n")
for _, q := range qs {
_, p := q.ans1631, q.para1631
fmt.Printf("【input】:%v 【output】:%v \n", p, minimumEffortPath(p.heights))
}
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/1209.Remove-All-Adjacent-Duplicates-in-String-II/1209. Remove All Adjacent Duplicates in String II.go | leetcode/1209.Remove-All-Adjacent-Duplicates-in-String-II/1209. Remove All Adjacent Duplicates in String II.go | package leetcode
// 解法一 stack
func removeDuplicates(s string, k int) string {
stack, arr := [][2]int{}, []byte{}
for _, c := range s {
i := int(c - 'a')
if len(stack) > 0 && stack[len(stack)-1][0] == i {
stack[len(stack)-1][1]++
if stack[len(stack)-1][1] == k {
stack = stack[:len(stack)-1]
}
} else {
stack = append(stack, [2]int{i, 1})
}
}
for _, pair := range stack {
c := byte(pair[0] + 'a')
for i := 0; i < pair[1]; i++ {
arr = append(arr, c)
}
}
return string(arr)
}
// 解法二 暴力
func removeDuplicates1(s string, k int) string {
arr, count, tmp := []rune{}, 0, '#'
for _, v := range s {
arr = append(arr, v)
for len(arr) > 0 {
count = 0
tmp = arr[len(arr)-1]
for i := len(arr) - 1; i >= 0; i-- {
if arr[i] != tmp {
break
}
count++
}
if count == k {
arr = arr[:len(arr)-k]
} else {
break
}
}
}
return string(arr)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1209.Remove-All-Adjacent-Duplicates-in-String-II/1209. Remove All Adjacent Duplicates in String II_test.go | leetcode/1209.Remove-All-Adjacent-Duplicates-in-String-II/1209. Remove All Adjacent Duplicates in String II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1209 struct {
para1209
ans1209
}
// para 是参数
// one 代表第一个参数
type para1209 struct {
s string
k int
}
// ans 是答案
// one 代表第一个答案
type ans1209 struct {
one string
}
func Test_Problem1209(t *testing.T) {
qs := []question1209{
// {
// para1209{"abcd", 2},
// ans1209{"abcd"},
// },
{
para1209{"deeedbbcccbdaa", 3},
ans1209{"aa"},
},
{
para1209{"pbbcggttciiippooaais", 2},
ans1209{"ps"},
},
}
fmt.Printf("------------------------Leetcode Problem 1209------------------------\n")
for _, q := range qs {
_, p := q.ans1209, q.para1209
fmt.Printf("【input】:%v 【output】:%v\n", p, removeDuplicates(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/0039.Combination-Sum/39. Combination Sum_test.go | leetcode/0039.Combination-Sum/39. Combination Sum_test.go | package leetcode
import (
"fmt"
"testing"
)
type question39 struct {
para39
ans39
}
// para 是参数
// one 代表第一个参数
type para39 struct {
n []int
k int
}
// ans 是答案
// one 代表第一个答案
type ans39 struct {
one [][]int
}
func Test_Problem39(t *testing.T) {
qs := []question39{
{
para39{[]int{2, 3, 6, 7}, 7},
ans39{[][]int{{7}, {2, 2, 3}}},
},
{
para39{[]int{2, 3, 5}, 8},
ans39{[][]int{{2, 2, 2, 2}, {2, 3, 3}, {3, 5}}},
},
}
fmt.Printf("------------------------Leetcode Problem 39------------------------\n")
for _, q := range qs {
_, p := q.ans39, q.para39
fmt.Printf("【input】:%v 【output】:%v\n", p, combinationSum(p.n, p.k))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0039.Combination-Sum/39. Combination Sum.go | leetcode/0039.Combination-Sum/39. Combination Sum.go | package leetcode
import "sort"
func combinationSum(candidates []int, target int) [][]int {
if len(candidates) == 0 {
return [][]int{}
}
c, res := []int{}, [][]int{}
sort.Ints(candidates)
findcombinationSum(candidates, target, 0, c, &res)
return res
}
func findcombinationSum(nums []int, target, index int, c []int, res *[][]int) {
if target <= 0 {
if target == 0 {
b := make([]int, len(c))
copy(b, c)
*res = append(*res, b)
}
return
}
for i := index; i < len(nums); i++ {
if nums[i] > target { // 这里可以剪枝优化
break
}
c = append(c, nums[i])
findcombinationSum(nums, target-nums[i], i, c, res) // 注意这里迭代的时候 index 依旧不变,因为一个元素可以取多次
c = c[:len(c)-1]
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0897.Increasing-Order-Search-Tree/897. Increasing Order Search Tree_test.go | leetcode/0897.Increasing-Order-Search-Tree/897. Increasing Order Search Tree_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question897 struct {
para897
ans897
}
// para 是参数
// one 代表第一个参数
type para897 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans897 struct {
one []int
}
func Test_Problem897(t *testing.T) {
qs := []question897{
{
para897{[]int{5, 3, 6, 2, 4, structures.NULL, 8, 1, structures.NULL, structures.NULL, structures.NULL, 7, 9}},
ans897{[]int{1, structures.NULL, 2, structures.NULL, 3, structures.NULL, 4, structures.NULL, 5, structures.NULL, 6, structures.NULL, 7, structures.NULL, 8, structures.NULL, 9}},
},
{
para897{[]int{3, 4, 4, 5, structures.NULL, structures.NULL, 5, 6, structures.NULL, structures.NULL, 6}},
ans897{[]int{6, structures.NULL, 5, structures.NULL, 4, structures.NULL, 3, structures.NULL, 4, structures.NULL, 5, structures.NULL, 6}},
},
{
para897{[]int{1, 2, 2, structures.NULL, 3, 3}},
ans897{[]int{2, structures.NULL, 3, structures.NULL, 1, structures.NULL, 3, structures.NULL, 2}},
},
{
para897{[]int{}},
ans897{[]int{}},
},
{
para897{[]int{1}},
ans897{[]int{1}},
},
{
para897{[]int{1, 2, 3}},
ans897{[]int{2, structures.NULL, 1, structures.NULL, 3}},
},
{
para897{[]int{1, 2, 2, 3, 4, 4, 3}},
ans897{[]int{3, structures.NULL, 2, structures.NULL, 4, structures.NULL, 1, structures.NULL, 4, structures.NULL, 2, structures.NULL, 3}},
},
{
para897{[]int{1, 2, 2, structures.NULL, 3, structures.NULL, 3}},
ans897{[]int{2, structures.NULL, 3, structures.NULL, 1, structures.NULL, 2, structures.NULL, 3}},
},
}
fmt.Printf("------------------------Leetcode Problem 897------------------------\n")
for _, q := range qs {
_, p := q.ans897, q.para897
fmt.Printf("【input】:%v ", p)
rootOne := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", structures.Tree2ints(increasingBST(rootOne)))
}
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/0897.Increasing-Order-Search-Tree/897. Increasing Order Search Tree.go | leetcode/0897.Increasing-Order-Search-Tree/897. Increasing Order Search Tree.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// 解法一 链表思想
func increasingBST(root *TreeNode) *TreeNode {
var head = &TreeNode{}
tail := head
recBST(root, tail)
return head.Right
}
func recBST(root, tail *TreeNode) *TreeNode {
if root == nil {
return tail
}
tail = recBST(root.Left, tail)
root.Left = nil // 切断 root 与其 Left 的连接,避免形成环
tail.Right, tail = root, root // 把 root 接上 tail,并保持 tail 指向尾部
tail = recBST(root.Right, tail)
return tail
}
// 解法二 模拟
func increasingBST1(root *TreeNode) *TreeNode {
list := []int{}
inorder(root, &list)
if len(list) == 0 {
return root
}
newRoot := &TreeNode{Val: list[0], Left: nil, Right: nil}
cur := newRoot
for index := 1; index < len(list); index++ {
tmp := &TreeNode{Val: list[index], Left: nil, Right: nil}
cur.Right = tmp
cur = tmp
}
return newRoot
}
func inorder(root *TreeNode, output *[]int) {
if root != nil {
inorder(root.Left, output)
*output = append(*output, root.Val)
inorder(root.Right, output)
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0352.Data-Stream-as-Disjoint-Intervals/352.Data Stream as Disjoint Intervals_test.go | leetcode/0352.Data-Stream-as-Disjoint-Intervals/352.Data Stream as Disjoint Intervals_test.go | package leetcode
import (
"fmt"
"testing"
)
type question352 struct {
para352
ans352
}
// para 是参数
type para352 struct {
para string
num int
}
// ans 是答案
type ans352 struct {
ans [][]int
}
func Test_Problem352(t *testing.T) {
qs := []question352{
{
para352{"addNum", 1},
ans352{[][]int{{1, 1}}},
},
{
para352{"addNum", 3},
ans352{[][]int{{1, 1}, {3, 3}}},
},
{
para352{"addNum", 7},
ans352{[][]int{{1, 1}, {3, 3}, {7, 7}}},
},
{
para352{"addNum", 2},
ans352{[][]int{{1, 3}, {7, 7}}},
},
{
para352{"addNum", 6},
ans352{[][]int{{1, 3}, {6, 7}}},
},
}
fmt.Printf("------------------------Leetcode Problem 352------------------------\n")
obj := Constructor()
for _, q := range qs {
_, p := q.ans352, q.para352
obj.AddNum(p.num)
fmt.Printf("【input】:%v 【output】:%v\n", p, obj.GetIntervals())
}
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/0352.Data-Stream-as-Disjoint-Intervals/352.Data Stream as Disjoint Intervals.go | leetcode/0352.Data-Stream-as-Disjoint-Intervals/352.Data Stream as Disjoint Intervals.go | package leetcode
import "sort"
type SummaryRanges struct {
nums []int
mp map[int]int
}
func Constructor() SummaryRanges {
return SummaryRanges{
nums: []int{},
mp: map[int]int{},
}
}
func (this *SummaryRanges) AddNum(val int) {
if _, ok := this.mp[val]; !ok {
this.mp[val] = 1
this.nums = append(this.nums, val)
}
sort.Ints(this.nums)
}
func (this *SummaryRanges) GetIntervals() [][]int {
n := len(this.nums)
var ans [][]int
if n == 0 {
return ans
}
if n == 1 {
ans = append(ans, []int{this.nums[0], this.nums[0]})
return ans
}
start, end := this.nums[0], this.nums[0]
ans = append(ans, []int{start, end})
index := 0
for i := 1; i < n; i++ {
if this.nums[i] == end+1 {
end = this.nums[i]
ans[index][1] = end
} else {
start, end = this.nums[i], this.nums[i]
ans = append(ans, []int{start, end})
index++
}
}
return ans
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0394.Decode-String/394. Decode String_test.go | leetcode/0394.Decode-String/394. Decode String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question394 struct {
para394
ans394
}
// para 是参数
// one 代表第一个参数
type para394 struct {
s string
}
// ans 是答案
// one 代表第一个答案
type ans394 struct {
one string
}
func Test_Problem394(t *testing.T) {
qs := []question394{
{
para394{"10[a]"},
ans394{"aaaaaaaaaa"},
},
{
para394{"3[a]2[bc]"},
ans394{"aaabcbc"},
},
{
para394{"3[a2[c]]"},
ans394{"accaccacc"},
},
{
para394{"2[abc]3[cd]ef"},
ans394{"abcabccdcdcdef"},
},
}
fmt.Printf("------------------------Leetcode Problem 394------------------------\n")
for _, q := range qs {
_, p := q.ans394, q.para394
fmt.Printf("【input】:%v 【output】:%v\n", p, decodeString(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/0394.Decode-String/394. Decode String.go | leetcode/0394.Decode-String/394. Decode String.go | package leetcode
import (
"strconv"
)
func decodeString(s string) string {
stack, res := []string{}, ""
for _, str := range s {
if len(stack) == 0 || (len(stack) > 0 && str != ']') {
stack = append(stack, string(str))
} else {
tmp := ""
for stack[len(stack)-1] != "[" {
tmp = stack[len(stack)-1] + tmp
stack = stack[:len(stack)-1]
}
stack = stack[:len(stack)-1]
index, repeat := 0, ""
for index = len(stack) - 1; index >= 0; index-- {
if stack[index] >= "0" && stack[index] <= "9" {
repeat = stack[index] + repeat
} else {
break
}
}
nums, _ := strconv.Atoi(repeat)
copyTmp := tmp
for i := 0; i < nums-1; i++ {
tmp += copyTmp
}
for i := 0; i < len(repeat)-1; i++ {
stack = stack[:len(stack)-1]
}
stack[index+1] = tmp
}
}
for _, s := range stack {
res += s
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0845.Longest-Mountain-in-Array/845. Longest Mountain in Array.go | leetcode/0845.Longest-Mountain-in-Array/845. Longest Mountain in Array.go | package leetcode
func longestMountain(A []int) int {
left, right, res, isAscending := 0, 0, 0, true
for left < len(A) {
if right+1 < len(A) && ((isAscending == true && A[right+1] > A[left] && A[right+1] > A[right]) || (right != left && A[right+1] < A[right])) {
if A[right+1] < A[right] {
isAscending = false
}
right++
} else {
if right != left && isAscending == false {
res = max(res, right-left+1)
}
left++
if right < left {
right = left
}
if right == left {
isAscending = true
}
}
}
return res
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0845.Longest-Mountain-in-Array/845. Longest Mountain in Array_test.go | leetcode/0845.Longest-Mountain-in-Array/845. Longest Mountain in Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question845 struct {
para845
ans845
}
// para 是参数
// one 代表第一个参数
type para845 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans845 struct {
one int
}
func Test_Problem845(t *testing.T) {
qs := []question845{
{
para845{[]int{875, 884, 239, 731, 723, 685}},
ans845{4},
},
{
para845{[]int{0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0}},
ans845{11},
},
{
para845{[]int{2, 3}},
ans845{0},
},
{
para845{[]int{2, 1, 4, 7, 3, 2, 5}},
ans845{5},
},
{
para845{[]int{2, 2, 2}},
ans845{0},
},
}
fmt.Printf("------------------------Leetcode Problem 845------------------------\n")
for _, q := range qs {
_, p := q.ans845, q.para845
fmt.Printf("【input】:%v 【output】:%v\n", p, longestMountain(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/0455.Assign-Cookies/455. Assign Cookies_test.go | leetcode/0455.Assign-Cookies/455. Assign Cookies_test.go | package leetcode
import (
"fmt"
"testing"
)
type question455 struct {
para455
ans455
}
// para 是参数
// one 代表第一个参数
type para455 struct {
g []int
s []int
}
// ans 是答案
// one 代表第一个答案
type ans455 struct {
one int
}
func Test_Problem455(t *testing.T) {
qs := []question455{
{
para455{[]int{1, 2, 3}, []int{1, 1}},
ans455{1},
},
{
para455{[]int{1, 2}, []int{1, 2, 3}},
ans455{2},
},
}
fmt.Printf("------------------------Leetcode Problem 455------------------------\n")
for _, q := range qs {
_, p := q.ans455, q.para455
fmt.Printf("【input】:%v 【output】:%v\n", p, findContentChildren(p.g, 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/0455.Assign-Cookies/455. Assign Cookies.go | leetcode/0455.Assign-Cookies/455. Assign Cookies.go | package leetcode
import "sort"
func findContentChildren(g []int, s []int) int {
sort.Ints(g)
sort.Ints(s)
gi, si, res := 0, 0, 0
for gi < len(g) && si < len(s) {
if s[si] >= g[gi] {
res++
si++
gi++
} else {
si++
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0968.Binary-Tree-Cameras/968. Binary Tree Cameras.go | leetcode/0968.Binary-Tree-Cameras/968. Binary Tree Cameras.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
* }
*/
type status int
const (
isLeaf status = iota
parentofLeaf
isMonitoredWithoutCamera
)
func minCameraCover(root *TreeNode) int {
res := 0
if minCameraCoverDFS(root, &res) == isLeaf {
res++
}
return res
}
func minCameraCoverDFS(root *TreeNode, res *int) status {
if root == nil {
return 2
}
left, right := minCameraCoverDFS(root.Left, res), minCameraCoverDFS(root.Right, res)
if left == isLeaf || right == isLeaf {
*res++
return parentofLeaf
} else if left == parentofLeaf || right == parentofLeaf {
return isMonitoredWithoutCamera
} else {
return isLeaf
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0968.Binary-Tree-Cameras/968. Binary Tree Cameras_test.go | leetcode/0968.Binary-Tree-Cameras/968. Binary Tree Cameras_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question968 struct {
para968
ans968
}
// para 是参数
// one 代表第一个参数
type para968 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans968 struct {
one int
}
func Test_Problem968(t *testing.T) {
qs := []question968{
{
para968{[]int{0, 0, structures.NULL, 0, 0}},
ans968{1},
},
{
para968{[]int{0, 0, structures.NULL, 0, structures.NULL, 0, structures.NULL, structures.NULL, 0}},
ans968{2},
},
}
fmt.Printf("------------------------Leetcode Problem 968------------------------\n")
for _, q := range qs {
_, p := q.ans968, q.para968
fmt.Printf("【input】:%v ", p)
root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", minCameraCover(root))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0795.Number-of-Subarrays-with-Bounded-Maximum/795. Number of Subarrays with Bounded Maximum_test.go | leetcode/0795.Number-of-Subarrays-with-Bounded-Maximum/795. Number of Subarrays with Bounded Maximum_test.go | package leetcode
import (
"fmt"
"testing"
)
type question795 struct {
para795
ans795
}
// para 是参数
// one 代表第一个参数
type para795 struct {
nums []int
left int
right int
}
// ans 是答案
// one 代表第一个答案
type ans795 struct {
one int
}
func Test_Problem795(t *testing.T) {
qs := []question795{
{
para795{[]int{2, 1, 4, 3}, 2, 3},
ans795{3},
},
}
fmt.Printf("------------------------Leetcode Problem 795------------------------\n")
for _, q := range qs {
_, p := q.ans795, q.para795
fmt.Printf("【input】:%v 【output】:%v\n", p, numSubarrayBoundedMax(p.nums, p.left, p.right))
}
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/0795.Number-of-Subarrays-with-Bounded-Maximum/795. Number of Subarrays with Bounded Maximum.go | leetcode/0795.Number-of-Subarrays-with-Bounded-Maximum/795. Number of Subarrays with Bounded Maximum.go | package leetcode
func numSubarrayBoundedMax(nums []int, left int, right int) int {
return getAnswerPerBound(nums, right) - getAnswerPerBound(nums, left-1)
}
func getAnswerPerBound(nums []int, bound int) int {
res, count := 0, 0
for _, num := range nums {
if num <= bound {
count++
} else {
count = 0
}
res += count
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0844.Backspace-String-Compare/844. Backspace String Compare_test.go | leetcode/0844.Backspace-String-Compare/844. Backspace String Compare_test.go | package leetcode
import (
"fmt"
"testing"
)
type question844 struct {
para844
ans844
}
// para 是参数
// one 代表第一个参数
type para844 struct {
s string
t string
}
// ans 是答案
// one 代表第一个答案
type ans844 struct {
one bool
}
func Test_Problem844(t *testing.T) {
qs := []question844{
{
para844{"ab#c", "ad#c"},
ans844{true},
},
{
para844{"ab##", "c#d#"},
ans844{true},
},
{
para844{"a##c", "#a#c"},
ans844{true},
},
{
para844{"a#c", "b"},
ans844{false},
},
}
fmt.Printf("------------------------Leetcode Problem 844------------------------\n")
for _, q := range qs {
_, p := q.ans844, q.para844
fmt.Printf("【input】:%v 【output】:%v\n", p, backspaceCompare(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/0844.Backspace-String-Compare/844. Backspace String Compare.go | leetcode/0844.Backspace-String-Compare/844. Backspace String Compare.go | package leetcode
func backspaceCompare(S string, T string) bool {
s := make([]rune, 0)
for _, c := range S {
if c == '#' {
if len(s) > 0 {
s = s[:len(s)-1]
}
} else {
s = append(s, c)
}
}
s2 := make([]rune, 0)
for _, c := range T {
if c == '#' {
if len(s2) > 0 {
s2 = s2[:len(s2)-1]
}
} else {
s2 = append(s2, c)
}
}
return string(s) == string(s2)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0454.4Sum-II/454. 4Sum II_test.go | leetcode/0454.4Sum-II/454. 4Sum II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question454 struct {
para454
ans454
}
// para 是参数
// one 代表第一个参数
type para454 struct {
a []int
b []int
c []int
d []int
}
// ans 是答案
// one 代表第一个答案
type ans454 struct {
one int
}
func Test_Problem454(t *testing.T) {
qs := []question454{
{
para454{[]int{1, 2}, []int{-2, -1}, []int{-1, 2}, []int{0, 2}},
ans454{2},
},
}
fmt.Printf("------------------------Leetcode Problem 454------------------------\n")
for _, q := range qs {
_, p := q.ans454, q.para454
fmt.Printf("【input】:%v 【output】:%v\n", p, fourSumCount(p.a, p.b, p.c, 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/0454.4Sum-II/454. 4Sum II.go | leetcode/0454.4Sum-II/454. 4Sum II.go | package leetcode
func fourSumCount(A []int, B []int, C []int, D []int) int {
m := make(map[int]int, len(A)*len(B))
for _, a := range A {
for _, b := range B {
m[a+b]++
}
}
ret := 0
for _, c := range C {
for _, d := range D {
ret += m[0-c-d]
}
}
return ret
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0150.Evaluate-Reverse-Polish-Notation/150. Evaluate Reverse Polish Notation.go | leetcode/0150.Evaluate-Reverse-Polish-Notation/150. Evaluate Reverse Polish Notation.go | package leetcode
import (
"strconv"
)
func evalRPN(tokens []string) int {
stack := make([]int, 0, len(tokens))
for _, token := range tokens {
v, err := strconv.Atoi(token)
if err == nil {
stack = append(stack, v)
} else {
num1, num2 := stack[len(stack)-2], stack[len(stack)-1]
stack = stack[:len(stack)-2]
switch token {
case "+":
stack = append(stack, num1+num2)
case "-":
stack = append(stack, num1-num2)
case "*":
stack = append(stack, num1*num2)
case "/":
stack = append(stack, num1/num2)
}
}
}
return stack[0]
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0150.Evaluate-Reverse-Polish-Notation/150. Evaluate Reverse Polish Notation_test.go | leetcode/0150.Evaluate-Reverse-Polish-Notation/150. Evaluate Reverse Polish Notation_test.go | package leetcode
import (
"fmt"
"testing"
)
type question150 struct {
para150
ans150
}
// para 是参数
// one 代表第一个参数
type para150 struct {
one []string
}
// ans 是答案
// one 代表第一个答案
type ans150 struct {
one int
}
func Test_Problem150(t *testing.T) {
qs := []question150{
{
para150{[]string{"18"}},
ans150{18},
},
{
para150{[]string{"2", "1", "+", "3", "*"}},
ans150{9},
},
{
para150{[]string{"4", "13", "5", "/", "+"}},
ans150{6},
},
{
para150{[]string{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"}},
ans150{22},
},
}
fmt.Printf("------------------------Leetcode Problem 150------------------------\n")
for _, q := range qs {
_, p := q.ans150, q.para150
fmt.Printf("【input】:%v 【output】:%v\n", p, evalRPN(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/0729.My-Calendar-I/729. My Calendar I.go | leetcode/0729.My-Calendar-I/729. My Calendar I.go | package leetcode
// 解法一 二叉排序树
// Event define
type Event struct {
start, end int
left, right *Event
}
// Insert define
func (e *Event) Insert(curr *Event) bool {
if e.end > curr.start && curr.end > e.start {
return false
}
if curr.start < e.start {
if e.left == nil {
e.left = curr
} else {
return e.left.Insert(curr)
}
} else {
if e.right == nil {
e.right = curr
} else {
return e.right.Insert(curr)
}
}
return true
}
// MyCalendar define
type MyCalendar struct {
root *Event
}
// Constructor729 define
func Constructor729() MyCalendar {
return MyCalendar{
root: nil,
}
}
// Book define
func (this *MyCalendar) Book(start int, end int) bool {
curr := &Event{start: start, end: end, left: nil, right: nil}
if this.root == nil {
this.root = curr
return true
}
return this.root.Insert(curr)
}
// 解法二 快排 + 二分
// MyCalendar define
// type MyCalendar struct {
// calendar []Interval
// }
// // Constructor729 define
// func Constructor729() MyCalendar {
// calendar := []Interval{}
// return MyCalendar{calendar: calendar}
// }
// // Book define
// func (this *MyCalendar) Book(start int, end int) bool {
// if len(this.calendar) == 0 {
// this.calendar = append(this.calendar, Interval{Start: start, End: end})
// return true
// }
// // 快排
// quickSort(this.calendar, 0, len(this.calendar)-1)
// // 二分
// pos := searchLastLessInterval(this.calendar, start, end)
// // 如果找到最后一个元素,需要判断 end
// if pos == len(this.calendar)-1 && this.calendar[pos].End <= start {
// this.calendar = append(this.calendar, Interval{Start: start, End: end})
// return true
// }
// // 如果不是开头和结尾的元素,还需要判断这个区间是否能插入到原数组中(要看起点和终点是否都能插入)
// if pos != len(this.calendar)-1 && pos != -1 && this.calendar[pos].End <= start && this.calendar[pos+1].Start >= end {
// this.calendar = append(this.calendar, Interval{Start: start, End: end})
// return true
// }
// // 如果元素比开头的元素还要小,要插入到开头
// if this.calendar[0].Start >= end {
// this.calendar = append(this.calendar, Interval{Start: start, End: end})
// return true
// }
// return false
// }
// func searchLastLessInterval(intervals []Interval, start, end int) int {
// low, high := 0, len(intervals)-1
// for low <= high {
// mid := low + ((high - low) >> 1)
// if intervals[mid].Start <= start {
// if (mid == len(intervals)-1) || (intervals[mid+1].Start > start) { // 找到最后一个小于等于 target 的元素
// return mid
// }
// low = mid + 1
// } else {
// high = mid - 1
// }
// }
// return -1
// }
/**
* Your MyCalendar object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Book(start,end);
*/
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0729.My-Calendar-I/729. My Calendar I_test.go | leetcode/0729.My-Calendar-I/729. My Calendar I_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem729(t *testing.T) {
obj := Constructor729()
param1 := obj.Book(10, 20)
fmt.Printf("param = %v obj = %v\n", param1, obj)
param1 = obj.Book(15, 25)
fmt.Printf("param = %v obj = %v\n", param1, obj)
param1 = obj.Book(20, 30)
fmt.Printf("param = %v obj = %v\n", param1, obj)
obj1 := Constructor729()
param2 := obj1.Book(47, 50)
fmt.Printf("param = %v obj = %v\n", param2, obj1)
param2 = obj1.Book(33, 41)
fmt.Printf("param = %v obj = %v\n", param2, obj1)
param2 = obj1.Book(39, 45)
fmt.Printf("param = %v obj = %v\n", param2, obj1)
param2 = obj1.Book(33, 42)
fmt.Printf("param = %v obj = %v\n", param2, obj1)
param2 = obj1.Book(25, 32)
fmt.Printf("param = %v obj = %v\n", param2, obj1)
param2 = obj1.Book(26, 35)
fmt.Printf("param = %v obj = %v\n", param2, obj1)
param2 = obj1.Book(19, 25)
fmt.Printf("param = %v obj = %v\n", param2, obj1)
param2 = obj1.Book(3, 8)
fmt.Printf("param = %v obj = %v\n", param2, obj1)
param2 = obj1.Book(8, 13)
fmt.Printf("param = %v obj = %v\n", param2, obj1)
param2 = obj1.Book(18, 27)
fmt.Printf("param = %v obj = %v\n", param2, obj1)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0051.N-Queens/51. N-Queens.go | leetcode/0051.N-Queens/51. N-Queens.go | package leetcode
// 解法一 DFS
func solveNQueens(n int) [][]string {
col, dia1, dia2, row, res := make([]bool, n), make([]bool, 2*n-1), make([]bool, 2*n-1), []int{}, [][]string{}
putQueen(n, 0, &col, &dia1, &dia2, &row, &res)
return res
}
// 尝试在一个n皇后问题中, 摆放第index行的皇后位置
func putQueen(n, index int, col, dia1, dia2 *[]bool, row *[]int, res *[][]string) {
if index == n {
*res = append(*res, generateBoard(n, row))
return
}
for i := 0; i < n; i++ {
// 尝试将第index行的皇后摆放在第i列
if !(*col)[i] && !(*dia1)[index+i] && !(*dia2)[index-i+n-1] {
*row = append(*row, i)
(*col)[i] = true
(*dia1)[index+i] = true
(*dia2)[index-i+n-1] = true
putQueen(n, index+1, col, dia1, dia2, row, res)
(*col)[i] = false
(*dia1)[index+i] = false
(*dia2)[index-i+n-1] = false
*row = (*row)[:len(*row)-1]
}
}
return
}
func generateBoard(n int, row *[]int) []string {
board := []string{}
res := ""
for i := 0; i < n; i++ {
res += "."
}
for i := 0; i < n; i++ {
board = append(board, res)
}
for i := 0; i < n; i++ {
tmp := []byte(board[i])
tmp[(*row)[i]] = 'Q'
board[i] = string(tmp)
}
return board
}
// 解法二 二进制操作法 Signed-off-by: Hanlin Shi shihanlin9@gmail.com
func solveNQueens2(n int) (res [][]string) {
placements := make([]string, n)
for i := range placements {
buf := make([]byte, n)
for j := range placements {
if i == j {
buf[j] = 'Q'
} else {
buf[j] = '.'
}
}
placements[i] = string(buf)
}
var construct func(prev []int)
construct = func(prev []int) {
if len(prev) == n {
plan := make([]string, n)
for i := 0; i < n; i++ {
plan[i] = placements[prev[i]]
}
res = append(res, plan)
return
}
occupied := 0
for i := range prev {
dist := len(prev) - i
bit := 1 << prev[i]
occupied |= bit | bit<<dist | bit>>dist
}
prev = append(prev, -1)
for i := 0; i < n; i++ {
if (occupied>>i)&1 != 0 {
continue
}
prev[len(prev)-1] = i
construct(prev)
}
}
construct(make([]int, 0, n))
return
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0051.N-Queens/51. N-Queens_test.go | leetcode/0051.N-Queens/51. N-Queens_test.go | package leetcode
import (
"fmt"
"testing"
)
type question51 struct {
para51
ans51
}
// para 是参数
// one 代表第一个参数
type para51 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans51 struct {
one [][]string
}
func Test_Problem51(t *testing.T) {
qs := []question51{
{
para51{4},
ans51{[][]string{
{".Q..",
"...Q",
"Q...",
"..Q."},
{"..Q.",
"Q...",
"...Q",
".Q.."},
}},
},
}
fmt.Printf("------------------------Leetcode Problem 51------------------------\n")
for _, q := range qs {
_, p := q.ans51, q.para51
fmt.Printf("【input】:%v 【output】:%v\n", p, solveNQueens(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/0050.Powx-n/50. Pow(x, n).go | leetcode/0050.Powx-n/50. Pow(x, n).go | package leetcode
// 时间复杂度 O(log n),空间复杂度 O(1)
func myPow(x float64, n int) float64 {
if n == 0 {
return 1
}
if n == 1 {
return x
}
if n < 0 {
n = -n
x = 1 / x
}
tmp := myPow(x, n/2)
if n%2 == 0 {
return tmp * tmp
}
return tmp * tmp * x
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0050.Powx-n/50. Pow(x, n)_test.go | leetcode/0050.Powx-n/50. Pow(x, n)_test.go | package leetcode
import (
"fmt"
"testing"
)
type question50 struct {
para50
ans50
}
// para 是参数
// one 代表第一个参数
type para50 struct {
x float64
n int
}
// ans 是答案
// one 代表第一个答案
type ans50 struct {
one float64
}
func Test_Problem50(t *testing.T) {
qs := []question50{
{
para50{2.00000, 10},
ans50{1024.00000},
},
{
para50{2.10000, 3},
ans50{9.26100},
},
{
para50{2.00000, -2},
ans50{0.25000},
},
}
fmt.Printf("------------------------Leetcode Problem 50------------------------\n")
for _, q := range qs {
_, p := q.ans50, q.para50
fmt.Printf("【input】:%v 【output】:%v\n", p, myPow(p.x, 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/1758.Minimum-Changes-To-Make-Alternating-Binary-String/1758. Minimum Changes To Make Alternating Binary String.go | leetcode/1758.Minimum-Changes-To-Make-Alternating-Binary-String/1758. Minimum Changes To Make Alternating Binary String.go | package leetcode
func minOperations(s string) int {
res := 0
for i := 0; i < len(s); i++ {
if int(s[i]-'0') != i%2 {
res++
}
}
return min(res, len(s)-res)
}
func min(a, 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/1758.Minimum-Changes-To-Make-Alternating-Binary-String/1758. Minimum Changes To Make Alternating Binary String_test.go | leetcode/1758.Minimum-Changes-To-Make-Alternating-Binary-String/1758. Minimum Changes To Make Alternating Binary String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1758 struct {
para1758
ans1758
}
// para 是参数
// one 代表第一个参数
type para1758 struct {
s string
}
// ans 是答案
// one 代表第一个答案
type ans1758 struct {
one int
}
func Test_Problem1758(t *testing.T) {
qs := []question1758{
{
para1758{"0100"},
ans1758{1},
},
{
para1758{"10"},
ans1758{0},
},
{
para1758{"1111"},
ans1758{2},
},
}
fmt.Printf("------------------------Leetcode Problem 1758------------------------\n")
for _, q := range qs {
_, p := q.ans1758, q.para1758
fmt.Printf("【input】:%v 【output】:%v\n", p, minOperations(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/1674.Minimum-Moves-to-Make-Array-Complementary/1674. Minimum Moves to Make Array Complementary.go | leetcode/1674.Minimum-Moves-to-Make-Array-Complementary/1674. Minimum Moves to Make Array Complementary.go | package leetcode
func minMoves(nums []int, limit int) int {
diff := make([]int, limit*2+2) // nums[i] <= limit, b+limit+1 is maximum limit+limit+1
for j := 0; j < len(nums)/2; j++ {
a, b := min(nums[j], nums[len(nums)-j-1]), max(nums[j], nums[len(nums)-j-1])
// using prefix sum: most interesting point, and is the key to reduce complexity
diff[2] += 2
diff[a+1]--
diff[a+b]--
diff[a+b+1]++
diff[b+limit+1]++
}
cur, res := 0, len(nums)
for i := 2; i <= 2*limit; i++ {
cur += diff[i]
res = min(res, cur)
}
return res
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
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/1674.Minimum-Moves-to-Make-Array-Complementary/1674. Minimum Moves to Make Array Complementary_test.go | leetcode/1674.Minimum-Moves-to-Make-Array-Complementary/1674. Minimum Moves to Make Array Complementary_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1674 struct {
para1674
ans1674
}
// para 是参数
// one 代表第一个参数
type para1674 struct {
nums []int
limit int
}
// ans 是答案
// one 代表第一个答案
type ans1674 struct {
one int
}
func Test_Problem1674(t *testing.T) {
qs := []question1674{
{
para1674{[]int{1, 2, 4, 3}, 4},
ans1674{1},
},
{
para1674{[]int{1, 2, 2, 1}, 2},
ans1674{2},
},
{
para1674{[]int{1, 2, 1, 2}, 2},
ans1674{0},
},
}
fmt.Printf("------------------------Leetcode Problem 1674------------------------\n")
for _, q := range qs {
_, p := q.ans1674, q.para1674
fmt.Printf("【input】:%v 【output】:%v\n", p, minMoves(p.nums, p.limit))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0107.Binary-Tree-Level-Order-Traversal-II/107. Binary Tree Level Order Traversal II_test.go | leetcode/0107.Binary-Tree-Level-Order-Traversal-II/107. Binary Tree Level Order Traversal II_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question107 struct {
para107
ans107
}
// para 是参数
// one 代表第一个参数
type para107 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans107 struct {
one [][]int
}
func Test_Problem107(t *testing.T) {
qs := []question107{
{
para107{[]int{}},
ans107{[][]int{}},
},
{
para107{[]int{1}},
ans107{[][]int{{1}}},
},
{
para107{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans107{[][]int{{15, 7}, {9, 20}, {3}}},
},
}
fmt.Printf("------------------------Leetcode Problem 107------------------------\n")
for _, q := range qs {
_, p := q.ans107, q.para107
fmt.Printf("【input】:%v ", p)
root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", levelOrderBottom(root))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0107.Binary-Tree-Level-Order-Traversal-II/107. Binary Tree Level Order Traversal II.go | leetcode/0107.Binary-Tree-Level-Order-Traversal-II/107. Binary Tree Level Order Traversal II.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func levelOrderBottom(root *TreeNode) [][]int {
tmp := levelOrder(root)
res := [][]int{}
for i := len(tmp) - 1; i >= 0; i-- {
res = append(res, tmp[i])
}
return res
}
func levelOrder(root *TreeNode) [][]int {
if root == nil {
return [][]int{}
}
queue := []*TreeNode{}
queue = append(queue, root)
curNum, nextLevelNum, res, tmp := 1, 0, [][]int{}, []int{}
for len(queue) != 0 {
if curNum > 0 {
node := queue[0]
if node.Left != nil {
queue = append(queue, node.Left)
nextLevelNum++
}
if node.Right != nil {
queue = append(queue, node.Right)
nextLevelNum++
}
curNum--
tmp = append(tmp, node.Val)
queue = queue[1:]
}
if curNum == 0 {
res = append(res, tmp)
curNum = nextLevelNum
nextLevelNum = 0
tmp = []int{}
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1020.Number-of-Enclaves/1020. Number of Enclaves.go | leetcode/1020.Number-of-Enclaves/1020. Number of Enclaves.go | package leetcode
var dir = [][]int{
{-1, 0},
{0, 1},
{1, 0},
{0, -1},
}
func numEnclaves(A [][]int) int {
m, n := len(A), len(A[0])
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if i == 0 || i == m-1 || j == 0 || j == n-1 {
if A[i][j] == 1 {
dfsNumEnclaves(A, i, j)
}
}
}
}
count := 0
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if A[i][j] == 1 {
count++
}
}
}
return count
}
func dfsNumEnclaves(A [][]int, x, y int) {
if !isInGrid(A, x, y) || A[x][y] == 0 {
return
}
A[x][y] = 0
for i := 0; i < 4; i++ {
nx := x + dir[i][0]
ny := y + dir[i][1]
dfsNumEnclaves(A, nx, ny)
}
}
func isInGrid(grid [][]int, x, y int) bool {
return x >= 0 && x < len(grid) && y >= 0 && y < len(grid[0])
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1020.Number-of-Enclaves/1020. Number of Enclaves_test.go | leetcode/1020.Number-of-Enclaves/1020. Number of Enclaves_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1020 struct {
para1020
ans1020
}
// para 是参数
// one 代表第一个参数
type para1020 struct {
one [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1020 struct {
one int
}
func Test_Problem1020(t *testing.T) {
qs := []question1020{
{
para1020{[][]int{
{1, 1, 1, 1, 0},
{1, 1, 0, 1, 0},
{1, 1, 0, 0, 0},
{0, 0, 0, 0, 0},
}},
ans1020{0},
},
{
para1020{[][]int{
{1, 1, 0, 0, 0},
{1, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 1},
}},
ans1020{1},
},
{
para1020{[][]int{
{1, 1, 1, 1, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 1, 1, 0},
{1, 0, 1, 0, 1, 1, 1, 0},
{1, 0, 0, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 0},
}},
ans1020{1},
},
{
para1020{[][]int{
{0, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{0, 1, 1, 1, 0},
}},
ans1020{0},
},
{
para1020{[][]int{
{1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1},
}},
ans1020{8},
},
}
fmt.Printf("------------------------Leetcode Problem 1020------------------------\n")
for _, q := range qs {
_, p := q.ans1020, q.para1020
fmt.Printf("【input】:%v 【output】:%v\n", p, numEnclaves(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/2171.Removing-Minimum-Number-of-Magic-Beans/2171. Removing Minimum Number of Magic Beans.go | leetcode/2171.Removing-Minimum-Number-of-Magic-Beans/2171. Removing Minimum Number of Magic Beans.go | package leetcode
import "sort"
func minimumRemoval(beans []int) int64 {
sort.Ints(beans)
sum, mx := 0, 0
for i, v := range beans {
sum += v
mx = max(mx, (len(beans)-i)*v)
}
return int64(sum - mx)
}
func max(a, b int) int {
if b > a {
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/2171.Removing-Minimum-Number-of-Magic-Beans/2171. Removing Minimum Number of Magic Beans_test.go | leetcode/2171.Removing-Minimum-Number-of-Magic-Beans/2171. Removing Minimum Number of Magic Beans_test.go | package leetcode
import (
"fmt"
"testing"
)
type question2170 struct {
para2170
ans2170
}
// para 是参数
// one 代表第一个参数
type para2170 struct {
beans []int
}
// ans 是答案
// one 代表第一个答案
type ans2170 struct {
one int
}
func Test_Problem1(t *testing.T) {
qs := []question2170{
{
para2170{[]int{4, 1, 6, 5}},
ans2170{4},
},
{
para2170{[]int{2, 10, 3, 2}},
ans2170{7},
},
}
fmt.Printf("------------------------Leetcode Problem 2170------------------------\n")
for _, q := range qs {
_, p := q.ans2170, q.para2170
fmt.Printf("【input】:%v 【output】:%v\n", p, minimumRemoval(p.beans))
}
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/0993.Cousins-in-Binary-Tree/993. Cousins in Binary Tree_test.go | leetcode/0993.Cousins-in-Binary-Tree/993. Cousins in Binary Tree_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question993 struct {
para993
ans993
}
// para 是参数
// one 代表第一个参数
type para993 struct {
one []int
x int
y int
}
// ans 是答案
// one 代表第一个答案
type ans993 struct {
one bool
}
func Test_Problem993(t *testing.T) {
qs := []question993{
{
para993{[]int{1, 2, 3, 4}, 4, 3},
ans993{false},
},
{
para993{[]int{1, 2, 3, structures.NULL, 4, structures.NULL, 5}, 5, 4},
ans993{true},
},
{
para993{[]int{1, 2, 3, structures.NULL, 4}, 2, 3},
ans993{false},
},
}
fmt.Printf("------------------------Leetcode Problem 993------------------------\n")
for _, q := range qs {
_, p := q.ans993, q.para993
fmt.Printf("【input】:%v ", p)
root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", isCousins(root, p.x, p.y))
}
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/0993.Cousins-in-Binary-Tree/993. Cousins in Binary Tree.go | leetcode/0993.Cousins-in-Binary-Tree/993. Cousins in Binary Tree.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// 解法一 递归
func isCousins(root *TreeNode, x int, y int) bool {
if root == nil {
return false
}
levelX, levelY := findLevel(root, x, 1), findLevel(root, y, 1)
if levelX != levelY {
return false
}
return !haveSameParents(root, x, y)
}
func findLevel(root *TreeNode, x, level int) int {
if root == nil {
return 0
}
if root.Val != x {
leftLevel, rightLevel := findLevel(root.Left, x, level+1), findLevel(root.Right, x, level+1)
if leftLevel == 0 {
return rightLevel
}
return leftLevel
}
return level
}
func haveSameParents(root *TreeNode, x, y int) bool {
if root == nil {
return false
}
if (root.Left != nil && root.Right != nil && root.Left.Val == x && root.Right.Val == y) ||
(root.Left != nil && root.Right != nil && root.Left.Val == y && root.Right.Val == x) {
return true
}
return haveSameParents(root.Left, x, y) || haveSameParents(root.Right, x, y)
}
// 解法二 BFS
type mark struct {
prev int
depth int
}
func isCousinsBFS(root *TreeNode, x int, y int) bool {
if root == nil {
return false
}
queue := []*TreeNode{root}
visited := [101]*mark{}
visited[root.Val] = &mark{prev: -1, depth: 1}
for len(queue) > 0 {
node := queue[0]
queue = queue[1:]
depth := visited[node.Val].depth
if node.Left != nil {
visited[node.Left.Val] = &mark{prev: node.Val, depth: depth + 1}
queue = append(queue, node.Left)
}
if node.Right != nil {
visited[node.Right.Val] = &mark{prev: node.Val, depth: depth + 1}
queue = append(queue, node.Right)
}
}
if visited[x] == nil || visited[y] == nil {
return false
}
if visited[x].depth == visited[y].depth && visited[x].prev != visited[y].prev {
return true
}
return false
}
// 解法三 DFS
func isCousinsDFS(root *TreeNode, x int, y int) bool {
var depth1, depth2, parent1, parent2 int
dfsCousins(root, x, 0, -1, &parent1, &depth1)
dfsCousins(root, y, 0, -1, &parent2, &depth2)
return depth1 > 1 && depth1 == depth2 && parent1 != parent2
}
func dfsCousins(root *TreeNode, val, depth, last int, parent, res *int) {
if root == nil {
return
}
if root.Val == val {
*res = depth
*parent = last
return
}
depth++
dfsCousins(root.Left, val, depth, root.Val, parent, res)
dfsCousins(root.Right, val, depth, root.Val, parent, res)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0457.Circular-Array-Loop/457. Circular Array Loop.go | leetcode/0457.Circular-Array-Loop/457. Circular Array Loop.go | package leetcode
func circularArrayLoop(nums []int) bool {
if len(nums) == 0 {
return false
}
for i := 0; i < len(nums); i++ {
if nums[i] == 0 {
continue
}
// slow/fast pointer
slow, fast, val := i, getNextIndex(nums, i), 0
for nums[fast]*nums[i] > 0 && nums[getNextIndex(nums, fast)]*nums[i] > 0 {
if slow == fast {
// check for loop with only one element
if slow == getNextIndex(nums, slow) {
break
}
return true
}
slow = getNextIndex(nums, slow)
fast = getNextIndex(nums, getNextIndex(nums, fast))
}
// loop not found, set all element along the way to 0
slow, val = i, nums[i]
for nums[slow]*val > 0 {
next := getNextIndex(nums, slow)
nums[slow] = 0
slow = next
}
}
return false
}
func getNextIndex(nums []int, index int) int {
return ((nums[index]+index)%len(nums) + len(nums)) % len(nums)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0457.Circular-Array-Loop/457. Circular Array Loop_test.go | leetcode/0457.Circular-Array-Loop/457. Circular Array Loop_test.go | package leetcode
import (
"fmt"
"testing"
)
type question457 struct {
para457
ans457
}
// para 是参数
// one 代表第一个参数
type para457 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans457 struct {
one bool
}
func Test_Problem457(t *testing.T) {
qs := []question457{
{
para457{[]int{-1}},
ans457{false},
},
{
para457{[]int{3, 1, 2}},
ans457{true},
},
{
para457{[]int{-8, -1, 1, 7, 2}},
ans457{false},
},
{
para457{[]int{-1, -2, -3, -4, -5}},
ans457{false},
},
{
para457{[]int{}},
ans457{false},
},
{
para457{[]int{2, -1, 1, 2, 2}},
ans457{true},
},
{
para457{[]int{-1, 2}},
ans457{false},
},
{
para457{[]int{-2, 1, -1, -2, -2}},
ans457{false},
},
}
fmt.Printf("------------------------Leetcode Problem 457------------------------\n")
for _, q := range qs {
_, p := q.ans457, q.para457
fmt.Printf("【input】:%v 【output】:%v\n", p, circularArrayLoop(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/0622.Design-Circular-Queue/622. Design Circular Queue_test.go | leetcode/0622.Design-Circular-Queue/622. Design Circular Queue_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem622(t *testing.T) {
obj := Constructor(3)
fmt.Printf("obj = %v\n", obj)
param1 := obj.EnQueue(1)
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
param2 := obj.DeQueue()
fmt.Printf("param_1 = %v obj = %v\n", param2, obj)
param3 := obj.Front()
fmt.Printf("param_1 = %v obj = %v\n", param3, obj)
param4 := obj.Rear()
fmt.Printf("param_1 = %v obj = %v\n", param4, obj)
param5 := obj.IsEmpty()
fmt.Printf("param_1 = %v obj = %v\n", param5, obj)
param6 := obj.IsFull()
fmt.Printf("param_1 = %v obj = %v\n", param6, obj)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0622.Design-Circular-Queue/622. Design Circular Queue.go | leetcode/0622.Design-Circular-Queue/622. Design Circular Queue.go | package leetcode
type MyCircularQueue struct {
cap int
size int
queue []int
left int
right int
}
func Constructor(k int) MyCircularQueue {
return MyCircularQueue{cap: k, size: 0, left: 0, right: 0, queue: make([]int, k)}
}
func (this *MyCircularQueue) EnQueue(value int) bool {
if this.size == this.cap {
return false
}
this.size++
this.queue[this.right] = value
this.right++
this.right %= this.cap
return true
}
func (this *MyCircularQueue) DeQueue() bool {
if this.size == 0 {
return false
}
this.size--
this.left++
this.left %= this.cap
return true
}
func (this *MyCircularQueue) Front() int {
if this.size == 0 {
return -1
}
return this.queue[this.left]
}
func (this *MyCircularQueue) Rear() int {
if this.size == 0 {
return -1
}
if this.right == 0 {
return this.queue[this.cap-1]
}
return this.queue[this.right-1]
}
func (this *MyCircularQueue) IsEmpty() bool {
return this.size == 0
}
func (this *MyCircularQueue) IsFull() bool {
return this.size == this.cap
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* obj := Constructor(k);
* param_1 := obj.EnQueue(value);
* param_2 := obj.DeQueue();
* param_3 := obj.Front();
* param_4 := obj.Rear();
* param_5 := obj.IsEmpty();
* param_6 := obj.IsFull();
*/
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0391.Perfect-Rectangle/391.Perfect Rectangle.go | leetcode/0391.Perfect-Rectangle/391.Perfect Rectangle.go | package leetcode
type point struct {
x int
y int
}
func isRectangleCover(rectangles [][]int) bool {
minX, minY, maxA, maxB := rectangles[0][0], rectangles[0][1], rectangles[0][2], rectangles[0][3]
area := 0
cnt := make(map[point]int)
for _, v := range rectangles {
x, y, a, b := v[0], v[1], v[2], v[3]
area += (a - x) * (b - y)
minX = min(minX, x)
minY = min(minY, y)
maxA = max(maxA, a)
maxB = max(maxB, b)
cnt[point{x, y}]++
cnt[point{a, b}]++
cnt[point{x, b}]++
cnt[point{a, y}]++
}
if area != (maxA-minX)*(maxB-minY) ||
cnt[point{minX, minY}] != 1 || cnt[point{maxA, maxB}] != 1 ||
cnt[point{minX, maxB}] != 1 || cnt[point{maxA, minY}] != 1 {
return false
}
delete(cnt, point{minX, minY})
delete(cnt, point{maxA, maxB})
delete(cnt, point{minX, maxB})
delete(cnt, point{maxA, minY})
for _, v := range cnt {
if v != 2 && v != 4 {
return false
}
}
return true
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
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/0391.Perfect-Rectangle/391.Perfect Rectangle_test.go | leetcode/0391.Perfect-Rectangle/391.Perfect Rectangle_test.go | package leetcode
import (
"fmt"
"testing"
)
type question391 struct {
para391
ans391
}
// para 是参数
type para391 struct {
rectangles [][]int
}
// ans 是答案
type ans391 struct {
ans bool
}
func Test_Problem391(t *testing.T) {
qs := []question391{
{
para391{[][]int{{1, 1, 3, 3}, {3, 1, 4, 2}, {3, 2, 4, 4}, {1, 3, 2, 4}, {2, 3, 3, 4}}},
ans391{true},
},
{
para391{[][]int{{1, 1, 2, 3}, {1, 3, 2, 4}, {3, 1, 4, 2}, {3, 2, 4, 4}}},
ans391{false},
},
{
para391{[][]int{{1, 1, 3, 3}, {3, 1, 4, 2}, {1, 3, 2, 4}, {3, 2, 4, 4}}},
ans391{false},
},
{
para391{[][]int{{1, 1, 3, 3}, {3, 1, 4, 2}, {1, 3, 2, 4}, {2, 2, 4, 4}}},
ans391{false},
},
}
fmt.Printf("------------------------Leetcode Problem 391------------------------\n")
for _, q := range qs {
_, p := q.ans391, q.para391
fmt.Printf("【input】:%v 【output】:%v\n", p, isRectangleCover(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/0002.Add-Two-Numbers/2. Add Two Numbers_test.go | leetcode/0002.Add-Two-Numbers/2. Add Two Numbers_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question2 struct {
para2
ans2
}
// para 是参数
// one 代表第一个参数
type para2 struct {
one []int
another []int
}
// ans 是答案
// one 代表第一个答案
type ans2 struct {
one []int
}
func Test_Problem2(t *testing.T) {
qs := []question2{
{
para2{[]int{}, []int{}},
ans2{[]int{}},
},
{
para2{[]int{1}, []int{1}},
ans2{[]int{2}},
},
{
para2{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}},
ans2{[]int{2, 4, 6, 8}},
},
{
para2{[]int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}},
ans2{[]int{2, 4, 6, 8, 0, 1}},
},
{
para2{[]int{1}, []int{9, 9, 9, 9, 9}},
ans2{[]int{0, 0, 0, 0, 0, 1}},
},
{
para2{[]int{9, 9, 9, 9, 9}, []int{1}},
ans2{[]int{0, 0, 0, 0, 0, 1}},
},
{
para2{[]int{2, 4, 3}, []int{5, 6, 4}},
ans2{[]int{7, 0, 8}},
},
{
para2{[]int{1, 8, 3}, []int{7, 1}},
ans2{[]int{8, 9, 3}},
},
// 如需多个测试,可以复制上方元素。
}
fmt.Printf("------------------------Leetcode Problem 2------------------------\n")
for _, q := range qs {
_, p := q.ans2, q.para2
fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(addTwoNumbers(structures.Ints2List(p.one), structures.Ints2List(p.another))))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0002.Add-Two-Numbers/2. Add Two Numbers.go | leetcode/0002.Add-Two-Numbers/2. Add Two Numbers.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 addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
head := &ListNode{Val: 0}
n1, n2, carry, current := 0, 0, 0, head
for l1 != nil || l2 != nil || carry != 0 {
if l1 == nil {
n1 = 0
} else {
n1 = l1.Val
l1 = l1.Next
}
if l2 == nil {
n2 = 0
} else {
n2 = l2.Val
l2 = l2.Next
}
current.Next = &ListNode{Val: (n1 + n2 + carry) % 10}
current = current.Next
carry = (n1 + n2 + carry) / 10
}
return head.Next
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1636.Sort-Array-by-Increasing-Frequency/1636. Sort Array by Increasing Frequency_test.go | leetcode/1636.Sort-Array-by-Increasing-Frequency/1636. Sort Array by Increasing Frequency_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1636 struct {
para1636
ans1636
}
// para 是参数
// one 代表第一个参数
type para1636 struct {
nums []int
}
// ans 是答案
// one 代表第一个答案
type ans1636 struct {
one []int
}
func Test_Problem1636(t *testing.T) {
qs := []question1636{
{
para1636{[]int{1, 1, 2, 2, 2, 3}},
ans1636{[]int{3, 1, 1, 2, 2, 2}},
},
{
para1636{[]int{2, 3, 1, 3, 2}},
ans1636{[]int{1, 3, 3, 2, 2}},
},
{
para1636{[]int{-1, 1, -6, 4, 5, -6, 1, 4, 1}},
ans1636{[]int{5, -1, 4, 4, -6, -6, 1, 1, 1}},
},
}
fmt.Printf("------------------------Leetcode Problem 1636------------------------\n")
for _, q := range qs {
_, p := q.ans1636, q.para1636
fmt.Printf("【input】:%v 【output】:%v \n", p, frequencySort(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/1636.Sort-Array-by-Increasing-Frequency/1636. Sort Array by Increasing Frequency.go | leetcode/1636.Sort-Array-by-Increasing-Frequency/1636. Sort Array by Increasing Frequency.go | package leetcode
import "sort"
func frequencySort(nums []int) []int {
freq := map[int]int{}
for _, v := range nums {
freq[v]++
}
sort.Slice(nums, func(i, j int) bool {
if freq[nums[i]] == freq[nums[j]] {
return nums[j] < nums[i]
}
return freq[nums[i]] < freq[nums[j]]
})
return nums
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1656.Design-an-Ordered-Stream/1656. Design an Ordered Stream.go | leetcode/1656.Design-an-Ordered-Stream/1656. Design an Ordered Stream.go | package leetcode
type OrderedStream struct {
ptr int
stream []string
}
func Constructor(n int) OrderedStream {
ptr, stream := 1, make([]string, n+1)
return OrderedStream{ptr: ptr, stream: stream}
}
func (this *OrderedStream) Insert(id int, value string) []string {
this.stream[id] = value
res := []string{}
if this.ptr == id || this.stream[this.ptr] != "" {
res = append(res, this.stream[this.ptr])
for i := id + 1; i < len(this.stream); i++ {
if this.stream[i] != "" {
res = append(res, this.stream[i])
} else {
this.ptr = i
return res
}
}
}
if len(res) > 0 {
return res
}
return []string{}
}
/**
* Your OrderedStream object will be instantiated and called as such:
* obj := Constructor(n);
* param_1 := obj.Insert(id,value);
*/
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1656.Design-an-Ordered-Stream/1656. Design an Ordered Stream_test.go | leetcode/1656.Design-an-Ordered-Stream/1656. Design an Ordered Stream_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem1656(t *testing.T) {
obj := Constructor(5)
fmt.Printf("obj = %v\n", obj)
param1 := obj.Insert(3, "ccccc")
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
param1 = obj.Insert(1, "aaaaa")
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
param1 = obj.Insert(2, "bbbbb")
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
param1 = obj.Insert(5, "eeeee")
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
param1 = obj.Insert(4, "ddddd")
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0304.Range-Sum-Query-2D-Immutable/304. Range Sum Query 2D - Immutable_test.go | leetcode/0304.Range-Sum-Query-2D-Immutable/304. Range Sum Query 2D - Immutable_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem304(t *testing.T) {
obj := Constructor(
[][]int{
{3, 0, 1, 4, 2},
{5, 6, 3, 2, 1},
{1, 2, 0, 1, 5},
{4, 1, 0, 1, 7},
{1, 0, 3, 0, 5},
},
)
fmt.Printf("obj = %v\n", obj.SumRegion(2, 1, 4, 3))
fmt.Printf("obj = %v\n", obj.SumRegion(1, 1, 2, 2))
fmt.Printf("obj = %v\n", obj.SumRegion(1, 2, 2, 4))
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0304.Range-Sum-Query-2D-Immutable/304. Range Sum Query 2D - Immutable.go | leetcode/0304.Range-Sum-Query-2D-Immutable/304. Range Sum Query 2D - Immutable.go | package leetcode
type NumMatrix struct {
cumsum [][]int
}
func Constructor(matrix [][]int) NumMatrix {
if len(matrix) == 0 {
return NumMatrix{nil}
}
cumsum := make([][]int, len(matrix)+1)
cumsum[0] = make([]int, len(matrix[0])+1)
for i := range matrix {
cumsum[i+1] = make([]int, len(matrix[i])+1)
for j := range matrix[i] {
cumsum[i+1][j+1] = matrix[i][j] + cumsum[i][j+1] + cumsum[i+1][j] - cumsum[i][j]
}
}
return NumMatrix{cumsum}
}
func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
cumsum := this.cumsum
return cumsum[row2+1][col2+1] - cumsum[row1][col2+1] - cumsum[row2+1][col1] + cumsum[row1][col1]
}
/**
* Your NumMatrix object will be instantiated and called as such:
* obj := Constructor(matrix);
* param_1 := obj.SumRegion(row1,col1,row2,col2);
*/
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0097.Interleaving-String/97. Interleaving String.go | leetcode/0097.Interleaving-String/97. Interleaving String.go | package leetcode
func isInterleave(s1 string, s2 string, s3 string) bool {
if len(s1)+len(s2) != len(s3) {
return false
}
visited := make(map[int]bool)
return dfs(s1, s2, s3, 0, 0, visited)
}
func dfs(s1, s2, s3 string, p1, p2 int, visited map[int]bool) bool {
if p1+p2 == len(s3) {
return true
}
if _, ok := visited[(p1*len(s3))+p2]; ok {
return false
}
visited[(p1*len(s3))+p2] = true
var match1, match2 bool
if p1 < len(s1) && s3[p1+p2] == s1[p1] {
match1 = true
}
if p2 < len(s2) && s3[p1+p2] == s2[p2] {
match2 = true
}
if match1 && match2 {
return dfs(s1, s2, s3, p1+1, p2, visited) || dfs(s1, s2, s3, p1, p2+1, visited)
} else if match1 {
return dfs(s1, s2, s3, p1+1, p2, visited)
} else if match2 {
return dfs(s1, s2, s3, p1, p2+1, visited)
} else {
return false
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0097.Interleaving-String/97. Interleaving String_test.go | leetcode/0097.Interleaving-String/97. Interleaving String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question97 struct {
para97
ans97
}
// para 是参数
// one 代表第一个参数
type para97 struct {
s1 string
s2 string
s3 string
}
// ans 是答案
// one 代表第一个答案
type ans97 struct {
one bool
}
func Test_Problem97(t *testing.T) {
qs := []question97{
{
para97{"aabcc", "dbbca", "aadbbcbcac"},
ans97{true},
},
{
para97{"aabcc", "dbbca", "aadbbbaccc"},
ans97{false},
},
{
para97{"", "", ""},
ans97{true},
},
}
fmt.Printf("------------------------Leetcode Problem 97------------------------\n")
for _, q := range qs {
_, p := q.ans97, q.para97
fmt.Printf("【input】:%v 【output】:%v\n", p, isInterleave(p.s1, p.s2, p.s3))
}
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/1154.Day-of-the-Year/1154. Day of the Year.go | leetcode/1154.Day-of-the-Year/1154. Day of the Year.go | package leetcode
import "time"
func dayOfYear(date string) int {
first := date[:4] + "-01-01"
firstDay, _ := time.Parse("2006-01-02", first)
dateDay, _ := time.Parse("2006-01-02", date)
duration := dateDay.Sub(firstDay)
return int(duration.Hours())/24 + 1
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1154.Day-of-the-Year/1154. Day of the Year_test.go | leetcode/1154.Day-of-the-Year/1154. Day of the Year_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1154 struct {
para1154
ans1154
}
// para 是参数
// one 代表第一个参数
type para1154 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans1154 struct {
one int
}
func Test_Problem1154(t *testing.T) {
qs := []question1154{
{
para1154{"2019-01-09"},
ans1154{9},
},
{
para1154{"2019-02-10"},
ans1154{41},
},
{
para1154{"2003-03-01"},
ans1154{60},
},
{
para1154{"2004-03-01"},
ans1154{61},
},
}
fmt.Printf("------------------------Leetcode Problem 1154------------------------\n")
for _, q := range qs {
_, p := q.ans1154, q.para1154
fmt.Printf("【input】:%v 【output】:%v\n", p, dayOfYear(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/0100.Same-Tree/100. Same Tree.go | leetcode/0100.Same-Tree/100. Same Tree.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSameTree(p *TreeNode, q *TreeNode) bool {
if p == nil && q == nil {
return true
} else if p != nil && q != nil {
if p.Val != q.Val {
return false
}
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
} else {
return false
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0100.Same-Tree/100. Same Tree_test.go | leetcode/0100.Same-Tree/100. Same Tree_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question100 struct {
para100
ans100
}
// para 是参数
// one 代表第一个参数
type para100 struct {
one []int
two []int
}
// ans 是答案
// one 代表第一个答案
type ans100 struct {
one bool
}
func Test_Problem100(t *testing.T) {
qs := []question100{
{
para100{[]int{}, []int{}},
ans100{true},
},
{
para100{[]int{}, []int{1}},
ans100{false},
},
{
para100{[]int{1}, []int{1}},
ans100{true},
},
{
para100{[]int{1, 2, 3}, []int{1, 2, 3}},
ans100{true},
},
{
para100{[]int{1, 2}, []int{1, structures.NULL, 2}},
ans100{false},
},
{
para100{[]int{1, 2, 1}, []int{1, 1, 2}},
ans100{false},
},
}
fmt.Printf("------------------------Leetcode Problem 100------------------------\n")
for _, q := range qs {
_, p := q.ans100, q.para100
fmt.Printf("【input】:%v ", p)
rootOne := structures.Ints2TreeNode(p.one)
rootTwo := structures.Ints2TreeNode(p.two)
fmt.Printf("【output】:%v \n", isSameTree(rootOne, rootTwo))
}
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/0661.Image-Smoother/661. Image Smoother.go | leetcode/0661.Image-Smoother/661. Image Smoother.go | package leetcode
func imageSmoother(M [][]int) [][]int {
res := make([][]int, len(M))
for i := range M {
res[i] = make([]int, len(M[0]))
}
for y := 0; y < len(M); y++ {
for x := 0; x < len(M[0]); x++ {
res[y][x] = smooth(x, y, M)
}
}
return res
}
func smooth(x, y int, M [][]int) int {
count, sum := 1, M[y][x]
// Check bottom
if y+1 < len(M) {
sum += M[y+1][x]
count++
}
// Check Top
if y-1 >= 0 {
sum += M[y-1][x]
count++
}
// Check left
if x-1 >= 0 {
sum += M[y][x-1]
count++
}
// Check Right
if x+1 < len(M[y]) {
sum += M[y][x+1]
count++
}
// Check Coners
// Top Left
if y-1 >= 0 && x-1 >= 0 {
sum += M[y-1][x-1]
count++
}
// Top Right
if y-1 >= 0 && x+1 < len(M[0]) {
sum += M[y-1][x+1]
count++
}
// Bottom Left
if y+1 < len(M) && x-1 >= 0 {
sum += M[y+1][x-1]
count++
}
//Bottom Right
if y+1 < len(M) && x+1 < len(M[0]) {
sum += M[y+1][x+1]
count++
}
return sum / count
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0661.Image-Smoother/661. Image Smoother_test.go | leetcode/0661.Image-Smoother/661. Image Smoother_test.go | package leetcode
import (
"fmt"
"testing"
)
type question661 struct {
para661
ans661
}
// para 是参数
// one 代表第一个参数
type para661 struct {
one [][]int
}
// ans 是答案
// one 代表第一个答案
type ans661 struct {
one [][]int
}
func Test_Problem661(t *testing.T) {
qs := []question661{
{
para661{[][]int{{1, 1, 1}, {1, 1, 2}}},
ans661{[][]int{{1, 1, 1}, {1, 1, 1}}},
},
{
para661{[][]int{{1, 1, 1}, {1, 1, 2}, {1, 1, 1}}},
ans661{[][]int{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}},
},
{
para661{[][]int{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}},
ans661{[][]int{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}},
},
}
fmt.Printf("------------------------Leetcode Problem 661------------------------\n")
for _, q := range qs {
_, p := q.ans661, q.para661
fmt.Printf("【input】:%v 【output】:%v\n", p, imageSmoother(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/1319.Number-of-Operations-to-Make-Network-Connected/1319. Number of Operations to Make Network Connected_test.go | leetcode/1319.Number-of-Operations-to-Make-Network-Connected/1319. Number of Operations to Make Network Connected_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1319 struct {
para1319
ans1319
}
// para 是参数
// one 代表第一个参数
type para1319 struct {
n int
connections [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1319 struct {
one int
}
func Test_Problem1319(t *testing.T) {
qs := []question1319{
{
para1319{4, [][]int{{0, 1}, {0, 2}, {1, 2}}},
ans1319{1},
},
{
para1319{6, [][]int{{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}}},
ans1319{2},
},
{
para1319{6, [][]int{{0, 1}, {0, 2}, {0, 3}, {1, 2}}},
ans1319{-1},
},
{
para1319{5, [][]int{{0, 1}, {0, 2}, {3, 4}, {2, 3}}},
ans1319{0},
},
{
para1319{12, [][]int{{1, 5}, {1, 7}, {1, 2}, {1, 4}, {3, 7}, {4, 7}, {3, 5}, {0, 6}, {0, 1}, {0, 4}, {2, 6}, {0, 3}, {0, 2}}},
ans1319{4},
},
}
fmt.Printf("------------------------Leetcode Problem 1319------------------------\n")
for _, q := range qs {
_, p := q.ans1319, q.para1319
fmt.Printf("【input】:%v 【output】:%v\n", p, makeConnected(p.n, p.connections))
}
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/1319.Number-of-Operations-to-Make-Network-Connected/1319. Number of Operations to Make Network Connected.go | leetcode/1319.Number-of-Operations-to-Make-Network-Connected/1319. Number of Operations to Make Network Connected.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/template"
)
func makeConnected(n int, connections [][]int) int {
if n-1 > len(connections) {
return -1
}
uf, redundance := template.UnionFind{}, 0
uf.Init(n)
for _, connection := range connections {
if uf.Find(connection[0]) == uf.Find(connection[1]) {
redundance++
} else {
uf.Union(connection[0], connection[1])
}
}
if uf.TotalCount() == 1 || redundance < uf.TotalCount()-1 {
return 0
}
return uf.TotalCount() - 1
}
| 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.