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/0874.Walking-Robot-Simulation/874. Walking Robot Simulation_test.go | leetcode/0874.Walking-Robot-Simulation/874. Walking Robot Simulation_test.go | package leetcode
import "testing"
func Test_robotSim(t *testing.T) {
type args struct {
commands []int
obstacles [][]int
}
cases := []struct {
name string
args args
want int
}{
{
"case 1",
args{
commands: []int{4, -1, 3},
obstacles: [][]int{{}},
},
25,
},
{
"case 2",
ar... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0874.Walking-Robot-Simulation/874. Walking Robot Simulation.go | leetcode/0874.Walking-Robot-Simulation/874. Walking Robot Simulation.go | package leetcode
func robotSim(commands []int, obstacles [][]int) int {
m := make(map[[2]int]struct{})
for _, v := range obstacles {
if len(v) != 0 {
m[[2]int{v[0], v[1]}] = struct{}{}
}
}
directX := []int{0, 1, 0, -1}
directY := []int{1, 0, -1, 0}
direct, x, y := 0, 0, 0
result := 0
for _, c := range c... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0891.Sum-of-Subsequence-Widths/891. Sum of Subsequence Widths.go | leetcode/0891.Sum-of-Subsequence-Widths/891. Sum of Subsequence Widths.go | package leetcode
import (
"sort"
)
func sumSubseqWidths(A []int) int {
sort.Ints(A)
res, mod, n, p := 0, 1000000007, len(A), 1
for i := 0; i < n; i++ {
res = (res + (A[i]-A[n-1-i])*p) % mod
p = (p << 1) % mod
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0891.Sum-of-Subsequence-Widths/891. Sum of Subsequence Widths_test.go | leetcode/0891.Sum-of-Subsequence-Widths/891. Sum of Subsequence Widths_test.go | package leetcode
import (
"fmt"
"testing"
)
type question891 struct {
para891
ans891
}
// para 是参数
// one 代表第一个参数
type para891 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans891 struct {
one int
}
func Test_Problem891(t *testing.T) {
qs := []question891{
{
para891{[]int{2, 1, 3}},
ans891{6... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0204.Count-Primes/204. Count Primes_test.go | leetcode/0204.Count-Primes/204. Count Primes_test.go | package leetcode
import (
"fmt"
"testing"
)
type question204 struct {
para204
ans204
}
// para 是参数
// one 代表第一个参数
type para204 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans204 struct {
one int
}
func Test_Problem204(t *testing.T) {
qs := []question204{
{
para204{10},
ans204{4},
},
{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0204.Count-Primes/204. Count Primes.go | leetcode/0204.Count-Primes/204. Count Primes.go | package leetcode
func countPrimes(n int) int {
isNotPrime := make([]bool, n)
for i := 2; i*i < n; i++ {
if isNotPrime[i] {
continue
}
for j := i * i; j < n; j = j + i {
isNotPrime[j] = true
}
}
count := 0
for i := 2; i < n; i++ {
if !isNotPrime[i] {
count++
}
}
return count
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1383.Maximum-Performance-of-a-Team/1383. Maximum Performance of a Team_test.go | leetcode/1383.Maximum-Performance-of-a-Team/1383. Maximum Performance of a Team_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1383 struct {
para1383
ans1383
}
// para 是参数
// one 代表第一个参数
type para1383 struct {
n int
speed []int
efficiency []int
k int
}
// ans 是答案
// one 代表第一个答案
type ans1383 struct {
one int
}
func Test_Problem1383(t *testing.T) {
qs... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1383.Maximum-Performance-of-a-Team/1383. Maximum Performance of a Team.go | leetcode/1383.Maximum-Performance-of-a-Team/1383. Maximum Performance of a Team.go | package leetcode
import (
"container/heap"
"sort"
)
func maxPerformance(n int, speed []int, efficiency []int, k int) int {
indexes := make([]int, n)
for i := range indexes {
indexes[i] = i
}
sort.Slice(indexes, func(i, j int) bool {
return efficiency[indexes[i]] > efficiency[indexes[j]]
})
ph := speedHeap... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0904.Fruit-Into-Baskets/904. Fruit Into Baskets_test.go | leetcode/0904.Fruit-Into-Baskets/904. Fruit Into Baskets_test.go | package leetcode
import (
"fmt"
"testing"
)
type question904 struct {
para904
ans904
}
// para 是参数
// one 代表第一个参数
type para904 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans904 struct {
one int
}
func Test_Problem904(t *testing.T) {
qs := []question904{
{
para904{[]int{1, 1}},
ans904{2},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0904.Fruit-Into-Baskets/904. Fruit Into Baskets.go | leetcode/0904.Fruit-Into-Baskets/904. Fruit Into Baskets.go | package leetcode
func totalFruit(tree []int) int {
if len(tree) == 0 {
return 0
}
left, right, counter, res, freq := 0, 0, 1, 1, map[int]int{}
freq[tree[0]]++
for left < len(tree) {
if right+1 < len(tree) && ((counter > 0 && tree[right+1] != tree[left]) || (tree[right+1] == tree[left] || freq[tree[right+1]] >... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0978.Longest-Turbulent-Subarray/978. Longest Turbulent Subarray.go | leetcode/0978.Longest-Turbulent-Subarray/978. Longest Turbulent Subarray.go | package leetcode
// 解法一 模拟法
func maxTurbulenceSize(arr []int) int {
inc, dec := 1, 1
maxLen := min(1, len(arr))
for i := 1; i < len(arr); i++ {
if arr[i-1] < arr[i] {
inc = dec + 1
dec = 1
} else if arr[i-1] > arr[i] {
dec = inc + 1
inc = 1
} else {
inc = 1
dec = 1
}
maxLen = max(maxLen,... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0978.Longest-Turbulent-Subarray/978. Longest Turbulent Subarray_test.go | leetcode/0978.Longest-Turbulent-Subarray/978. Longest Turbulent Subarray_test.go | package leetcode
import (
"fmt"
"testing"
)
type question978 struct {
para978
ans978
}
// para 是参数
// one 代表第一个参数
type para978 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans978 struct {
one int
}
func Test_Problem978(t *testing.T) {
qs := []question978{
{
para978{[]int{0, 1, 1, 0, 1, 0, 1, 1... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0211.Design-Add-and-Search-Words-Data-Structure/211. Design Add and Search Words Data Structure_test.go | leetcode/0211.Design-Add-and-Search-Words-Data-Structure/211. Design Add and Search Words Data Structure_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem211(t *testing.T) {
obj := Constructor211()
fmt.Printf("obj = %v\n", obj)
obj.AddWord("bad")
fmt.Printf("obj = %v\n", obj)
obj.AddWord("dad")
fmt.Printf("obj = %v\n", obj)
obj.AddWord("mad")
fmt.Printf("obj = %v\n", obj)
param1 := obj.Search("pa... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0211.Design-Add-and-Search-Words-Data-Structure/211. Design Add and Search Words Data Structure.go | leetcode/0211.Design-Add-and-Search-Words-Data-Structure/211. Design Add and Search Words Data Structure.go | package leetcode
type WordDictionary struct {
children map[rune]*WordDictionary
isWord bool
}
/** Initialize your data structure here. */
func Constructor211() WordDictionary {
return WordDictionary{children: make(map[rune]*WordDictionary)}
}
/** Adds a word into the data structure. */
func (this *WordDictionar... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0433.Minimum-Genetic-Mutation/433. Minimum Genetic Mutation.go | leetcode/0433.Minimum-Genetic-Mutation/433. Minimum Genetic Mutation.go | package leetcode
// 解法一 BFS
func minMutation(start string, end string, bank []string) int {
wordMap, que, depth := getWordMap(bank, start), []string{start}, 0
for len(que) > 0 {
depth++
qlen := len(que)
for i := 0; i < qlen; i++ {
word := que[0]
que = que[1:]
candidates := getCandidates433(word)
fo... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0433.Minimum-Genetic-Mutation/433. Minimum Genetic Mutation_test.go | leetcode/0433.Minimum-Genetic-Mutation/433. Minimum Genetic Mutation_test.go | package leetcode
import (
"fmt"
"testing"
)
type question433 struct {
para433
ans433
}
// para 是参数
// one 代表第一个参数
type para433 struct {
start string
end string
bank []string
}
// ans 是答案
// one 代表第一个答案
type ans433 struct {
one int
}
func Test_Problem433(t *testing.T) {
qs := []question433{
{
para... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0077.Combinations/77. Combinations_test.go | leetcode/0077.Combinations/77. Combinations_test.go | package leetcode
import (
"fmt"
"testing"
)
type question77 struct {
para77
ans77
}
// para 是参数
// one 代表第一个参数
type para77 struct {
n int
k int
}
// ans 是答案
// one 代表第一个答案
type ans77 struct {
one [][]int
}
func Test_Problem77(t *testing.T) {
qs := []question77{
{
para77{4, 2},
ans77{[][]int{{2, 4... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0077.Combinations/77. Combinations.go | leetcode/0077.Combinations/77. Combinations.go | package leetcode
func combine(n int, k int) [][]int {
if n <= 0 || k <= 0 || k > n {
return [][]int{}
}
c, res := []int{}, [][]int{}
generateCombinations(n, k, 1, c, &res)
return res
}
func generateCombinations(n, k, start int, c []int, res *[][]int) {
if len(c) == k {
b := make([]int, len(c))
copy(b, c)
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0062.Unique-Paths/62. Unique Paths_test.go | leetcode/0062.Unique-Paths/62. Unique Paths_test.go | package leetcode
import (
"fmt"
"testing"
)
type question62 struct {
para62
ans62
}
// para 是参数
// one 代表第一个参数
type para62 struct {
m int
n int
}
// ans 是答案
// one 代表第一个答案
type ans62 struct {
one int
}
func Test_Problem62(t *testing.T) {
qs := []question62{
{
para62{3, 2},
ans62{3},
},
{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0062.Unique-Paths/62. Unique Paths.go | leetcode/0062.Unique-Paths/62. Unique Paths.go | package leetcode
func uniquePaths(m int, n int) int {
dp := make([][]int, n)
for i := 0; i < n; i++ {
dp[i] = make([]int, m)
}
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
if i == 0 || j == 0 {
dp[i][j] = 1
continue
}
dp[i][j] = dp[i-1][j] + dp[i][j-1]
}
}
return dp[n-1][m-1]
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0056.Merge-Intervals/56. Merge Intervals_test.go | leetcode/0056.Merge-Intervals/56. Merge Intervals_test.go | package leetcode
import (
"fmt"
"testing"
)
type question56 struct {
para56
ans56
}
// para 是参数
// one 代表第一个参数
type para56 struct {
one []Interval
}
// ans 是答案
// one 代表第一个答案
type ans56 struct {
one []Interval
}
func Test_Problem56(t *testing.T) {
qs := []question56{
{
para56{[]Interval{}},
ans56{... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0056.Merge-Intervals/56. Merge Intervals.go | leetcode/0056.Merge-Intervals/56. Merge Intervals.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// Interval define
type Interval = structures.Interval
/**
* Definition for an interval.
* type Interval struct {
* Start int
* End int
* }
*/
func merge56(intervals []Interval) []Interval {
if len(intervals) == 0 {
return in... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0138.Copy-List-With-Random-Pointer/138. Copy List With Random Pointer.go | leetcode/0138.Copy-List-With-Random-Pointer/138. Copy List With Random Pointer.go | package leetcode
// Node define
type Node struct {
Val int
Next *Node
Random *Node
}
func copyRandomList(head *Node) *Node {
if head == nil {
return nil
}
tempHead := copyNodeToLinkedList(head)
return splitLinkedList(tempHead)
}
func splitLinkedList(head *Node) *Node {
cur := head
head = head.Next
f... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0138.Copy-List-With-Random-Pointer/138. Copy List With Random Pointer_test.go | leetcode/0138.Copy-List-With-Random-Pointer/138. Copy List With Random Pointer_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question138 struct {
para138
ans138
}
// para 是参数
// one 代表第一个参数
type para138 struct {
head [][]int
}
// ans 是答案
// one 代表第一个答案
type ans138 struct {
one [][]int
}
func Test_Problem138(t *testing.T) {
qs := []que... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1689.Partitioning-Into-Minimum-Number-Of-Deci-Binary-Numbers/1689. Partitioning Into Minimum Number Of Deci-Binary Numbers.go | leetcode/1689.Partitioning-Into-Minimum-Number-Of-Deci-Binary-Numbers/1689. Partitioning Into Minimum Number Of Deci-Binary Numbers.go | package leetcode
func minPartitions(n string) int {
res := 0
for i := 0; i < len(n); i++ {
if int(n[i]-'0') > res {
res = int(n[i] - '0')
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1689.Partitioning-Into-Minimum-Number-Of-Deci-Binary-Numbers/1689. Partitioning Into Minimum Number Of Deci-Binary Numbers_test.go | leetcode/1689.Partitioning-Into-Minimum-Number-Of-Deci-Binary-Numbers/1689. Partitioning Into Minimum Number Of Deci-Binary Numbers_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1689 struct {
para1689
ans1689
}
// para 是参数
// one 代表第一个参数
type para1689 struct {
n string
}
// ans 是答案
// one 代表第一个答案
type ans1689 struct {
one int
}
func Test_Problem1689(t *testing.T) {
qs := []question1689{
{
para1689{"32"},
ans1689{3},... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0725.Split-Linked-List-in-Parts/725. Split Linked List in Parts.go | leetcode/0725.Split-Linked-List-in-Parts/725. Split Linked List in Parts.go | package leetcode
import (
"fmt"
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func splitListToParts(root *ListNode, k int) []*ListNode {
res :=... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0725.Split-Linked-List-in-Parts/725. Split Linked List in Parts_test.go | leetcode/0725.Split-Linked-List-in-Parts/725. Split Linked List in Parts_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question725 struct {
para725
ans725
}
// para 是参数
// one 代表第一个参数
type para725 struct {
one []int
n int
}
// ans 是答案
// one 代表第一个答案
type ans725 struct {
one []int
}
func Test_Problem725(t *testing.T) {
qs := [... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0089.Gray-Code/89. Gray Code_test.go | leetcode/0089.Gray-Code/89. Gray Code_test.go | package leetcode
import (
"fmt"
"testing"
)
type question89 struct {
para89
ans89
}
// para 是参数
// one 代表第一个参数
type para89 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans89 struct {
one []int
}
func Test_Problem89(t *testing.T) {
qs := []question89{
{
para89{2},
ans89{[]int{0, 1, 3, 2}},
}... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0089.Gray-Code/89. Gray Code.go | leetcode/0089.Gray-Code/89. Gray Code.go | package leetcode
// 解法一 递归方法,时间复杂度和空间复杂度都较优
func grayCode(n int) []int {
if n == 0 {
return []int{0}
}
res := []int{}
num := make([]int, n)
generateGrayCode(int(1<<uint(n)), 0, &num, &res)
return res
}
func generateGrayCode(n, step int, num *[]int, res *[]int) {
if n == 0 {
return
}
*res = append(*res, c... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0456.132-Pattern/456. 132 Pattern_test.go | leetcode/0456.132-Pattern/456. 132 Pattern_test.go | package leetcode
import (
"fmt"
"testing"
)
type question456 struct {
para456
ans456
}
// para 是参数
// one 代表第一个参数
type para456 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans456 struct {
one bool
}
func Test_Problem456(t *testing.T) {
qs := []question456{
{
para456{[]int{}},
ans456{false},... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0456.132-Pattern/456. 132 Pattern.go | leetcode/0456.132-Pattern/456. 132 Pattern.go | package leetcode
import (
"math"
)
// 解法一 单调栈
func find132pattern(nums []int) bool {
if len(nums) < 3 {
return false
}
num3, stack := math.MinInt64, []int{}
for i := len(nums) - 1; i >= 0; i-- {
if nums[i] < num3 {
return true
}
for len(stack) != 0 && nums[i] > stack[len(stack)-1] {
num3 = stack[le... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0079.Word-Search/79. Word Search_test.go | leetcode/0079.Word-Search/79. Word Search_test.go | package leetcode
import (
"fmt"
"testing"
)
type question79 struct {
para79
ans79
}
// para 是参数
// one 代表第一个参数
type para79 struct {
b [][]byte
word string
}
// ans 是答案
// one 代表第一个答案
type ans79 struct {
one bool
}
func Test_Problem79(t *testing.T) {
qs := []question79{
{
para79{[][]byte{
{'A'... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0079.Word-Search/79. Word Search.go | leetcode/0079.Word-Search/79. Word Search.go | package leetcode
var dir = [][]int{
{-1, 0},
{0, 1},
{1, 0},
{0, -1},
}
func exist(board [][]byte, word string) bool {
visited := make([][]bool, len(board))
for i := 0; i < len(visited); i++ {
visited[i] = make([]bool, len(board[0]))
}
for i, v := range board {
for j := range v {
if searchWord(board, v... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0645.Set-Mismatch/645. Set Mismatch_test.go | leetcode/0645.Set-Mismatch/645. Set Mismatch_test.go | package leetcode
import (
"fmt"
"testing"
)
type question645 struct {
para645
ans645
}
// para 是参数
// one 代表第一个参数
type para645 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans645 struct {
one []int
}
func Test_Problem645(t *testing.T) {
qs := []question645{
{
para645{[]int{1, 2, 2, 4}},
ans... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0645.Set-Mismatch/645. Set Mismatch.go | leetcode/0645.Set-Mismatch/645. Set Mismatch.go | package leetcode
func findErrorNums(nums []int) []int {
m, res := make([]int, len(nums)), make([]int, 2)
for _, n := range nums {
if m[n-1] == 0 {
m[n-1] = 1
} else {
res[0] = n
}
}
for i := range m {
if m[i] == 0 {
res[1] = i + 1
break
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0470.Implement-Rand10-Using-Rand7/470. Implement Rand10() Using Rand7().go | leetcode/0470.Implement-Rand10-Using-Rand7/470. Implement Rand10() Using Rand7().go | package leetcode
import "math/rand"
func rand10() int {
rand10 := 10
for rand10 >= 10 {
rand10 = (rand7() - 1) + rand7()
}
return rand10%10 + 1
}
func rand7() int {
return rand.Intn(7)
}
func rand101() int {
rand40 := 40
for rand40 >= 40 {
rand40 = (rand7()-1)*7 + rand7() - 1
}
return rand40%10 + 1
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0470.Implement-Rand10-Using-Rand7/470. Implement Rand10() Using Rand7()_test.go | leetcode/0470.Implement-Rand10-Using-Rand7/470. Implement Rand10() Using Rand7()_test.go | package leetcode
import (
"fmt"
"testing"
)
type question470 struct {
para470
ans470
}
// para 是参数
// one 代表第一个参数
type para470 struct {
}
// ans 是答案
// one 代表第一个答案
type ans470 struct {
one int
}
func Test_Problem470(t *testing.T) {
qs := []question470{
{
para470{},
ans470{2},
},
{
para470{}... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0303.Range-Sum-Query-Immutable/303. Range Sum Query - Immutable_test.go | leetcode/0303.Range-Sum-Query-Immutable/303. Range Sum Query - Immutable_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem303(t *testing.T) {
obj := Constructor303([]int{-2, 0, 3, -5, 2, -1})
fmt.Printf("obj = %v\n", obj)
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 2)) // return 1
fmt.Printf("SumRange(2,5) = %v\n", obj.SumRange(2, 5)) // return -1
fmt.Printf("Sum... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0303.Range-Sum-Query-Immutable/303. Range Sum Query - Immutable.go | leetcode/0303.Range-Sum-Query-Immutable/303. Range Sum Query - Immutable.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/template"
)
//解法一 线段树,sumRange 时间复杂度 O(1)
// NumArray define
type NumArray struct {
st *template.SegmentTree
}
// Constructor303 define
func Constructor303(nums []int) NumArray {
st := template.SegmentTree{}
st.Init(nums, func(i, j int) int {
return i... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0013.Roman-to-Integer/13. Roman to Integer_test.go | leetcode/0013.Roman-to-Integer/13. Roman to Integer_test.go | package leetcode
import (
"fmt"
"testing"
)
type question13 struct {
para13
ans13
}
// para 是参数
// one 代表第一个参数
type para13 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans13 struct {
one int
}
func Test_Problem13(t *testing.T) {
qs := []question13{
{
para13{"III"},
ans13{3},
},
{
p... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0013.Roman-to-Integer/13. Roman to Integer.go | leetcode/0013.Roman-to-Integer/13. Roman to Integer.go | package leetcode
var roman = map[string]int{
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000,
}
func romanToInt(s string) int {
if s == "" {
return 0
}
num, lastint, total := 0, 0, 0
for i := 0; i < len(s); i++ {
char := s[len(s)-(i+1) : len(s)-i]
num = roman[char]
if num < lastint ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0461.Hamming-Distance/461. Hamming Distance_test.go | leetcode/0461.Hamming-Distance/461. Hamming Distance_test.go | package leetcode
import (
"fmt"
"testing"
)
type question461 struct {
para461
ans461
}
// para 是参数
// one 代表第一个参数
type para461 struct {
x int
y int
}
// ans 是答案
// one 代表第一个答案
type ans461 struct {
one int
}
func Test_Problem461(t *testing.T) {
qs := []question461{
{
para461{1, 4},
ans461{2},
},... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0461.Hamming-Distance/461. Hamming Distance.go | leetcode/0461.Hamming-Distance/461. Hamming Distance.go | package leetcode
func hammingDistance(x int, y int) int {
distance := 0
for xor := x ^ y; xor != 0; xor &= (xor - 1) {
distance++
}
return distance
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0559.Maximum-Depth-of-N-ary-Tree/559.Maximum Depth of N-ary Tree_test.go | leetcode/0559.Maximum-Depth-of-N-ary-Tree/559.Maximum Depth of N-ary Tree_test.go | package leetcode
import (
"fmt"
"testing"
)
type question559 struct {
para559
ans559
}
// para 是参数
type para559 struct {
root *Node
}
// ans 是答案
type ans559 struct {
ans int
}
func Test_Problem559(t *testing.T) {
qs := []question559{
{
para559{&Node{
Val: 1,
Children: []*Node{
{Val: 3, C... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0559.Maximum-Depth-of-N-ary-Tree/559.Maximum Depth of N-ary Tree.go | leetcode/0559.Maximum-Depth-of-N-ary-Tree/559.Maximum Depth of N-ary Tree.go | package leetcode
type Node struct {
Val int
Children []*Node
}
func maxDepth(root *Node) int {
if root == nil {
return 0
}
return 1 + bfs(root)
}
func bfs(root *Node) int {
var q []*Node
var depth int
q = append(q, root.Children...)
for len(q) != 0 {
depth++
length := len(q)
for length != 0 {
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0763.Partition-Labels/763. Partition Labels_test.go | leetcode/0763.Partition-Labels/763. Partition Labels_test.go | package leetcode
import (
"fmt"
"testing"
)
type question763 struct {
para763
ans763
}
// para 是参数
// one 代表第一个参数
type para763 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans763 struct {
one []int
}
func Test_Problem763(t *testing.T) {
qs := []question763{
{
para763{"ababcbacadefegdehijhklij... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0763.Partition-Labels/763. Partition Labels.go | leetcode/0763.Partition-Labels/763. Partition Labels.go | package leetcode
// 解法一
func partitionLabels(S string) []int {
var lastIndexOf [26]int
for i, v := range S {
lastIndexOf[v-'a'] = i
}
var arr []int
for start, end := 0, 0; start < len(S); start = end + 1 {
end = lastIndexOf[S[start]-'a']
for i := start; i < end; i++ {
if end < lastIndexOf[S[i]-'a'] {
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array/1685. Sum of Absolute Differences in a Sorted Array.go | leetcode/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array/1685. Sum of Absolute Differences in a Sorted Array.go | package leetcode
// 解法一 优化版 prefixSum + sufixSum
func getSumAbsoluteDifferences(nums []int) []int {
size := len(nums)
sufixSum := make([]int, size)
sufixSum[size-1] = nums[size-1]
for i := size - 2; i >= 0; i-- {
sufixSum[i] = sufixSum[i+1] + nums[i]
}
ans, preSum := make([]int, size), 0
for i := 0; i < size;... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array/1685. Sum of Absolute Differences in a Sorted Array_test.go | leetcode/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array/1685. Sum of Absolute Differences in a Sorted Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1685 struct {
para1685
ans1685
}
// para 是参数
// one 代表第一个参数
type para1685 struct {
nums []int
}
// ans 是答案
// one 代表第一个答案
type ans1685 struct {
one []int
}
func Test_Problem1685(t *testing.T) {
qs := []question1685{
{
para1685{[]int{2, 3, 5}},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II_test.go | leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question132 struct {
para132
ans132
}
// para 是参数
// one 代表第一个参数
type para132 struct {
s string
}
// ans 是答案
// one 代表第一个答案
type ans132 struct {
one int
}
func Test_Problem132(t *testing.T) {
qs := []question132{
{
para132{"aab"},
ans132{1},
},
}... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II.go | leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II.go | package leetcode
func minCut(s string) int {
if s == "" {
return 0
}
result := len(s)
current := make([]string, 0, len(s))
dfs132(s, 0, current, &result)
return result
}
func dfs132(s string, idx int, cur []string, result *int) {
start, end := idx, len(s)
if start == end {
*result = min(*result, len(cur)-... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0099.Recover-Binary-Search-Tree/99. Recover Binary Search Tree.go | leetcode/0099.Recover-Binary-Search-Tree/99. Recover Binary Search Tree.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func recoverTree(root *TreeNode) {
var prev, targ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0099.Recover-Binary-Search-Tree/99. Recover Binary Search Tree_test.go | leetcode/0099.Recover-Binary-Search-Tree/99. Recover Binary Search Tree_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question99 struct {
para99
ans99
}
// para 是参数
// one 代表第一个参数
type para99 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans99 struct {
one []int
}
func Test_Problem99(t *testing.T) {
qs := []question99{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0264.Ugly-Number-II/264. Ugly Number II_test.go | leetcode/0264.Ugly-Number-II/264. Ugly Number II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question264 struct {
para264
ans264
}
// para 是参数
// one 代表第一个参数
type para264 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans264 struct {
one int
}
func Test_Problem264(t *testing.T) {
qs := []question264{
{
para264{10},
ans264{12},
},
{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0264.Ugly-Number-II/264. Ugly Number II.go | leetcode/0264.Ugly-Number-II/264. Ugly Number II.go | package leetcode
func nthUglyNumber(n int) int {
dp, p2, p3, p5 := make([]int, n+1), 1, 1, 1
dp[0], dp[1] = 0, 1
for i := 2; i <= n; i++ {
x2, x3, x5 := dp[p2]*2, dp[p3]*3, dp[p5]*5
dp[i] = min(min(x2, x3), x5)
if dp[i] == x2 {
p2++
}
if dp[i] == x3 {
p3++
}
if dp[i] == x5 {
p5++
}
}
retu... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1690.Stone-Game-VII/1690. Stone Game VII_test.go | leetcode/1690.Stone-Game-VII/1690. Stone Game VII_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1690 struct {
para1690
ans1690
}
// para 是参数
// one 代表第一个参数
type para1690 struct {
stones []int
}
// ans 是答案
// one 代表第一个答案
type ans1690 struct {
one int
}
func Test_Problem1690(t *testing.T) {
qs := []question1690{
{
para1690{[]int{5, 3, 1, 4,... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1690.Stone-Game-VII/1690. Stone Game VII.go | leetcode/1690.Stone-Game-VII/1690. Stone Game VII.go | package leetcode
// 解法一 优化空间版 DP
func stoneGameVII(stones []int) int {
n := len(stones)
sum := make([]int, n)
dp := make([]int, n)
for i, d := range stones {
sum[i] = d
}
for i := 1; i < n; i++ {
for j := 0; j+i < n; j++ {
if (n-i)%2 == 1 {
d0 := dp[j] + sum[j]
d1 := dp[j+1] + sum[j+1]
if d0 >... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0104.Maximum-Depth-of-Binary-Tree/104. Maximum Depth of Binary Tree_test.go | leetcode/0104.Maximum-Depth-of-Binary-Tree/104. Maximum Depth of Binary Tree_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question104 struct {
para104
ans104
}
// para 是参数
// one 代表第一个参数
type para104 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans104 struct {
one int
}
func Test_Problem104(t *testing.T) {
qs := []question10... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0104.Maximum-Depth-of-Binary-Tree/104. Maximum Depth of Binary Tree.go | leetcode/0104.Maximum-Depth-of-Binary-Tree/104. Maximum Depth of Binary Tree.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
if root == ni... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0168.Excel-Sheet-Column-Title/168. Excel Sheet Column Title_test.go | leetcode/0168.Excel-Sheet-Column-Title/168. Excel Sheet Column Title_test.go | package leetcode
import (
"fmt"
"testing"
)
type question168 struct {
para168
ans168
}
// para 是参数
// one 代表第一个参数
type para168 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans168 struct {
one string
}
func Test_Problem168(t *testing.T) {
qs := []question168{
{
para168{1},
ans168{"A"},
},
{... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0168.Excel-Sheet-Column-Title/168. Excel Sheet Column Title.go | leetcode/0168.Excel-Sheet-Column-Title/168. Excel Sheet Column Title.go | package leetcode
func convertToTitle(n int) string {
result := []byte{}
for n > 0 {
result = append(result, 'A'+byte((n-1)%26))
n = (n - 1) / 26
}
for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
result[i], result[j] = result[j], result[i]
}
return string(result)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0981.Time-Based-Key-Value-Store/981. Time Based Key-Value Store.go | leetcode/0981.Time-Based-Key-Value-Store/981. Time Based Key-Value Store.go | package leetcode
import "sort"
type data struct {
time int
value string
}
// TimeMap is a timebased key-value store
// TimeMap define
type TimeMap map[string][]data
// Constructor981 define
func Constructor981() TimeMap {
return make(map[string][]data, 1024)
}
// Set define
func (t TimeMap) Set(key string, val... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0981.Time-Based-Key-Value-Store/981. Time Based Key-Value Store_test.go | leetcode/0981.Time-Based-Key-Value-Store/981. Time Based Key-Value Store_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem981(t *testing.T) {
obj := Constructor981()
obj.Set("foo", "bar", 1)
fmt.Printf("Get = %v\n", obj.Get("foo", 1))
fmt.Printf("Get = %v\n", obj.Get("foo", 3))
obj.Set("foo", "bar2", 4)
fmt.Printf("Get = %v\n", obj.Get("foo", 4))
fmt.Printf("Get = %v\... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0191.Number-of-1-Bits/191. Number of 1 Bits.go | leetcode/0191.Number-of-1-Bits/191. Number of 1 Bits.go | package leetcode
import "math/bits"
// 解法一
func hammingWeight(num uint32) int {
return bits.OnesCount(uint(num))
}
// 解法二
func hammingWeight1(num uint32) int {
count := 0
for num != 0 {
num = num & (num - 1)
count++
}
return count
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0191.Number-of-1-Bits/191. Number of 1 Bits_test.go | leetcode/0191.Number-of-1-Bits/191. Number of 1 Bits_test.go | package leetcode
import (
"fmt"
"strconv"
"testing"
)
type question191 struct {
para191
ans191
}
// para 是参数
// one 代表第一个参数
type para191 struct {
one uint32
}
// ans 是答案
// one 代表第一个答案
type ans191 struct {
one int
}
func Test_Problem191(t *testing.T) {
qs := []question191{
{
para191{5},
ans191{1}... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0977.Squares-of-a-Sorted-Array/977. Squares of a Sorted Array.go | leetcode/0977.Squares-of-a-Sorted-Array/977. Squares of a Sorted Array.go | package leetcode
import "sort"
// 解法一
func sortedSquares(A []int) []int {
ans := make([]int, len(A))
for i, k, j := 0, len(A)-1, len(ans)-1; i <= j; k-- {
if A[i]*A[i] > A[j]*A[j] {
ans[k] = A[i] * A[i]
i++
} else {
ans[k] = A[j] * A[j]
j--
}
}
return ans
}
// 解法二
func sortedSquares1(A []int) [... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0977.Squares-of-a-Sorted-Array/977. Squares of a Sorted Array_test.go | leetcode/0977.Squares-of-a-Sorted-Array/977. Squares of a Sorted Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question977 struct {
para977
ans977
}
// para 是参数
// one 代表第一个参数
type para977 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans977 struct {
one []int
}
func Test_Problem977(t *testing.T) {
qs := []question977{
{
para977{[]int{-4, -1, 0, 3, 10}},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0338.Counting-Bits/338. Counting Bits_test.go | leetcode/0338.Counting-Bits/338. Counting Bits_test.go | package leetcode
import (
"fmt"
"testing"
)
type question338 struct {
para338
ans338
}
// para 是参数
// one 代表第一个参数
type para338 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans338 struct {
one []int
}
func Test_Problem338(t *testing.T) {
qs := []question338{
{
para338{2},
ans338{[]int{0, 1, 1}... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0338.Counting-Bits/338. Counting Bits.go | leetcode/0338.Counting-Bits/338. Counting Bits.go | package leetcode
func countBits(num int) []int {
bits := make([]int, num+1)
for i := 1; i <= num; i++ {
bits[i] += bits[i&(i-1)] + 1
}
return bits
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0880.Decoded-String-at-Index/880. Decoded String at Index_test.go | leetcode/0880.Decoded-String-at-Index/880. Decoded String at Index_test.go | package leetcode
import (
"fmt"
"testing"
)
type question880 struct {
para880
ans880
}
// para 是参数
// one 代表第一个参数
type para880 struct {
s string
k int
}
// ans 是答案
// one 代表第一个答案
type ans880 struct {
one string
}
func Test_Problem880(t *testing.T) {
qs := []question880{
{
para880{"aw4eguc6cs", 41},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0880.Decoded-String-at-Index/880. Decoded String at Index.go | leetcode/0880.Decoded-String-at-Index/880. Decoded String at Index.go | package leetcode
func isLetter(char byte) bool {
if char >= 'a' && char <= 'z' {
return true
}
return false
}
func decodeAtIndex(S string, K int) string {
length := 0
for i := 0; i < len(S); i++ {
if isLetter(S[i]) {
length++
if length == K {
return string(S[i])
}
} else {
if length*int(S[i... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0210.Course-Schedule-II/210. Course Schedule II.go | leetcode/0210.Course-Schedule-II/210. Course Schedule II.go | package leetcode
func findOrder(numCourses int, prerequisites [][]int) []int {
in := make([]int, numCourses)
frees := make([][]int, numCourses)
next := make([]int, 0, numCourses)
for _, v := range prerequisites {
in[v[0]]++
frees[v[1]] = append(frees[v[1]], v[0])
}
for i := 0; i < numCourses; i++ {
if in[i... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0210.Course-Schedule-II/210. Course Schedule II_test.go | leetcode/0210.Course-Schedule-II/210. Course Schedule II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question210 struct {
para210
ans210
}
// para 是参数
// one 代表第一个参数
type para210 struct {
one int
pre [][]int
}
// ans 是答案
// one 代表第一个答案
type ans210 struct {
one []int
}
func Test_Problem210(t *testing.T) {
qs := []question210{
{
para210{2, [][]int{{1, ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1018.Binary-Prefix-Divisible-By-5/1018. Binary Prefix Divisible By 5.go | leetcode/1018.Binary-Prefix-Divisible-By-5/1018. Binary Prefix Divisible By 5.go | package leetcode
func prefixesDivBy5(a []int) []bool {
res, num := make([]bool, len(a)), 0
for i, v := range a {
num = (num<<1 | v) % 5
res[i] = num == 0
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1018.Binary-Prefix-Divisible-By-5/1018. Binary Prefix Divisible By 5_test.go | leetcode/1018.Binary-Prefix-Divisible-By-5/1018. Binary Prefix Divisible By 5_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1018 struct {
para1018
ans1018
}
// para 是参数
// one 代表第一个参数
type para1018 struct {
a []int
}
// ans 是答案
// one 代表第一个答案
type ans1018 struct {
one []bool
}
func Test_Problem1018(t *testing.T) {
qs := []question1018{
{
para1018{[]int{0, 1, 1}},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0793.Preimage-Size-of-Factorial-Zeroes-Function/793. Preimage Size of Factorial Zeroes Function.go | leetcode/0793.Preimage-Size-of-Factorial-Zeroes-Function/793. Preimage Size of Factorial Zeroes Function.go | package leetcode
// 解法一 二分搜索
func preimageSizeFZF(K int) int {
low, high := 0, 5*K
for low <= high {
mid := low + (high-low)>>1
k := trailingZeroes(mid)
if k == K {
return 5
} else if k > K {
high = mid - 1
} else {
low = mid + 1
}
}
return 0
}
func trailingZeroes(n int) int {
if n/5 == 0 {
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0793.Preimage-Size-of-Factorial-Zeroes-Function/793. Preimage Size of Factorial Zeroes Function_test.go | leetcode/0793.Preimage-Size-of-Factorial-Zeroes-Function/793. Preimage Size of Factorial Zeroes Function_test.go | package leetcode
import (
"fmt"
"testing"
)
type question793 struct {
para793
ans793
}
// para 是参数
// one 代表第一个参数
type para793 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans793 struct {
one int
}
func Test_Problem793(t *testing.T) {
qs := []question793{
{
para793{0},
ans793{5},
},
{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0215.Kth-Largest-Element-in-an-Array/215. Kth Largest Element in an Array.go | leetcode/0215.Kth-Largest-Element-in-an-Array/215. Kth Largest Element in an Array.go | package leetcode
import (
"math/rand"
"sort"
)
// 解法一 排序,排序的方法反而速度是最快的
func findKthLargest1(nums []int, k int) int {
sort.Ints(nums)
return nums[len(nums)-k]
}
// 解法二 这个方法的理论依据是 partition 得到的点的下标就是最终排序之后的下标,根据这个下标,我们可以判断第 K 大的数在哪里
// 时间复杂度 O(n),空间复杂度 O(log n),最坏时间复杂度为 O(n^2),空间复杂度 O(n)
func findKthLargest(nums [... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0215.Kth-Largest-Element-in-an-Array/215. Kth Largest Element in an Array_test.go | leetcode/0215.Kth-Largest-Element-in-an-Array/215. Kth Largest Element in an Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question215 struct {
para215
ans215
}
// para 是参数
// one 代表第一个参数
type para215 struct {
one []int
two int
}
// ans 是答案
// one 代表第一个答案
type ans215 struct {
one int
}
func Test_Problem215(t *testing.T) {
qs := []question215{
{
para215{[]int{3, 2, 1}, 2},... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring.go | leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring.go | package leetcode
// 解法一 二分搜索 + Rabin-Karp
func longestDupSubstring(S string) string {
// low, high := 0, len(S)
// for low < high {
// mid := (low + high + 1) >> 1
// if hasRepeated("", B, mid) {
// low = mid
// } else {
// high = mid - 1
// }
// }
return "这个解法还有问题!"
}
// func hashSlice(arr []int, l... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring_test.go | leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1044 struct {
para1044
ans1044
}
// para 是参数
// one 代表第一个参数
type para1044 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans1044 struct {
one string
}
func Test_Problem1044(t *testing.T) {
qs := []question1044{
{
para1044{"banana"},
ans... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1658.Minimum-Operations-to-Reduce-X-to-Zero/1658. Minimum Operations to Reduce X to Zero.go | leetcode/1658.Minimum-Operations-to-Reduce-X-to-Zero/1658. Minimum Operations to Reduce X to Zero.go | package leetcode
func minOperations(nums []int, x int) int {
total := 0
for _, n := range nums {
total += n
}
target := total - x
if target < 0 {
return -1
}
if target == 0 {
return len(nums)
}
left, right, sum, res := 0, 0, 0, -1
for right < len(nums) {
if sum < target {
sum += nums[right]
rig... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1658.Minimum-Operations-to-Reduce-X-to-Zero/1658. Minimum Operations to Reduce X to Zero_test.go | leetcode/1658.Minimum-Operations-to-Reduce-X-to-Zero/1658. Minimum Operations to Reduce X to Zero_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1658 struct {
para1658
ans1658
}
// para 是参数
// one 代表第一个参数
type para1658 struct {
nums []int
x int
}
// ans 是答案
// one 代表第一个答案
type ans1658 struct {
one int
}
func Test_Problem1658(t *testing.T) {
qs := []question1658{
{
para1658{[]int{1, ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0227.Basic-Calculator-II/227. Basic Calculator II_test.go | leetcode/0227.Basic-Calculator-II/227. Basic Calculator II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question227 struct {
para227
ans227
}
// para 是参数
// one 代表第一个参数
type para227 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans227 struct {
one int
}
func Test_Problem227(t *testing.T) {
qs := []question227{
{
para227{"3+2*2"},
ans227{7},
}... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0227.Basic-Calculator-II/227. Basic Calculator II.go | leetcode/0227.Basic-Calculator-II/227. Basic Calculator II.go | package leetcode
func calculate(s string) int {
stack, preSign, num, res := []int{}, '+', 0, 0
for i, ch := range s {
isDigit := '0' <= ch && ch <= '9'
if isDigit {
num = num*10 + int(ch-'0')
}
if !isDigit && ch != ' ' || i == len(s)-1 {
switch preSign {
case '+':
stack = append(stack, num)
c... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1680.Concatenation-of-Consecutive-Binary-Numbers/1680. Concatenation of Consecutive Binary Numbers_test.go | leetcode/1680.Concatenation-of-Consecutive-Binary-Numbers/1680. Concatenation of Consecutive Binary Numbers_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1680 struct {
para1680
ans1680
}
// para 是参数
// one 代表第一个参数
type para1680 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans1680 struct {
one int
}
func Test_Problem1680(t *testing.T) {
qs := []question1680{
{
para1680{1},
ans1680{1},
},
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1680.Concatenation-of-Consecutive-Binary-Numbers/1680. Concatenation of Consecutive Binary Numbers.go | leetcode/1680.Concatenation-of-Consecutive-Binary-Numbers/1680. Concatenation of Consecutive Binary Numbers.go | package leetcode
import (
"math/bits"
)
// 解法一 模拟
func concatenatedBinary(n int) int {
res, mod, shift := 0, 1000000007, 0
for i := 1; i <= n; i++ {
if (i & (i - 1)) == 0 {
shift++
}
res = ((res << shift) + i) % mod
}
return res
}
// 解法二 位运算
func concatenatedBinary1(n int) int {
res := 0
for i := 1; ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0119.Pascals-Triangle-II/119. Pascals Triangle II_test.go | leetcode/0119.Pascals-Triangle-II/119. Pascals Triangle II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question119 struct {
para119
ans119
}
// para 是参数
// one 代表第一个参数
type para119 struct {
rowIndex int
}
// ans 是答案
// one 代表第一个答案
type ans119 struct {
one []int
}
func Test_Problem119(t *testing.T) {
qs := []question119{
{
para119{3},
ans119{[]int{1, ... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0119.Pascals-Triangle-II/119. Pascals Triangle II.go | leetcode/0119.Pascals-Triangle-II/119. Pascals Triangle II.go | package leetcode
func getRow(rowIndex int) []int {
row := make([]int, rowIndex+1)
row[0] = 1
for i := 1; i <= rowIndex; i++ {
row[i] = row[i-1] * (rowIndex - i + 1) / i
}
return row
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0526.Beautiful-Arrangement/526. Beautiful Arrangement.go | leetcode/0526.Beautiful-Arrangement/526. Beautiful Arrangement.go | package leetcode
// 解法一 暴力打表法
func countArrangement1(N int) int {
res := []int{0, 1, 2, 3, 8, 10, 36, 41, 132, 250, 700, 750, 4010, 4237, 10680, 24679, 87328, 90478, 435812}
return res[N]
}
// 解法二 DFS 回溯
func countArrangement(N int) int {
if N == 0 {
return 0
}
nums, used, p, res := make([]int, N), make([]bool... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0526.Beautiful-Arrangement/526. Beautiful Arrangement_test.go | leetcode/0526.Beautiful-Arrangement/526. Beautiful Arrangement_test.go | package leetcode
import (
"fmt"
"testing"
)
type question526 struct {
para526
ans526
}
// para 是参数
// one 代表第一个参数
type para526 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans526 struct {
one int
}
func Test_Problem526(t *testing.T) {
qs := []question526{
{
para526{1},
ans526{1},
},
{
... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0300.Longest-Increasing-Subsequence/300. Longest Increasing Subsequence_test.go | leetcode/0300.Longest-Increasing-Subsequence/300. Longest Increasing Subsequence_test.go | package leetcode
import (
"fmt"
"testing"
)
type question300 struct {
para300
ans300
}
// para 是参数
// one 代表第一个参数
type para300 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans300 struct {
one int
}
func Test_Problem300(t *testing.T) {
qs := []question300{
{
para300{[]int{10, 9, 2, 5, 3, 7, 101... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0300.Longest-Increasing-Subsequence/300. Longest Increasing Subsequence.go | leetcode/0300.Longest-Increasing-Subsequence/300. Longest Increasing Subsequence.go | package leetcode
import "sort"
// 解法一 O(n^2) DP
func lengthOfLIS(nums []int) int {
dp, res := make([]int, len(nums)+1), 0
dp[0] = 0
for i := 1; i <= len(nums); i++ {
for j := 1; j < i; j++ {
if nums[j-1] < nums[i-1] {
dp[i] = max(dp[i], dp[j])
}
}
dp[i] = dp[i] + 1
res = max(res, dp[i])
}
retur... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0816.Ambiguous-Coordinates/816. Ambiguous Coordinates_test.go | leetcode/0816.Ambiguous-Coordinates/816. Ambiguous Coordinates_test.go | package leetcode
import (
"fmt"
"testing"
)
type question816 struct {
para816
ans816
}
// para 是参数
// one 代表第一个参数
type para816 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans816 struct {
one []string
}
func Test_Problem816(t *testing.T) {
qs := []question816{
{
para816{"(120123)"},
ans816... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0816.Ambiguous-Coordinates/816. Ambiguous Coordinates.go | leetcode/0816.Ambiguous-Coordinates/816. Ambiguous Coordinates.go | package leetcode
func ambiguousCoordinates(s string) []string {
res := []string{}
s = s[1 : len(s)-1]
for i := range s[:len(s)-1] {
a := build(s[:i+1])
b := build(s[i+1:])
for _, ta := range a {
for _, tb := range b {
res = append(res, "("+ta+", "+tb+")")
}
}
}
return res
}
func build(s string)... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0767.Reorganize-String/767. Reorganize String.go | leetcode/0767.Reorganize-String/767. Reorganize String.go | package leetcode
import (
"sort"
)
func reorganizeString(S string) string {
fs := frequencySort767(S)
if fs == "" {
return ""
}
bs := []byte(fs)
ans := ""
j := (len(bs)-1)/2 + 1
for i := 0; i <= (len(bs)-1)/2; i++ {
ans += string(bs[i])
if j < len(bs) {
ans += string(bs[j])
}
j++
}
return ans
}... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0767.Reorganize-String/767. Reorganize String_test.go | leetcode/0767.Reorganize-String/767. Reorganize String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question767 struct {
para767
ans767
}
// para 是参数
// one 代表第一个参数
type para767 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans767 struct {
one string
}
func Test_Problem767(t *testing.T) {
qs := []question767{
{
para767{"aab"},
ans767{"aba"},... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0515.Find-Largest-Value-in-Each-Tree-Row/515. Find Largest Value in Each Tree Row.go | leetcode/0515.Find-Largest-Value-in-Each-Tree-Row/515. Find Largest Value in Each Tree Row.go | package leetcode
import (
"math"
"sort"
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// 解法一 层序遍历二叉树,再将每层排序取出最大值
func l... | go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0515.Find-Largest-Value-in-Each-Tree-Row/515. Find Largest Value in Each Tree Row_test.go | leetcode/0515.Find-Largest-Value-in-Each-Tree-Row/515. Find Largest Value in Each Tree Row_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question515 struct {
para515
ans515
}
// para 是参数
// one 代表第一个参数
type para515 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans515 struct {
one []int
}
func Test_Problem515(t *testing.T) {
qs := []question... | 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.