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/0111.Minimum-Depth-of-Binary-Tree/111. Minimum Depth of Binary Tree.go
leetcode/0111.Minimum-Depth-of-Binary-Tree/111. Minimum 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 minDepth(root *TreeNode) int { if root == nil { return 0 } if root.Left == nil { return minDepth(root.Right) + 1 } if root.Right == nil { return minDepth(root.Left) + 1 } return min(minDepth(root.Left), minDepth(root.Right)) + 1 } func min(a int, b int) int { if a > b { return b } return a }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0111.Minimum-Depth-of-Binary-Tree/111. Minimum Depth of Binary Tree_test.go
leetcode/0111.Minimum-Depth-of-Binary-Tree/111. Minimum Depth of Binary Tree_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question111 struct { para111 ans111 } // para 是参数 // one 代表第一个参数 type para111 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans111 struct { one int } func Test_Problem111(t *testing.T) { qs := []question111{ { para111{[]int{}}, ans111{0}, }, { para111{[]int{1}}, ans111{1}, }, { para111{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}}, ans111{2}, }, { para111{[]int{1, 2}}, ans111{2}, }, } fmt.Printf("------------------------Leetcode Problem 111------------------------\n") for _, q := range qs { _, p := q.ans111, q.para111 fmt.Printf("【input】:%v ", p) root := structures.Ints2TreeNode(p.one) fmt.Printf("【output】:%v \n", minDepth(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/0477.Total-Hamming-Distance/477. Total Hamming Distance.go
leetcode/0477.Total-Hamming-Distance/477. Total Hamming Distance.go
package leetcode func totalHammingDistance(nums []int) int { total, n := 0, len(nums) for i := 0; i < 32; i++ { bitCount := 0 for j := 0; j < n; j++ { bitCount += (nums[j] >> uint(i)) & 1 } total += bitCount * (n - bitCount) } return total } // 暴力解法超时! func totalHammingDistance1(nums []int) int { res := 0 for i := 0; i < len(nums); i++ { for j := i + 1; j < len(nums); j++ { res += hammingDistance(nums[i], nums[j]) } } return res } 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/0477.Total-Hamming-Distance/477. Total Hamming Distance_test.go
leetcode/0477.Total-Hamming-Distance/477. Total Hamming Distance_test.go
package leetcode import ( "fmt" "testing" ) type question477 struct { para477 ans477 } // para 是参数 // one 代表第一个参数 type para477 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans477 struct { one int } func Test_Problem477(t *testing.T) { qs := []question477{ { para477{[]int{4, 14, 2}}, ans477{6}, }, } fmt.Printf("------------------------Leetcode Problem 477------------------------\n") for _, q := range qs { _, p := q.ans477, q.para477 fmt.Printf("【input】:%v 【output】:%v\n", p, totalHammingDistance(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/0696.Count-Binary-Substrings/696. Count Binary Substrings.go
leetcode/0696.Count-Binary-Substrings/696. Count Binary Substrings.go
package leetcode func countBinarySubstrings(s string) int { last, res := 0, 0 for i := 0; i < len(s); { c, count := s[i], 1 for i++; i < len(s) && s[i] == c; i++ { count++ } res += min(count, last) last = count } return res } func min(a, b int) int { if a < b { return a } return b }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0696.Count-Binary-Substrings/696. Count Binary Substrings_test.go
leetcode/0696.Count-Binary-Substrings/696. Count Binary Substrings_test.go
package leetcode import ( "fmt" "testing" ) type question696 struct { para696 ans696 } // para 是参数 // one 代表第一个参数 type para696 struct { one string } // ans 是答案 // one 代表第一个答案 type ans696 struct { one int } func Test_Problem696(t *testing.T) { qs := []question696{ { para696{"00110011"}, ans696{6}, }, { para696{"10101"}, ans696{4}, }, { para696{"0110001111"}, ans696{6}, }, { para696{"0001111"}, ans696{3}, }, } fmt.Printf("------------------------Leetcode Problem 696------------------------\n") for _, q := range qs { _, p := q.ans696, q.para696 fmt.Printf("【input】:%v 【output】:%v\n", p, countBinarySubstrings(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/0598.Range-Addition-II/598. Range Addition II.go
leetcode/0598.Range-Addition-II/598. Range Addition II.go
package leetcode func maxCount(m int, n int, ops [][]int) int { minM, minN := m, n for _, op := range ops { minM = min(minM, op[0]) minN = min(minN, op[1]) } return minM * minN } func min(a, b int) int { if a < b { return a } return b }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0598.Range-Addition-II/598. Range Addition II_test.go
leetcode/0598.Range-Addition-II/598. Range Addition II_test.go
package leetcode import ( "fmt" "testing" ) type question598 struct { para598 ans598 } // para 是参数 // one 代表第一个参数 type para598 struct { m int n int ops [][]int } // ans 是答案 // one 代表第一个答案 type ans598 struct { one int } func Test_Problem598(t *testing.T) { qs := []question598{ { para598{3, 3, [][]int{{2, 2}, {3, 3}}}, ans598{4}, }, } fmt.Printf("------------------------Leetcode Problem 598------------------------\n") for _, q := range qs { _, p := q.ans598, q.para598 fmt.Printf("【input】:%v 【output】:%v\n", p, maxCount(p.m, p.n, p.ops)) } 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/0970.Powerful-Integers/970. Powerful Integers_test.go
leetcode/0970.Powerful-Integers/970. Powerful Integers_test.go
package leetcode import ( "fmt" "testing" ) type question970 struct { para970 ans970 } // para 是参数 // one 代表第一个参数 type para970 struct { one int two int b int } // ans 是答案 // one 代表第一个答案 type ans970 struct { one []int } func Test_Problem970(t *testing.T) { qs := []question970{ { para970{2, 3, 10}, ans970{[]int{2, 3, 4, 5, 7, 9, 10}}, }, { para970{3, 5, 15}, ans970{[]int{2, 4, 6, 8, 10, 14}}, }, } fmt.Printf("------------------------Leetcode Problem 970------------------------\n") for _, q := range qs { _, p := q.ans970, q.para970 fmt.Printf("【input】:%v 【output】:%v\n", p, powerfulIntegers(p.one, p.two, p.b)) } 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/0970.Powerful-Integers/970. Powerful Integers.go
leetcode/0970.Powerful-Integers/970. Powerful Integers.go
package leetcode import "math" func powerfulIntegers(x int, y int, bound int) []int { if x == 1 && y == 1 { if bound < 2 { return []int{} } return []int{2} } if x > y { x, y = y, x } visit, result := make(map[int]bool), make([]int, 0) for i := 0; ; i++ { found := false for j := 0; pow(x, i)+pow(y, j) <= bound; j++ { v := pow(x, i) + pow(y, j) if !visit[v] { found = true visit[v] = true result = append(result, v) } } if !found { break } } return result } func pow(x, i int) int { return int(math.Pow(float64(x), float64(i))) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0400.Nth-Digit/400.Nth Digit.go
leetcode/0400.Nth-Digit/400.Nth Digit.go
package leetcode import "math" func findNthDigit(n int) int { if n <= 9 { return n } bits := 1 for n > 9*int(math.Pow10(bits-1))*bits { n -= 9 * int(math.Pow10(bits-1)) * bits bits++ } idx := n - 1 start := int(math.Pow10(bits - 1)) num := start + idx/bits digitIdx := idx % bits return num / int(math.Pow10(bits-digitIdx-1)) % 10 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0400.Nth-Digit/400.Nth Digit_test.go
leetcode/0400.Nth-Digit/400.Nth Digit_test.go
package leetcode import ( "fmt" "testing" ) type question400 struct { para400 ans400 } // para 是参数 type para400 struct { n int } // ans 是答案 type ans400 struct { ans int } func Test_Problem400(t *testing.T) { qs := []question400{ { para400{3}, ans400{3}, }, { para400{11}, ans400{0}, }, } fmt.Printf("------------------------Leetcode Problem 400------------------------\n") for _, q := range qs { _, p := q.ans400, q.para400 fmt.Printf("【input】:%v 【output】:%v\n", p.n, findNthDigit(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/0498.Diagonal-Traverse/498. Diagonal Traverse_test.go
leetcode/0498.Diagonal-Traverse/498. Diagonal Traverse_test.go
package leetcode import ( "fmt" "testing" ) type question498 struct { para498 ans498 } // para 是参数 // one 代表第一个参数 type para498 struct { one [][]int } // ans 是答案 // one 代表第一个答案 type ans498 struct { one []int } func Test_Problem498(t *testing.T) { qs := []question498{ { para498{[][]int{{3}, {2}, {9}}}, ans498{[]int{3, 2, 9}}, }, { para498{[][]int{{6, 9, 7}}}, ans498{[]int{6, 9, 7}}, }, { para498{[][]int{{3}, {2}}}, ans498{[]int{3, 2}}, }, { para498{[][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}, ans498{[]int{1, 2, 4, 7, 5, 3, 6, 8, 9}}, }, { para498{[][]int{{0}}}, ans498{[]int{0}}, }, { para498{[][]int{{}}}, ans498{[]int{}}, }, } fmt.Printf("------------------------Leetcode Problem 498------------------------\n") for _, q := range qs { _, p := q.ans498, q.para498 fmt.Printf("【input】:%v 【output】:%v\n", p, findDiagonalOrder(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/0498.Diagonal-Traverse/498. Diagonal Traverse.go
leetcode/0498.Diagonal-Traverse/498. Diagonal Traverse.go
package leetcode // 解法一 func findDiagonalOrder1(matrix [][]int) []int { if matrix == nil || len(matrix) == 0 || len(matrix[0]) == 0 { return nil } row, col, dir, i, x, y, d := len(matrix), len(matrix[0]), [2][2]int{ {-1, 1}, {1, -1}, }, 0, 0, 0, 0 total := row * col res := make([]int, total) for i < total { for x >= 0 && x < row && y >= 0 && y < col { res[i] = matrix[x][y] i++ x += dir[d][0] y += dir[d][1] } d = (d + 1) % 2 if x == row { x-- y += 2 } if y == col { y-- x += 2 } if x < 0 { x = 0 } if y < 0 { y = 0 } } return res } // 解法二 func findDiagonalOrder(matrix [][]int) []int { if len(matrix) == 0 { return []int{} } if len(matrix) == 1 { return matrix[0] } // dir = 0 代表从右上到左下的方向, dir = 1 代表从左下到右上的方向 dir = -1 代表上一次转变了方向 m, n, i, j, dir, res := len(matrix), len(matrix[0]), 0, 0, 0, []int{} for index := 0; index < m*n; index++ { if dir == -1 { if (i == 0 && j < n-1) || (j == n-1) { // 上边界和右边界 i++ if j > 0 { j-- } dir = 0 addTraverse(matrix, i, j, &res) continue } if (j == 0 && i < m-1) || (i == m-1) { // 左边界和下边界 if i > 0 { i-- } j++ dir = 1 addTraverse(matrix, i, j, &res) continue } } if i == 0 && j == 0 { res = append(res, matrix[i][j]) if j < n-1 { j++ dir = -1 addTraverse(matrix, i, j, &res) continue } else { if i < m-1 { i++ dir = -1 addTraverse(matrix, i, j, &res) continue } } } if i == 0 && j < n-1 { // 上边界 if j < n-1 { j++ dir = -1 addTraverse(matrix, i, j, &res) continue } } if j == 0 && i < m-1 { // 左边界 if i < m-1 { i++ dir = -1 addTraverse(matrix, i, j, &res) continue } } if j == n-1 { // 右边界 if i < m-1 { i++ dir = -1 addTraverse(matrix, i, j, &res) continue } } if i == m-1 { // 下边界 j++ dir = -1 addTraverse(matrix, i, j, &res) continue } if dir == 1 { i-- j++ addTraverse(matrix, i, j, &res) continue } if dir == 0 { i++ j-- addTraverse(matrix, i, j, &res) continue } } return res } func addTraverse(matrix [][]int, i, j int, res *[]int) { if i >= 0 && i <= len(matrix)-1 && j >= 0 && j <= len(matrix[0])-1 { *res = append(*res, matrix[i][j]) } }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1655.Distribute-Repeating-Integers/1655. Distribute Repeating Integers_test.go
leetcode/1655.Distribute-Repeating-Integers/1655. Distribute Repeating Integers_test.go
package leetcode import ( "fmt" "testing" ) type question1655 struct { para1655 ans1655 } // para 是参数 // one 代表第一个参数 type para1655 struct { nums []int quantity []int } // ans 是答案 // one 代表第一个答案 type ans1655 struct { one bool } func Test_Problem1655(t *testing.T) { qs := []question1655{ { para1655{[]int{1, 2, 3, 4}, []int{2}}, ans1655{false}, }, { para1655{[]int{1, 2, 3, 3}, []int{2}}, ans1655{true}, }, { para1655{[]int{1, 1, 2, 2}, []int{2, 2}}, ans1655{true}, }, { para1655{[]int{1, 1, 2, 3}, []int{2, 2}}, ans1655{false}, }, { para1655{[]int{1, 1, 1, 1, 1}, []int{2, 3}}, ans1655{true}, }, } fmt.Printf("------------------------Leetcode Problem 1655------------------------\n") for _, q := range qs { _, p := q.ans1655, q.para1655 fmt.Printf("【input】:%v 【output】:%v \n", p, canDistribute(p.nums, p.quantity)) } 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/1655.Distribute-Repeating-Integers/1655. Distribute Repeating Integers.go
leetcode/1655.Distribute-Repeating-Integers/1655. Distribute Repeating Integers.go
package leetcode func canDistribute(nums []int, quantity []int) bool { freq := make(map[int]int) for _, n := range nums { freq[n]++ } return dfs(freq, quantity) } func dfs(freq map[int]int, quantity []int) bool { if len(quantity) == 0 { return true } visited := make(map[int]bool) for i := range freq { if visited[freq[i]] { continue } visited[freq[i]] = true if freq[i] >= quantity[0] { freq[i] -= quantity[0] if dfs(freq, quantity[1:]) { return true } freq[i] += quantity[0] } } return false }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0015.3Sum/15. 3Sum_test.go
leetcode/0015.3Sum/15. 3Sum_test.go
package leetcode import ( "fmt" "testing" ) type question15 struct { para15 ans15 } // para 是参数 // one 代表第一个参数 type para15 struct { a []int } // ans 是答案 // one 代表第一个答案 type ans15 struct { one [][]int } func Test_Problem15(t *testing.T) { qs := []question15{ { para15{[]int{0, 0, 0}}, ans15{[][]int{{0, 0, 0}}}, }, { para15{[]int{-1, 0, 1, 2, -1, -4}}, ans15{[][]int{{-1, 0, 1}, {-1, -1, 2}}}, }, { para15{[]int{-4, -2, -2, -2, 0, 1, 2, 2, 2, 3, 3, 4, 4, 6, 6}}, ans15{[][]int{{-4, -2, 6}, {-4, 0, 4}, {-4, 1, 3}, {-4, 2, 2}, {-2, -2, 4}, {-2, 0, 2}}}, }, { para15{[]int{5, -7, 3, -3, 5, -10, 4, 8, -3, -8, -3, -3, -1, -8, 6, 4, -4, 7, 2, -5, -2, -7, -3, 7, 2, 4, -6, 5}}, ans15{[][]int{{-10, 2, 8}, {-10, 3, 7}, {-10, 4, 6}, {-10, 5, 5}, {-8, 2, 6}, {-8, 3, 5}, {-8, 4, 4}, {-7, -1, 8}, {-7, 2, 5}, {-7, 3, 4}, {-6, -2, 8}, {-6, -1, 7}, {-6, 2, 4}, {-5, -3, 8}, {-5, -2, 7}, {-5, -1, 6}, {-5, 2, 3}, {-4, -3, 7}, {-4, -2, 6}, {-4, -1, 5}, {-4, 2, 2}, {-3, -3, 6}, {-3, -2, 5}, {-3, -1, 4}, {-2, -1, 3}}}, }, } fmt.Printf("------------------------Leetcode Problem 15------------------------\n") for _, q := range qs { _, p := q.ans15, q.para15 fmt.Printf("【input】:%v 【output】:%v\n", p, threeSum(p.a)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0015.3Sum/15. 3Sum.go
leetcode/0015.3Sum/15. 3Sum.go
package leetcode import ( "sort" ) // 解法一 最优解,双指针 + 排序 func threeSum(nums []int) [][]int { sort.Ints(nums) result, start, end, index, addNum, length := make([][]int, 0), 0, 0, 0, 0, len(nums) for index = 1; index < length-1; index++ { start, end = 0, length-1 if index > 1 && nums[index] == nums[index-1] { start = index - 1 } for start < index && end > index { if start > 0 && nums[start] == nums[start-1] { start++ continue } if end < length-1 && nums[end] == nums[end+1] { end-- continue } addNum = nums[start] + nums[end] + nums[index] if addNum == 0 { result = append(result, []int{nums[start], nums[index], nums[end]}) start++ end-- } else if addNum > 0 { end-- } else { start++ } } } return result } // 解法二 func threeSum1(nums []int) [][]int { res := [][]int{} counter := map[int]int{} for _, value := range nums { counter[value]++ } uniqNums := []int{} for key := range counter { uniqNums = append(uniqNums, key) } sort.Ints(uniqNums) for i := 0; i < len(uniqNums); i++ { if (uniqNums[i]*3 == 0) && counter[uniqNums[i]] >= 3 { res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[i]}) } for j := i + 1; j < len(uniqNums); j++ { if (uniqNums[i]*2+uniqNums[j] == 0) && counter[uniqNums[i]] > 1 { res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[j]}) } if (uniqNums[j]*2+uniqNums[i] == 0) && counter[uniqNums[j]] > 1 { res = append(res, []int{uniqNums[i], uniqNums[j], uniqNums[j]}) } c := 0 - uniqNums[i] - uniqNums[j] if c > uniqNums[j] && counter[c] > 0 { res = append(res, []int{uniqNums[i], uniqNums[j], c}) } } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0828.Count-Unique-Characters-of-All-Substrings-of-a-Given-String/828. Count Unique Characters of All Substrings of a Given String.go
leetcode/0828.Count-Unique-Characters-of-All-Substrings-of-a-Given-String/828. Count Unique Characters of All Substrings of a Given String.go
package leetcode func uniqueLetterString(S string) int { res, left, right := 0, 0, 0 for i := 0; i < len(S); i++ { left = i - 1 for left >= 0 && S[left] != S[i] { left-- } right = i + 1 for right < len(S) && S[right] != S[i] { right++ } res += (i - left) * (right - i) } return res % 1000000007 } // 暴力解法,超时!时间复杂度 O(n^2) func uniqueLetterString1(S string) int { if len(S) == 0 { return 0 } res, mod := 0, 1000000007 for i := 0; i < len(S); i++ { letterMap := map[byte]int{} for j := i; j < len(S); j++ { letterMap[S[j]]++ tmp := 0 for _, v := range letterMap { if v > 1 { tmp++ } } if tmp == len(letterMap) { continue } else { res += len(letterMap) - tmp } } } return res % mod }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0828.Count-Unique-Characters-of-All-Substrings-of-a-Given-String/828. Count Unique Characters of All Substrings of a Given String_test.go
leetcode/0828.Count-Unique-Characters-of-All-Substrings-of-a-Given-String/828. Count Unique Characters of All Substrings of a Given String_test.go
package leetcode import ( "fmt" "testing" ) type question828 struct { para828 ans828 } // para 是参数 // one 代表第一个参数 type para828 struct { one string } // ans 是答案 // one 代表第一个答案 type ans828 struct { one int } func Test_Problem828(t *testing.T) { qs := []question828{ { para828{"BABABBABAA"}, ans828{35}, }, { para828{"ABC"}, ans828{10}, }, { para828{"ABA"}, ans828{8}, }, { para828{"ABAB"}, ans828{12}, }, } fmt.Printf("------------------------Leetcode Problem 828------------------------\n") for _, q := range qs { _, p := q.ans828, q.para828 fmt.Printf("【input】:%v 【output】:%v\n", p, uniqueLetterString(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/0084.Largest-Rectangle-in-Histogram/84. Largest Rectangle in Histogram_test.go
leetcode/0084.Largest-Rectangle-in-Histogram/84. Largest Rectangle in Histogram_test.go
package leetcode import ( "fmt" "testing" ) type question84 struct { para84 ans84 } // para 是参数 // one 代表第一个参数 type para84 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans84 struct { one int } func Test_Problem84(t *testing.T) { qs := []question84{ { para84{[]int{2, 1, 5, 6, 2, 3}}, ans84{10}, }, { para84{[]int{1}}, ans84{1}, }, { para84{[]int{1, 1}}, ans84{2}, }, { para84{[]int{2, 1, 2}}, ans84{3}, }, } fmt.Printf("------------------------Leetcode Problem 84------------------------\n") for _, q := range qs { _, p := q.ans84, q.para84 fmt.Printf("【input】:%v 【output】:%v\n", p, largestRectangleArea(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/0084.Largest-Rectangle-in-Histogram/84. Largest Rectangle in Histogram.go
leetcode/0084.Largest-Rectangle-in-Histogram/84. Largest Rectangle in Histogram.go
package leetcode func largestRectangleArea(heights []int) int { maxArea := 0 n := len(heights) + 2 // Add a sentry at the beginning and the end getHeight := func(i int) int { if i == 0 || n-1 == i { return 0 } return heights[i-1] } st := make([]int, 0, n/2) for i := 0; i < n; i++ { for len(st) > 0 && getHeight(st[len(st)-1]) > getHeight(i) { // pop stack idx := st[len(st)-1] st = st[:len(st)-1] maxArea = max(maxArea, getHeight(idx)*(i-st[len(st)-1]-1)) } // push stack st = append(st, i) } return maxArea } 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/0697.Degree-of-an-Array/697. Degree of an Array.go
leetcode/0697.Degree-of-an-Array/697. Degree of an Array.go
package leetcode func findShortestSubArray(nums []int) int { frequency, maxFreq, smallest := map[int][]int{}, 0, len(nums) for i, num := range nums { if _, found := frequency[num]; !found { frequency[num] = []int{1, i, i} } else { frequency[num][0]++ frequency[num][2] = i } if maxFreq < frequency[num][0] { maxFreq = frequency[num][0] } } for _, indices := range frequency { if indices[0] == maxFreq { if smallest > indices[2]-indices[1]+1 { smallest = indices[2] - indices[1] + 1 } } } return smallest }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0697.Degree-of-an-Array/697. Degree of an Array_test.go
leetcode/0697.Degree-of-an-Array/697. Degree of an Array_test.go
package leetcode import ( "fmt" "testing" ) type question697 struct { para697 ans697 } // para 是参数 // one 代表第一个参数 type para697 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans697 struct { one int } func Test_Problem697(t *testing.T) { qs := []question697{ { para697{[]int{1, 2, 2, 3, 1}}, ans697{2}, }, { para697{[]int{1, 2, 2, 3, 1, 4, 2}}, ans697{6}, }, { para697{[]int{}}, ans697{0}, }, { para697{[]int{1, 1, 1, 1, 1}}, ans697{5}, }, { para697{[]int{1, 2, 2, 3, 1, 4, 2, 1, 2, 2, 3, 1, 4, 2}}, ans697{13}, }, } fmt.Printf("------------------------Leetcode Problem 697------------------------\n") for _, q := range qs { _, p := q.ans697, q.para697 fmt.Printf("【input】:%v 【output】:%v\n", p, findShortestSubArray(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/0540.Single-Element-in-a-Sorted-Array/540.Single Element in a Sorted Array.go
leetcode/0540.Single-Element-in-a-Sorted-Array/540.Single Element in a Sorted Array.go
package leetcode func singleNonDuplicate(nums []int) int { left, right := 0, len(nums)-1 for left < right { mid := (left + right) / 2 if mid%2 == 0 { if nums[mid] == nums[mid+1] { left = mid + 1 } else { right = mid } } else { if nums[mid] == nums[mid-1] { left = mid + 1 } else { right = mid } } } return nums[left] }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0540.Single-Element-in-a-Sorted-Array/540.Single Element in a Sorted Array_test.go
leetcode/0540.Single-Element-in-a-Sorted-Array/540.Single Element in a Sorted Array_test.go
package leetcode import ( "fmt" "testing" ) type question540 struct { para540 ans540 } // para 是参数 type para540 struct { nums []int } // ans 是答案 type ans540 struct { ans int } func Test_Problem540(t *testing.T) { qs := []question540{ { para540{[]int{1, 1, 2, 3, 3, 4, 4, 8, 8}}, ans540{2}, }, { para540{[]int{3, 3, 7, 7, 10, 11, 11}}, ans540{10}, }, } fmt.Printf("------------------------Leetcode Problem 540------------------------\n") for _, q := range qs { _, p := q.ans540, q.para540 fmt.Printf("【input】:%v ", p.nums) fmt.Printf("【output】:%v \n", singleNonDuplicate(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/1791.Find-Center-of-Star-Graph/1791.Find Center of Star Graph_test.go
leetcode/1791.Find-Center-of-Star-Graph/1791.Find Center of Star Graph_test.go
package leetcode import ( "fmt" "testing" ) type question1791 struct { para1791 ans1791 } // para 是参数 type para1791 struct { edges [][]int } // ans 是答案 type ans1791 struct { ans int } func Test_Problem1791(t *testing.T) { qs := []question1791{ { para1791{[][]int{{1, 2}, {2, 3}, {4, 2}}}, ans1791{2}, }, { para1791{[][]int{{1, 2}, {5, 1}, {1, 3}, {1, 4}}}, ans1791{1}, }, } fmt.Printf("------------------------Leetcode Problem 1791------------------------\n") for _, q := range qs { _, p := q.ans1791, q.para1791 fmt.Printf("【input】:%v ", p.edges) fmt.Printf("【output】:%v \n", findCenter(p.edges)) } 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/1791.Find-Center-of-Star-Graph/1791.Find Center of Star Graph.go
leetcode/1791.Find-Center-of-Star-Graph/1791.Find Center of Star Graph.go
package leetcode func findCenter(edges [][]int) int { if edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1] { return edges[0][0] } return edges[0][1] }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0021.Merge-Two-Sorted-Lists/21. Merge Two Sorted Lists.go
leetcode/0021.Merge-Two-Sorted-Lists/21. Merge Two Sorted Lists.go
package leetcode import ( "github.com/halfrost/LeetCode-Go/structures" ) // ListNode define type ListNode = structures.ListNode /** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { if l1 == nil { return l2 } if l2 == nil { return l1 } if l1.Val < l2.Val { l1.Next = mergeTwoLists(l1.Next, l2) return l1 } l2.Next = mergeTwoLists(l1, l2.Next) return l2 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0021.Merge-Two-Sorted-Lists/21. Merge Two Sorted Lists_test.go
leetcode/0021.Merge-Two-Sorted-Lists/21. Merge Two Sorted Lists_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question21 struct { para21 ans21 } // para 是参数 // one 代表第一个参数 type para21 struct { one []int another []int } // ans 是答案 // one 代表第一个答案 type ans21 struct { one []int } func Test_Problem21(t *testing.T) { qs := []question21{ { para21{[]int{}, []int{}}, ans21{[]int{}}, }, { para21{[]int{1}, []int{1}}, ans21{[]int{1, 1}}, }, { para21{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}}, ans21{[]int{1, 1, 2, 2, 3, 3, 4, 4}}, }, { para21{[]int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}}, ans21{[]int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}}, }, { para21{[]int{1}, []int{9, 9, 9, 9, 9}}, ans21{[]int{1, 9, 9, 9, 9, 9}}, }, { para21{[]int{9, 9, 9, 9, 9}, []int{1}}, ans21{[]int{1, 9, 9, 9, 9, 9}}, }, { para21{[]int{2, 3, 4}, []int{4, 5, 6}}, ans21{[]int{2, 3, 4, 4, 5, 6}}, }, { para21{[]int{1, 3, 8}, []int{1, 7}}, ans21{[]int{1, 1, 3, 7, 8}}, }, // 如需多个测试,可以复制上方元素。 } fmt.Printf("------------------------Leetcode Problem 21------------------------\n") for _, q := range qs { _, p := q.ans21, q.para21 fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(mergeTwoLists(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/1665.Minimum-Initial-Energy-to-Finish-Tasks/1665. Minimum Initial Energy to Finish Tasks_test.go
leetcode/1665.Minimum-Initial-Energy-to-Finish-Tasks/1665. Minimum Initial Energy to Finish Tasks_test.go
package leetcode import ( "fmt" "testing" ) type question1665 struct { para1665 ans1665 } // para 是参数 // one 代表第一个参数 type para1665 struct { tasks [][]int } // ans 是答案 // one 代表第一个答案 type ans1665 struct { one int } func Test_Problem1665(t *testing.T) { qs := []question1665{ { para1665{[][]int{{1, 2}, {2, 4}, {4, 8}}}, ans1665{8}, }, { para1665{[][]int{{1, 3}, {2, 4}, {10, 11}, {10, 12}, {8, 9}}}, ans1665{32}, }, { para1665{[][]int{{1, 7}, {2, 8}, {3, 9}, {4, 10}, {5, 11}, {6, 12}}}, ans1665{27}, }, } fmt.Printf("------------------------Leetcode Problem 1665------------------------\n") for _, q := range qs { _, p := q.ans1665, q.para1665 fmt.Printf("【input】:%v 【output】:%v \n", p, minimumEffort(p.tasks)) } 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/1665.Minimum-Initial-Energy-to-Finish-Tasks/1665. Minimum Initial Energy to Finish Tasks.go
leetcode/1665.Minimum-Initial-Energy-to-Finish-Tasks/1665. Minimum Initial Energy to Finish Tasks.go
package leetcode import ( "sort" ) func minimumEffort(tasks [][]int) int { sort.Sort(Task(tasks)) res, cur := 0, 0 for _, t := range tasks { if t[1] > cur { res += t[1] - cur cur = t[1] - t[0] } else { cur -= t[0] } } return res } func max(a, b int) int { if a > b { return a } return b } // Task define type Task [][]int func (task Task) Len() int { return len(task) } func (task Task) Less(i, j int) bool { t1, t2 := task[i][1]-task[i][0], task[j][1]-task[j][0] if t1 != t2 { return t2 < t1 } return task[j][1] < task[i][1] } func (task Task) Swap(i, j int) { t := task[i] task[i] = task[j] task[j] = t }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0029.Divide-Two-Integers/29. Divide Two Integers.go
leetcode/0029.Divide-Two-Integers/29. Divide Two Integers.go
package leetcode import ( "math" ) // 解法一 递归版的二分搜索 func divide(dividend int, divisor int) int { sign, res := -1, 0 // low, high := 0, abs(dividend) if dividend == 0 { return 0 } if divisor == 1 { return dividend } if dividend == math.MinInt32 && divisor == -1 { return math.MaxInt32 } if dividend > 0 && divisor > 0 || dividend < 0 && divisor < 0 { sign = 1 } if dividend > math.MaxInt32 { dividend = math.MaxInt32 } // 如果把递归改成非递归,可以改成下面这段代码 // for low <= high { // quotient := low + (high-low)>>1 // if ((quotient+1)*abs(divisor) > abs(dividend) && quotient*abs(divisor) <= abs(dividend)) || ((quotient+1)*abs(divisor) >= abs(dividend) && quotient*abs(divisor) < abs(dividend)) { // if (quotient+1)*abs(divisor) == abs(dividend) { // res = quotient + 1 // break // } // res = quotient // break // } // if (quotient+1)*abs(divisor) > abs(dividend) && quotient*abs(divisor) > abs(dividend) { // high = quotient - 1 // } // if (quotient+1)*abs(divisor) < abs(dividend) && quotient*abs(divisor) < abs(dividend) { // low = quotient + 1 // } // } res = binarySearchQuotient(0, abs(dividend), abs(divisor), abs(dividend)) if res > math.MaxInt32 { return sign * math.MaxInt32 } if res < math.MinInt32 { return sign * math.MinInt32 } return sign * res } func binarySearchQuotient(low, high, val, dividend int) int { quotient := low + (high-low)>>1 if ((quotient+1)*val > dividend && quotient*val <= dividend) || ((quotient+1)*val >= dividend && quotient*val < dividend) { if (quotient+1)*val == dividend { return quotient + 1 } return quotient } if (quotient+1)*val > dividend && quotient*val > dividend { return binarySearchQuotient(low, quotient-1, val, dividend) } if (quotient+1)*val < dividend && quotient*val < dividend { return binarySearchQuotient(quotient+1, high, val, dividend) } return 0 } // 解法二 非递归版的二分搜索 func divide1(divided int, divisor int) int { if divided == math.MinInt32 && divisor == -1 { return math.MaxInt32 } result := 0 sign := -1 if divided > 0 && divisor > 0 || divided < 0 && divisor < 0 { sign = 1 } dvd, dvs := abs(divided), abs(divisor) for dvd >= dvs { temp := dvs m := 1 for temp<<1 <= dvd { temp <<= 1 m <<= 1 } dvd -= temp result += m } return sign * result } func abs(a int) int { if a > 0 { return a } return -a }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0029.Divide-Two-Integers/29. Divide Two Integers_test.go
leetcode/0029.Divide-Two-Integers/29. Divide Two Integers_test.go
package leetcode import ( "fmt" "testing" ) type question29 struct { para29 ans29 } // para 是参数 // one 代表第一个参数 type para29 struct { dividend int divisor int } // ans 是答案 // one 代表第一个答案 type ans29 struct { one int } func Test_Problem29(t *testing.T) { qs := []question29{ { para29{10, 3}, ans29{3}, }, { para29{7, -3}, ans29{-2}, }, { para29{-1, 1}, ans29{-1}, }, { para29{1, -1}, ans29{-1}, }, { para29{2147483647, 3}, ans29{715827882}, }, } fmt.Printf("------------------------Leetcode Problem 29------------------------\n") for _, q := range qs { _, p := q.ans29, q.para29 fmt.Printf("【input】:%v 【output】:%v\n", p, divide(p.dividend, p.divisor)) } 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/2166.Design-Bitset/2166. Design Bitset_test.go
leetcode/2166.Design-Bitset/2166. Design Bitset_test.go
package leetcode import ( "fmt" "testing" ) func Test_Problem2166(t *testing.T) { obj := Constructor(5) fmt.Printf("obj = %v\n", obj) obj.Fix(3) fmt.Printf("obj = %v\n", obj) obj.Fix(1) fmt.Printf("obj = %v\n", obj) obj.Flip() fmt.Printf("obj = %v\n", obj) fmt.Printf("all = %v\n", obj.All()) obj.Unfix(0) fmt.Printf("obj = %v\n", obj) obj.Flip() fmt.Printf("obj = %v\n", obj) fmt.Printf("one = %v\n", obj.One()) obj.Unfix(0) fmt.Printf("obj = %v\n", obj) fmt.Printf("count = %v\n", obj.Count()) fmt.Printf("toString = %v\n", obj.ToString()) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/2166.Design-Bitset/2166. Design Bitset.go
leetcode/2166.Design-Bitset/2166. Design Bitset.go
package leetcode type Bitset struct { set []byte flipped []byte oneCount int size int } func Constructor(size int) Bitset { set := make([]byte, size) flipped := make([]byte, size) for i := 0; i < size; i++ { set[i] = byte('0') flipped[i] = byte('1') } return Bitset{ set: set, flipped: flipped, oneCount: 0, size: size, } } func (this *Bitset) Fix(idx int) { if this.set[idx] == byte('0') { this.set[idx] = byte('1') this.flipped[idx] = byte('0') this.oneCount++ } } func (this *Bitset) Unfix(idx int) { if this.set[idx] == byte('1') { this.set[idx] = byte('0') this.flipped[idx] = byte('1') this.oneCount-- } } func (this *Bitset) Flip() { this.set, this.flipped = this.flipped, this.set this.oneCount = this.size - this.oneCount } func (this *Bitset) All() bool { return this.oneCount == this.size } func (this *Bitset) One() bool { return this.oneCount != 0 } func (this *Bitset) Count() int { return this.oneCount } func (this *Bitset) ToString() string { return string(this.set) } /** * Your Bitset object will be instantiated and called as such: * obj := Constructor(size); * obj.Fix(idx); * obj.Unfix(idx); * obj.Flip(); * param_4 := obj.All(); * param_5 := obj.One(); * param_6 := obj.Count(); * param_7 := obj.ToString(); */
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0949.Largest-Time-for-Given-Digits/949. Largest Time for Given Digits.go
leetcode/0949.Largest-Time-for-Given-Digits/949. Largest Time for Given Digits.go
package leetcode import "fmt" func largestTimeFromDigits(A []int) string { flag, res := false, 0 for i := 0; i < 4; i++ { for j := 0; j < 4; j++ { if i == j { continue } for k := 0; k < 4; k++ { if i == k || j == k { continue } l := 6 - i - j - k hour := A[i]*10 + A[j] min := A[k]*10 + A[l] if hour < 24 && min < 60 { if hour*60+min >= res { res = hour*60 + min flag = true } } } } } if flag { return fmt.Sprintf("%02d:%02d", res/60, res%60) } else { return "" } }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0949.Largest-Time-for-Given-Digits/949. Largest Time for Given Digits_test.go
leetcode/0949.Largest-Time-for-Given-Digits/949. Largest Time for Given Digits_test.go
package leetcode import ( "fmt" "testing" ) type question949 struct { para949 ans949 } // para 是参数 // one 代表第一个参数 type para949 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans949 struct { one string } func Test_Problem949(t *testing.T) { qs := []question949{ { para949{[]int{1, 2, 3, 4}}, ans949{"23:41"}, }, { para949{[]int{5, 5, 5, 5}}, ans949{""}, }, } fmt.Printf("------------------------Leetcode Problem 949------------------------\n") for _, q := range qs { _, p := q.ans949, q.para949 fmt.Printf("【input】:%v 【output】:%v\n", p, largestTimeFromDigits(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/0771.Jewels-and-Stones/771. Jewels and Stones_test.go
leetcode/0771.Jewels-and-Stones/771. Jewels and Stones_test.go
package leetcode import ( "fmt" "testing" ) type question771 struct { para771 ans771 } // para 是参数 // one 代表第一个参数 type para771 struct { one string two string } // ans 是答案 // one 代表第一个答案 type ans771 struct { one int } func Test_Problem771(t *testing.T) { qs := []question771{ { para771{"aA", "aAAbbbb"}, ans771{3}, }, { para771{"z", "ZZ"}, ans771{0}, }, } fmt.Printf("------------------------Leetcode Problem 771------------------------\n") for _, q := range qs { _, p := q.ans771, q.para771 fmt.Printf("【input】:%v 【output】:%v\n", p, numJewelsInStones(p.one, p.two)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0771.Jewels-and-Stones/771. Jewels and Stones.go
leetcode/0771.Jewels-and-Stones/771. Jewels and Stones.go
package leetcode import "strings" // 解法一 func numJewelsInStones(J string, S string) int { count := 0 for i := range S { if strings.Contains(J, string(S[i])) { count++ } } return count } // 解法二 func numJewelsInStones1(J string, S string) int { cache, result := make(map[rune]bool), 0 for _, r := range J { cache[r] = true } for _, r := range S { if _, ok := cache[r]; ok { result++ } } return result }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0792.Number-of-Matching-Subsequences/792. Number of Matching Subsequences_test.go
leetcode/0792.Number-of-Matching-Subsequences/792. Number of Matching Subsequences_test.go
package leetcode import ( "fmt" "testing" ) type question792 struct { para792 ans792 } // para 是参数 // one 代表第一个参数 type para792 struct { s string words []string } // ans 是答案 // one 代表第一个答案 type ans792 struct { one int } func Test_Problem792(t *testing.T) { qs := []question792{ { para792{"abcde", []string{"a", "bb", "acd", "ace"}}, ans792{3}, }, } fmt.Printf("------------------------Leetcode Problem 792------------------------\n") for _, q := range qs { _, p := q.ans792, q.para792 fmt.Printf("【input】:%v 【output】:%v\n", p, numMatchingSubseq(p.s, p.words)) } 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/0792.Number-of-Matching-Subsequences/792. Number of Matching Subsequences.go
leetcode/0792.Number-of-Matching-Subsequences/792. Number of Matching Subsequences.go
package leetcode func numMatchingSubseq(s string, words []string) int { hash, res := make([][]string, 26), 0 for _, w := range words { hash[int(w[0]-'a')] = append(hash[int(w[0]-'a')], w) } for _, c := range s { words := hash[int(byte(c)-'a')] hash[int(byte(c)-'a')] = []string{} for _, w := range words { if len(w) == 1 { res += 1 continue } hash[int(w[1]-'a')] = append(hash[int(w[1]-'a')], w[1:]) } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0667.Beautiful-Arrangement-II/667. Beautiful Arrangement II.go
leetcode/0667.Beautiful-Arrangement-II/667. Beautiful Arrangement II.go
package leetcode func constructArray(n int, k int) []int { res := []int{} for i := 0; i < n-k-1; i++ { res = append(res, i+1) } for i := n - k; i < n-k+(k+1)/2; i++ { res = append(res, i) res = append(res, 2*n-k-i) } if k%2 == 0 { res = append(res, n-k+(k+1)/2) } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0667.Beautiful-Arrangement-II/667. Beautiful Arrangement II_test.go
leetcode/0667.Beautiful-Arrangement-II/667. Beautiful Arrangement II_test.go
package leetcode import ( "fmt" "testing" ) type question667 struct { para667 ans667 } // para 是参数 // one 代表第一个参数 type para667 struct { n int k int } // ans 是答案 // one 代表第一个答案 type ans667 struct { one []int } func Test_Problem667(t *testing.T) { qs := []question667{ { para667{3, 1}, ans667{[]int{1, 2, 3}}, }, { para667{3, 2}, ans667{[]int{1, 3, 2}}, }, } fmt.Printf("------------------------Leetcode Problem 667------------------------\n") for _, q := range qs { _, p := q.ans667, q.para667 fmt.Printf("【input】:%v 【output】:%v\n", p, constructArray(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/0817.Linked-List-Components/817. Linked List Components.go
leetcode/0817.Linked-List-Components/817. Linked List Components.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 numComponents(head *ListNode, G []int) int { if head.Next == nil { return 1 } gMap := toMap(G) count := 0 cur := head for cur != nil { if _, ok := gMap[cur.Val]; ok { if cur.Next == nil { // 末尾存在,直接加一 count++ } else { if _, ok = gMap[cur.Next.Val]; !ok { count++ } } } cur = cur.Next } return count } func toMap(G []int) map[int]int { GMap := make(map[int]int, 0) for _, value := range G { GMap[value] = 0 } return GMap }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0817.Linked-List-Components/817. Linked List Components_test.go
leetcode/0817.Linked-List-Components/817. Linked List Components_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question817 struct { para817 ans817 } // para 是参数 // one 代表第一个参数 type para817 struct { one []int another []int } // ans 是答案 // one 代表第一个答案 type ans817 struct { one int } func Test_Problem817(t *testing.T) { qs := []question817{ { para817{[]int{0, 1, 2, 3}, []int{0, 1, 3}}, ans817{2}, }, { para817{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}}, ans817{1}, }, { para817{[]int{0, 1, 2, 3, 4}, []int{0, 3, 1, 4}}, ans817{2}, }, { para817{[]int{0, 1, 2}, []int{0, 2}}, ans817{2}, }, { para817{[]int{1, 2, 0, 4, 3}, []int{3, 4, 0, 2, 1}}, ans817{1}, }, { para817{[]int{0, 2, 4, 3, 1}, []int{3, 2, 4}}, ans817{1}, }, { para817{[]int{7, 5, 13, 3, 16, 11, 12, 0, 17, 1, 4, 15, 6, 14, 2, 19, 9, 10, 8, 18}, []int{8, 10, 3, 11, 17, 16, 7, 9, 5, 15, 13, 6}}, ans817{4}, }, } fmt.Printf("------------------------Leetcode Problem 817------------------------\n") for _, q := range qs { _, p := q.ans817, q.para817 fmt.Printf("【input】:%v 【output】:%v\n", p, numComponents(structures.Ints2List(p.one), 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/0319.Bulb-Switcher/319.Bulb Switcher.go
leetcode/0319.Bulb-Switcher/319.Bulb Switcher.go
package leetcode import "math" func bulbSwitch(n int) int { return int(math.Sqrt(float64(n))) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0319.Bulb-Switcher/319.Bulb Switcher_test.go
leetcode/0319.Bulb-Switcher/319.Bulb Switcher_test.go
package leetcode import ( "fmt" "testing" ) type question319 struct { para319 ans319 } // para 是参数 type para319 struct { n int } // ans 是答案 type ans319 struct { ans int } func Test_Problem319(t *testing.T) { qs := []question319{ { para319{3}, ans319{1}, }, { para319{0}, ans319{0}, }, { para319{1}, ans319{1}, }, } fmt.Printf("------------------------Leetcode Problem 319------------------------\n") for _, q := range qs { _, p := q.ans319, q.para319 fmt.Printf("【input】:%v 【output】:%v\n", p, bulbSwitch(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/0810.Chalkboard-XOR-Game/810. Chalkboard XOR Game_test.go
leetcode/0810.Chalkboard-XOR-Game/810. Chalkboard XOR Game_test.go
package leetcode import ( "fmt" "testing" ) type question810 struct { para810 ans810 } // para 是参数 // one 代表第一个参数 type para810 struct { nums []int } // ans 是答案 // one 代表第一个答案 type ans810 struct { one bool } func Test_Problem810(t *testing.T) { qs := []question810{ { para810{[]int{1, 1, 2}}, ans810{false}, }, } fmt.Printf("------------------------Leetcode Problem 810------------------------\n") for _, q := range qs { _, p := q.ans810, q.para810 fmt.Printf("【input】:%v 【output】:%v\n", p, xorGame(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/0810.Chalkboard-XOR-Game/810. Chalkboard XOR Game.go
leetcode/0810.Chalkboard-XOR-Game/810. Chalkboard XOR Game.go
package leetcode func xorGame(nums []int) bool { if len(nums)%2 == 0 { return true } xor := 0 for _, num := range nums { xor ^= num } return xor == 0 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1299.Replace-Elements-with-Greatest-Element-on-Right-Side/1299. Replace Elements with Greatest Element on Right Side_test.go
leetcode/1299.Replace-Elements-with-Greatest-Element-on-Right-Side/1299. Replace Elements with Greatest Element on Right Side_test.go
package leetcode import ( "fmt" "testing" ) type question1299 struct { para1299 ans1299 } // para 是参数 // one 代表第一个参数 type para1299 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans1299 struct { one []int } func Test_Problem1299(t *testing.T) { qs := []question1299{ { para1299{[]int{17, 18, 5, 4, 6, 1}}, ans1299{[]int{18, 6, 6, 6, 1, -1}}, }, } fmt.Printf("------------------------Leetcode Problem 1299------------------------\n") for _, q := range qs { _, p := q.ans1299, q.para1299 fmt.Printf("【input】:%v 【output】:%v\n", p, replaceElements(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/1299.Replace-Elements-with-Greatest-Element-on-Right-Side/1299. Replace Elements with Greatest Element on Right Side.go
leetcode/1299.Replace-Elements-with-Greatest-Element-on-Right-Side/1299. Replace Elements with Greatest Element on Right Side.go
package leetcode func replaceElements(arr []int) []int { j, temp := -1, 0 for i := len(arr) - 1; i >= 0; i-- { temp = arr[i] arr[i] = j j = max(j, temp) } return arr } 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/2043.Simple-Bank-System/2043.Simple Bank System_test.go
leetcode/2043.Simple-Bank-System/2043.Simple Bank System_test.go
package leetcode import ( "fmt" "testing" ) type question2043 struct { para2043 ans2043 } // para 是参数 type para2043 struct { ops []string para [][]int64 } // ans 是答案 type ans2043 struct { ans []bool } func Test_Problem2043(t *testing.T) { qs := []question2043{ { para2043{ []string{"Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"}, [][]int64{{10, 100, 20, 50, 30}, {3, 10}, {5, 1, 20}, {5, 20}, {3, 4, 15}, {10, 50}}}, ans2043{[]bool{true, true, true, false, false}}, }, } fmt.Printf("------------------------Leetcode Problem 2043------------------------\n") for _, q := range qs { var b Bank var res []bool _, p := q.ans2043, q.para2043 for i, op := range p.ops { if op == "Bank" { b = Constructor(q.para[i]) } else if op == "withdraw" { isSuccess := b.Withdraw(int(p.para[i][0]), p.para[i][1]) res = append(res, isSuccess) } else if op == "transfer" { isSuccess := b.Transfer(int(p.para[i][0]), int(p.para[i][0]), p.para[i][2]) res = append(res, isSuccess) } else if op == "deposit" { isSuccess := b.Deposit(int(p.para[i][0]), p.para[i][1]) res = append(res, isSuccess) } else { fmt.Println("unknown operation") } } fmt.Printf("【input】:%v \n", p) fmt.Printf("【output】:%v \n", res) } 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/2043.Simple-Bank-System/2043.Simple Bank System.go
leetcode/2043.Simple-Bank-System/2043.Simple Bank System.go
package leetcode type Bank struct { accounts []int64 n int } func Constructor(balance []int64) Bank { return Bank{ accounts: balance, n: len(balance), } } func (this *Bank) Transfer(account1 int, account2 int, money int64) bool { if account1 > this.n || account2 > this.n { return false } if this.accounts[account1-1] < money { return false } this.accounts[account1-1] -= money this.accounts[account2-1] += money return true } func (this *Bank) Deposit(account int, money int64) bool { if account > this.n { return false } this.accounts[account-1] += money return true } func (this *Bank) Withdraw(account int, money int64) bool { if account > this.n { return false } if this.accounts[account-1] < money { return false } this.accounts[account-1] -= money return true }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1249.Minimum-Remove-to-Make-Valid-Parentheses/1249. Minimum Remove to Make Valid Parentheses.go
leetcode/1249.Minimum-Remove-to-Make-Valid-Parentheses/1249. Minimum Remove to Make Valid Parentheses.go
package leetcode func minRemoveToMakeValid(s string) string { res, opens := []byte{}, 0 for i := 0; i < len(s); i++ { if s[i] == '(' { opens++ } else if s[i] == ')' { if opens == 0 { continue } opens-- } res = append(res, s[i]) } for i := len(res) - 1; i >= 0; i-- { if res[i] == '(' && opens > 0 { opens-- res = append(res[:i], res[i+1:]...) } } return string(res) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1249.Minimum-Remove-to-Make-Valid-Parentheses/1249. Minimum Remove to Make Valid Parentheses_test.go
leetcode/1249.Minimum-Remove-to-Make-Valid-Parentheses/1249. Minimum Remove to Make Valid Parentheses_test.go
package leetcode import ( "fmt" "testing" ) type question1249 struct { para1249 ans1249 } // para 是参数 // one 代表第一个参数 type para1249 struct { s string } // ans 是答案 // one 代表第一个答案 type ans1249 struct { one string } func Test_Problem1249(t *testing.T) { qs := []question1249{ { para1249{"lee(t(c)o)de)"}, ans1249{"lee(t(c)o)de"}, }, { para1249{"a)b(c)d"}, ans1249{"ab(c)d"}, }, { para1249{"))(("}, ans1249{""}, }, { para1249{"(a(b(c)d)"}, ans1249{"a(b(c)d)"}, }, } fmt.Printf("------------------------Leetcode Problem 1249------------------------\n") for _, q := range qs { _, p := q.ans1249, q.para1249 fmt.Printf("【input】:%v 【output】:%v\n", p, minRemoveToMakeValid(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/1175.Prime-Arrangements/1175. Prime Arrangements.go
leetcode/1175.Prime-Arrangements/1175. Prime Arrangements.go
package leetcode import "sort" var primes = []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97} func numPrimeArrangements(n int) int { primeCount := sort.Search(25, func(i int) bool { return primes[i] > n }) return factorial(primeCount) * factorial(n-primeCount) % 1000000007 } func factorial(n int) int { if n == 1 || n == 0 { return 1 } return n * factorial(n-1) % 1000000007 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1175.Prime-Arrangements/1175. Prime Arrangements_test.go
leetcode/1175.Prime-Arrangements/1175. Prime Arrangements_test.go
package leetcode import ( "fmt" "testing" ) type question1175 struct { para1175 ans1175 } // para 是参数 // one 代表第一个参数 type para1175 struct { one int } // ans 是答案 // one 代表第一个答案 type ans1175 struct { one int } func Test_Problem1175(t *testing.T) { qs := []question1175{ { para1175{5}, ans1175{12}, }, { para1175{99}, ans1175{75763854}, }, { para1175{100}, ans1175{682289015}, }, } fmt.Printf("------------------------Leetcode Problem 1175------------------------\n") for _, q := range qs { _, p := q.ans1175, q.para1175 fmt.Printf("【input】:%v 【output】:%v\n", p, numPrimeArrangements(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/0011.Container-With-Most-Water/11. Container With Most Water.go
leetcode/0011.Container-With-Most-Water/11. Container With Most Water.go
package leetcode func maxArea(height []int) int { max, start, end := 0, 0, len(height)-1 for start < end { width := end - start high := 0 if height[start] < height[end] { high = height[start] start++ } else { high = height[end] end-- } temp := width * high if temp > max { max = temp } } return max }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0011.Container-With-Most-Water/11. Container With Most Water_test.go
leetcode/0011.Container-With-Most-Water/11. Container With Most Water_test.go
package leetcode import ( "fmt" "testing" ) type question11 struct { para11 ans11 } // para 是参数 // one 代表第一个参数 type para11 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans11 struct { one int } func Test_Problem11(t *testing.T) { qs := []question11{ { para11{[]int{1, 8, 6, 2, 5, 4, 8, 3, 7}}, ans11{49}, }, { para11{[]int{1, 1}}, ans11{1}, }, } fmt.Printf("------------------------Leetcode Problem 11------------------------\n") for _, q := range qs { _, p := q.ans11, q.para11 fmt.Printf("【input】:%v 【output】:%v\n", p.one, maxArea(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/1673.Find-the-Most-Competitive-Subsequence/1673. Find the Most Competitive Subsequence.go
leetcode/1673.Find-the-Most-Competitive-Subsequence/1673. Find the Most Competitive Subsequence.go
package leetcode // 单调栈 func mostCompetitive(nums []int, k int) []int { stack := make([]int, 0, len(nums)) for i := 0; i < len(nums); i++ { for len(stack)+len(nums)-i > k && len(stack) > 0 && nums[i] < stack[len(stack)-1] { stack = stack[:len(stack)-1] } stack = append(stack, nums[i]) } return stack[:k] }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1673.Find-the-Most-Competitive-Subsequence/1673. Find the Most Competitive Subsequence_test.go
leetcode/1673.Find-the-Most-Competitive-Subsequence/1673. Find the Most Competitive Subsequence_test.go
package leetcode import ( "fmt" "testing" ) type question1673 struct { para1673 ans1673 } // para 是参数 // one 代表第一个参数 type para1673 struct { nums []int k int } // ans 是答案 // one 代表第一个答案 type ans1673 struct { one []int } func Test_Problem1673(t *testing.T) { qs := []question1673{ { para1673{[]int{3, 5, 2, 6}, 2}, ans1673{[]int{2, 6}}, }, { para1673{[]int{2, 4, 3, 3, 5, 4, 9, 6}, 4}, ans1673{[]int{2, 3, 3, 4}}, }, { para1673{[]int{2, 4, 3, 3, 5, 4, 9, 6}, 4}, ans1673{[]int{2, 3, 3, 4}}, }, { para1673{[]int{71, 18, 52, 29, 55, 73, 24, 42, 66, 8, 80, 2}, 3}, ans1673{[]int{8, 80, 2}}, }, { para1673{[]int{84, 10, 71, 23, 66, 61, 62, 64, 34, 41, 80, 25, 91, 43, 4, 75, 65, 13, 37, 41, 46, 90, 55, 8, 85, 61, 95, 71}, 24}, ans1673{[]int{10, 23, 61, 62, 34, 41, 80, 25, 91, 43, 4, 75, 65, 13, 37, 41, 46, 90, 55, 8, 85, 61, 95, 71}}, }, } fmt.Printf("------------------------Leetcode Problem 1673------------------------\n") for _, q := range qs { _, p := q.ans1673, q.para1673 fmt.Printf("【input】:%v 【output】:%v\n", p, mostCompetitive(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/0059.Spiral-Matrix-II/59. Spiral Matrix II_test.go
leetcode/0059.Spiral-Matrix-II/59. Spiral Matrix II_test.go
package leetcode import ( "fmt" "testing" ) type question59 struct { para59 ans59 } // para 是参数 // one 代表第一个参数 type para59 struct { one int } // ans 是答案 // one 代表第一个答案 type ans59 struct { one [][]int } func Test_Problem59(t *testing.T) { qs := []question59{ { para59{3}, ans59{[][]int{{1, 2, 3}, {8, 9, 4}, {7, 6, 5}}}, }, { para59{4}, ans59{[][]int{{1, 2, 3, 4}, {12, 13, 14, 5}, {11, 16, 15, 6}, {10, 9, 8, 7}}}, }, } fmt.Printf("------------------------Leetcode Problem 59------------------------\n") for _, q := range qs { _, p := q.ans59, q.para59 fmt.Printf("【input】:%v 【output】:%v\n", p, generateMatrix(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/0059.Spiral-Matrix-II/59. Spiral Matrix II.go
leetcode/0059.Spiral-Matrix-II/59. Spiral Matrix II.go
package leetcode func generateMatrix(n int) [][]int { if n == 0 { return [][]int{} } if n == 1 { return [][]int{{1}} } res, visit, round, x, y, spDir := make([][]int, n), make([][]int, n), 0, 0, 0, [][]int{ {0, 1}, // 朝右 {1, 0}, // 朝下 {0, -1}, // 朝左 {-1, 0}, // 朝上 } for i := 0; i < n; i++ { visit[i] = make([]int, n) res[i] = make([]int, n) } visit[x][y] = 1 res[x][y] = 1 for i := 0; i < n*n; i++ { x += spDir[round%4][0] y += spDir[round%4][1] if (x == 0 && y == n-1) || (x == n-1 && y == n-1) || (y == 0 && x == n-1) { round++ } if x > n-1 || y > n-1 || x < 0 || y < 0 { return res } if visit[x][y] == 0 { visit[x][y] = 1 res[x][y] = i + 2 } switch round % 4 { case 0: if y+1 <= n-1 && visit[x][y+1] == 1 { round++ continue } case 1: if x+1 <= n-1 && visit[x+1][y] == 1 { round++ continue } case 2: if y-1 >= 0 && visit[x][y-1] == 1 { round++ continue } case 3: if x-1 >= 0 && visit[x-1][y] == 1 { round++ continue } } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0572.Subtree-of-Another-Tree/572. Subtree of Another Tree.go
leetcode/0572.Subtree-of-Another-Tree/572. Subtree of Another 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 isSubtree(s *TreeNode, t *TreeNode) bool { if isSameTree(s, t) { return true } if s == nil { return false } if isSubtree(s.Left, t) || isSubtree(s.Right, t) { return true } return false } // this is 100 solution 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/0572.Subtree-of-Another-Tree/572. Subtree of Another Tree_test.go
leetcode/0572.Subtree-of-Another-Tree/572. Subtree of Another Tree_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question572 struct { para572 ans572 } // para 是参数 // one 代表第一个参数 type para572 struct { s []int t []int } // ans 是答案 // one 代表第一个答案 type ans572 struct { one bool } func Test_Problem572(t *testing.T) { qs := []question572{ { para572{[]int{}, []int{}}, ans572{true}, }, { para572{[]int{3, 4, 5, 1, 2}, []int{4, 1, 2}}, ans572{true}, }, { para572{[]int{1, 1}, []int{1}}, ans572{true}, }, { para572{[]int{1, structures.NULL, 1, structures.NULL, 1, structures.NULL, 1, structures.NULL, 1, structures.NULL, 1, structures.NULL, 1, structures.NULL, 1, structures.NULL, 1, structures.NULL, 1, structures.NULL, 1, 2}, []int{1, structures.NULL, 1, structures.NULL, 1, structures.NULL, 1, structures.NULL, 1, structures.NULL, 1, 2}}, ans572{true}, }, } fmt.Printf("------------------------Leetcode Problem 572------------------------\n") for _, q := range qs { _, p := q.ans572, q.para572 fmt.Printf("【input】:%v ", p) roots := structures.Ints2TreeNode(p.s) roott := structures.Ints2TreeNode(p.t) fmt.Printf("【output】:%v \n", isSubtree(roots, roott)) } 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/1818.Minimum-Absolute-Sum-Difference/1818. Minimum Absolute Sum Difference_test.go
leetcode/1818.Minimum-Absolute-Sum-Difference/1818. Minimum Absolute Sum Difference_test.go
package leetcode import ( "fmt" "testing" ) type question1818 struct { para1818 ans1818 } // para 是参数 // one 代表第一个参数 type para1818 struct { nums1 []int nums2 []int } // ans 是答案 // one 代表第一个答案 type ans1818 struct { one int } func Test_Problem1818(t *testing.T) { qs := []question1818{ { para1818{[]int{1, 7, 5}, []int{2, 3, 5}}, ans1818{3}, }, { para1818{[]int{2, 4, 6, 8, 10}, []int{2, 4, 6, 8, 10}}, ans1818{0}, }, { para1818{[]int{1, 10, 4, 4, 2, 7}, []int{9, 3, 5, 1, 7, 4}}, ans1818{20}, }, } fmt.Printf("------------------------Leetcode Problem 1818------------------------\n") for _, q := range qs { _, p := q.ans1818, q.para1818 fmt.Printf("【input】:%v 【output】:%v\n", p, minAbsoluteSumDiff(p.nums1, p.nums2)) } 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/1818.Minimum-Absolute-Sum-Difference/1818. Minimum Absolute Sum Difference.go
leetcode/1818.Minimum-Absolute-Sum-Difference/1818. Minimum Absolute Sum Difference.go
package leetcode func minAbsoluteSumDiff(nums1 []int, nums2 []int) int { diff := 0 maxDiff := 0 for i, n2 := range nums2 { d := abs(nums1[i] - n2) diff += d if maxDiff < d { t := 100001 for _, n1 := range nums1 { maxDiff = max(maxDiff, d-min(t, abs(n1-n2))) } } } return (diff - maxDiff) % (1e9 + 7) } func max(a, b int) int { if a > b { return a } return b } func abs(a int) int { if a > 0 { return a } return -a } 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/0148.Sort-List/148. Sort List.go
leetcode/0148.Sort-List/148. Sort List.go
package leetcode import ( "github.com/halfrost/LeetCode-Go/structures" ) // ListNode define type ListNode = structures.ListNode /** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ func sortList(head *ListNode) *ListNode { length := 0 cur := head for cur != nil { length++ cur = cur.Next } if length <= 1 { return head } middleNode := middleNode(head) cur = middleNode.Next middleNode.Next = nil middleNode = cur left := sortList(head) right := sortList(middleNode) return mergeTwoLists(left, right) } func middleNode(head *ListNode) *ListNode { if head == nil || head.Next == nil { return head } p1 := head p2 := head for p2.Next != nil && p2.Next.Next != nil { p1 = p1.Next p2 = p2.Next.Next } return p1 } func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { if l1 == nil { return l2 } if l2 == nil { return l1 } if l1.Val < l2.Val { l1.Next = mergeTwoLists(l1.Next, l2) return l1 } l2.Next = mergeTwoLists(l1, l2.Next) return l2 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0148.Sort-List/148. Sort List_test.go
leetcode/0148.Sort-List/148. Sort List_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question148 struct { para148 ans148 } // para 是参数 // one 代表第一个参数 type para148 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans148 struct { one []int } func Test_Problem148(t *testing.T) { qs := []question148{ { para148{[]int{1, 2, 3, 4, 5}}, ans148{[]int{1, 2, 3, 4, 5}}, }, { para148{[]int{1, 1, 2, 5, 5, 4, 10, 0}}, ans148{[]int{0, 1, 1, 2, 4, 5, 5}}, }, { para148{[]int{1}}, ans148{[]int{1}}, }, { para148{[]int{}}, ans148{[]int{}}, }, } fmt.Printf("------------------------Leetcode Problem 148------------------------\n") for _, q := range qs { _, p := q.ans148, q.para148 fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(sortList(structures.Ints2List(p.one)))) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0108.Convert-Sorted-Array-to-Binary-Search-Tree/108. Convert Sorted Array to Binary Search Tree.go
leetcode/0108.Convert-Sorted-Array-to-Binary-Search-Tree/108. Convert Sorted Array to 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 sortedArrayToBST(nums []int) *TreeNode { if len(nums) == 0 { return nil } return &TreeNode{Val: nums[len(nums)/2], Left: sortedArrayToBST(nums[:len(nums)/2]), Right: sortedArrayToBST(nums[len(nums)/2+1:])} }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0108.Convert-Sorted-Array-to-Binary-Search-Tree/108. Convert Sorted Array to Binary Search Tree_test.go
leetcode/0108.Convert-Sorted-Array-to-Binary-Search-Tree/108. Convert Sorted Array to Binary Search Tree_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question108 struct { para108 ans108 } // para 是参数 // one 代表第一个参数 type para108 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans108 struct { one []int } func Test_Problem108(t *testing.T) { qs := []question108{ { para108{[]int{-10, -3, 0, 5, 9}}, ans108{[]int{0, -3, 9, -10, structures.NULL, 5}}, }, } fmt.Printf("------------------------Leetcode Problem 108------------------------\n") for _, q := range qs { _, p := q.ans108, q.para108 arr := []int{} structures.T2s(sortedArrayToBST(p.one), &arr) fmt.Printf("【input】:%v 【output】:%v\n", p, arr) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1389.Create-Target-Array-in-the-Given-Order/1389. Create Target Array in the Given Order.go
leetcode/1389.Create-Target-Array-in-the-Given-Order/1389. Create Target Array in the Given Order.go
package leetcode func createTargetArray(nums []int, index []int) []int { result := make([]int, len(nums)) for i, pos := range index { copy(result[pos+1:i+1], result[pos:i]) result[pos] = nums[i] } return result }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1389.Create-Target-Array-in-the-Given-Order/1389. Create Target Array in the Given Order_test.go
leetcode/1389.Create-Target-Array-in-the-Given-Order/1389. Create Target Array in the Given Order_test.go
package leetcode import ( "fmt" "testing" ) type question1389 struct { para1389 ans1389 } // para 是参数 // one 代表第一个参数 type para1389 struct { nums []int index []int } // ans 是答案 // one 代表第一个答案 type ans1389 struct { one []int } func Test_Problem1389(t *testing.T) { qs := []question1389{ { para1389{[]int{0, 1, 2, 3, 4}, []int{0, 1, 2, 2, 1}}, ans1389{[]int{0, 4, 1, 3, 2}}, }, { para1389{[]int{1, 2, 3, 4, 0}, []int{0, 1, 2, 3, 0}}, ans1389{[]int{0, 1, 2, 3, 4}}, }, { para1389{[]int{1}, []int{0}}, ans1389{[]int{1}}, }, } fmt.Printf("------------------------Leetcode Problem 1389------------------------\n") for _, q := range qs { _, p := q.ans1389, q.para1389 fmt.Printf("【input】:%v ", p) fmt.Printf("【output】:%v \n", createTargetArray(p.nums, p.index)) } 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/0497.Random-Point-in-Non-overlapping-Rectangles/497. Random Point in Non-overlapping Rectangles.go
leetcode/0497.Random-Point-in-Non-overlapping-Rectangles/497. Random Point in Non-overlapping Rectangles.go
package leetcode import "math/rand" // Solution497 define type Solution497 struct { rects [][]int arr []int } // Constructor497 define func Constructor497(rects [][]int) Solution497 { s := Solution497{ rects: rects, arr: make([]int, len(rects)), } for i := 0; i < len(rects); i++ { area := (rects[i][2] - rects[i][0] + 1) * (rects[i][3] - rects[i][1] + 1) if area < 0 { area = -area } if i == 0 { s.arr[0] = area } else { s.arr[i] = s.arr[i-1] + area } } return s } // Pick define func (so *Solution497) Pick() []int { r := rand.Int() % so.arr[len(so.arr)-1] //get rectangle first low, high, index := 0, len(so.arr)-1, -1 for low <= high { mid := low + (high-low)>>1 if so.arr[mid] > r { if mid == 0 || so.arr[mid-1] <= r { index = mid break } high = mid - 1 } else { low = mid + 1 } } if index == -1 { index = low } if index > 0 { r = r - so.arr[index-1] } length := so.rects[index][2] - so.rects[index][0] return []int{so.rects[index][0] + r%(length+1), so.rects[index][1] + r/(length+1)} } /** * Your Solution object will be instantiated and called as such: * obj := Constructor(rects); * param_1 := obj.Pick(); */
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0497.Random-Point-in-Non-overlapping-Rectangles/497. Random Point in Non-overlapping Rectangles_test.go
leetcode/0497.Random-Point-in-Non-overlapping-Rectangles/497. Random Point in Non-overlapping Rectangles_test.go
package leetcode import ( "fmt" "testing" ) func Test_Problem497(t *testing.T) { w := [][]int{{1, 1, 5, 5}} sol := Constructor497(w) fmt.Printf("1.Pick = %v\n", sol.Pick()) fmt.Printf("2.Pick = %v\n", sol.Pick()) fmt.Printf("3.Pick = %v\n", sol.Pick()) fmt.Printf("4.Pick = %v\n", sol.Pick()) fmt.Printf("5.Pick = %v\n", sol.Pick()) fmt.Printf("6.Pick = %v\n", sol.Pick()) }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0130.Surrounded-Regions/130. Surrounded Regions.go
leetcode/0130.Surrounded-Regions/130. Surrounded Regions.go
package leetcode import ( "github.com/halfrost/LeetCode-Go/template" ) var dir = [][]int{ {-1, 0}, {0, 1}, {1, 0}, {0, -1}, } // 解法一 并查集 func solve(board [][]byte) { if len(board) == 0 { return } m, n := len(board[0]), len(board) uf := template.UnionFind{} uf.Init(n*m + 1) // 特意多一个特殊点用来标记 for i := 0; i < n; i++ { for j := 0; j < m; j++ { if (i == 0 || i == n-1 || j == 0 || j == m-1) && board[i][j] == 'O' { //棋盘边缘上的 'O' 点 uf.Union(i*m+j, n*m) } else if board[i][j] == 'O' { //棋盘非边缘上的内部的 'O' 点 if board[i-1][j] == 'O' { uf.Union(i*m+j, (i-1)*m+j) } if board[i+1][j] == 'O' { uf.Union(i*m+j, (i+1)*m+j) } if board[i][j-1] == 'O' { uf.Union(i*m+j, i*m+j-1) } if board[i][j+1] == 'O' { uf.Union(i*m+j, i*m+j+1) } } } } for i := 0; i < n; i++ { for j := 0; j < m; j++ { if uf.Find(i*m+j) != uf.Find(n*m) { board[i][j] = 'X' } } } } // 解法二 DFS func solve1(board [][]byte) { for i := range board { for j := range board[i] { if i == 0 || i == len(board)-1 || j == 0 || j == len(board[i])-1 { if board[i][j] == 'O' { dfs130(i, j, board) } } } } for i := range board { for j := range board[i] { if board[i][j] == '*' { board[i][j] = 'O' } else if board[i][j] == 'O' { board[i][j] = 'X' } } } } func dfs130(i, j int, board [][]byte) { if i < 0 || i > len(board)-1 || j < 0 || j > len(board[i])-1 { return } if board[i][j] == 'O' { board[i][j] = '*' for k := 0; k < 4; k++ { dfs130(i+dir[k][0], j+dir[k][1], board) } } }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0130.Surrounded-Regions/130. Surrounded Regions_test.go
leetcode/0130.Surrounded-Regions/130. Surrounded Regions_test.go
package leetcode import ( "fmt" "testing" ) type question130 struct { para130 ans130 } // para 是参数 // one 代表第一个参数 type para130 struct { one [][]byte } // ans 是答案 // one 代表第一个答案 type ans130 struct { one [][]byte } func Test_Problem130(t *testing.T) { qs := []question130{ { para130{[][]byte{}}, ans130{[][]byte{}}, }, { para130{[][]byte{{'X', 'X', 'X', 'X'}, {'X', 'O', 'O', 'X'}, {'X', 'X', 'O', 'X'}, {'X', 'O', 'X', 'X'}}}, ans130{[][]byte{{'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X'}, {'X', 'O', 'X', 'X'}}}, }, } fmt.Printf("------------------------Leetcode Problem 130------------------------\n") for _, q := range qs { _, p := q.ans130, q.para130 fmt.Printf("【input】:%v ", p) solve1(p.one) fmt.Printf("【output】:%v \n", p) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1019.Next-Greater-Node-In-Linked-List/1019. Next Greater Node In Linked List_test.go
leetcode/1019.Next-Greater-Node-In-Linked-List/1019. Next Greater Node In Linked List_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question1019 struct { para1019 ans1019 } // para 是参数 // one 代表第一个参数 type para1019 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans1019 struct { one []int } func Test_Problem1019(t *testing.T) { qs := []question1019{ { para1019{[]int{2, 1, 5}}, ans1019{[]int{5, 5, 0}}, }, { para1019{[]int{2, 7, 4, 3, 5}}, ans1019{[]int{7, 0, 5, 5, 0}}, }, { para1019{[]int{1, 7, 5, 1, 9, 2, 5, 1}}, ans1019{[]int{7, 9, 9, 9, 0, 5, 0, 0}}, }, { para1019{[]int{1, 7, 5, 1, 9, 2, 5, 6, 7, 8, 1}}, ans1019{[]int{7, 9, 9, 9, 0, 5, 6, 7, 8, 0, 0}}, }, } fmt.Printf("------------------------Leetcode Problem 1019------------------------\n") for _, q := range qs { _, p := q.ans1019, q.para1019 fmt.Printf("【input】:%v 【output】:%v\n", p, nextLargerNodes(structures.Ints2List(p.one))) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1019.Next-Greater-Node-In-Linked-List/1019. Next Greater Node In Linked List.go
leetcode/1019.Next-Greater-Node-In-Linked-List/1019. Next Greater Node In Linked List.go
package leetcode import ( "github.com/halfrost/LeetCode-Go/structures" ) // ListNode define type ListNode = structures.ListNode /** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ // 解法一 单调栈 func nextLargerNodes(head *ListNode) []int { type node struct { index, val int } var monoStack []node var res []int for head != nil { for len(monoStack) > 0 && monoStack[len(monoStack)-1].val < head.Val { res[monoStack[len(monoStack)-1].index] = head.Val monoStack = monoStack[:len(monoStack)-1] } monoStack = append(monoStack, node{len(res), head.Val}) res = append(res, 0) head = head.Next } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle.go
leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle.go
package leetcode func maximalRectangle(matrix [][]byte) int { return 0 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle_test.go
leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle_test.go
package leetcode import ( "fmt" "testing" ) type question85 struct { para85 ans85 } // para 是参数 // one 代表第一个参数 type para85 struct { one [][]byte } // ans 是答案 // one 代表第一个答案 type ans85 struct { one int } func Test_Problem85(t *testing.T) { qs := []question85{ { para85{[][]byte{{'1', '0', '1', '0', '0'}, {'1', '0', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '0', '0', '1', '0'}}}, ans85{6}, }, } fmt.Printf("------------------------Leetcode Problem 85------------------------\n") for _, q := range qs { _, p := q.ans85, q.para85 fmt.Printf("【input】:%v 【output】:%v\n", p, maximalRectangle(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/0020.Valid-Parentheses/20. Valid Parentheses.go
leetcode/0020.Valid-Parentheses/20. Valid Parentheses.go
package leetcode func isValid(s string) bool { // 空字符串直接返回 true if len(s) == 0 { return true } stack := make([]rune, 0) for _, v := range s { if (v == '[') || (v == '(') || (v == '{') { stack = append(stack, v) } else if ((v == ']') && len(stack) > 0 && stack[len(stack)-1] == '[') || ((v == ')') && len(stack) > 0 && stack[len(stack)-1] == '(') || ((v == '}') && len(stack) > 0 && stack[len(stack)-1] == '{') { stack = stack[:len(stack)-1] } else { return false } } return len(stack) == 0 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0020.Valid-Parentheses/20. Valid Parentheses_test.go
leetcode/0020.Valid-Parentheses/20. Valid Parentheses_test.go
package leetcode import ( "fmt" "testing" ) type question20 struct { para20 ans20 } // para 是参数 // one 代表第一个参数 type para20 struct { one string } // ans 是答案 // one 代表第一个答案 type ans20 struct { one bool } func Test_Problem20(t *testing.T) { qs := []question20{ { para20{"()[]{}"}, ans20{true}, }, { para20{"(]"}, ans20{false}, }, { para20{"({[]})"}, ans20{true}, }, { para20{"(){[({[]})]}"}, ans20{true}, }, { para20{"((([[[{{{"}, ans20{false}, }, { para20{"(())]]"}, ans20{false}, }, { para20{""}, ans20{true}, }, { para20{"["}, ans20{false}, }, } fmt.Printf("------------------------Leetcode Problem 20------------------------\n") for _, q := range qs { _, p := q.ans20, q.para20 fmt.Printf("【input】:%v 【output】:%v\n", p, isValid(p.one)) } }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0946.Validate-Stack-Sequences/946. Validate Stack Sequences.go
leetcode/0946.Validate-Stack-Sequences/946. Validate Stack Sequences.go
package leetcode func validateStackSequences(pushed []int, popped []int) bool { stack, j, N := []int{}, 0, len(pushed) for _, x := range pushed { stack = append(stack, x) for len(stack) != 0 && j < N && stack[len(stack)-1] == popped[j] { stack = stack[0 : len(stack)-1] j++ } } return j == N }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0946.Validate-Stack-Sequences/946. Validate Stack Sequences_test.go
leetcode/0946.Validate-Stack-Sequences/946. Validate Stack Sequences_test.go
package leetcode import ( "fmt" "testing" ) type question946 struct { para946 ans946 } // para 是参数 // one 代表第一个参数 type para946 struct { one []int two []int } // ans 是答案 // one 代表第一个答案 type ans946 struct { one bool } func Test_Problem946(t *testing.T) { qs := []question946{ { para946{[]int{1, 2, 3, 4, 5}, []int{5, 4, 3, 2, 1}}, ans946{true}, }, { para946{[]int{1, 2, 3, 4, 5}, []int{4, 3, 5, 1, 2}}, ans946{false}, }, { para946{[]int{1, 0}, []int{1, 0}}, ans946{true}, }, } fmt.Printf("------------------------Leetcode Problem 946------------------------\n") for _, q := range qs { _, p := q.ans946, q.para946 fmt.Printf("【input】:%v 【output】:%v\n", p, validateStackSequences(p.one, p.two)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0752.Open-the-Lock/752. Open the Lock.go
leetcode/0752.Open-the-Lock/752. Open the Lock.go
package leetcode func openLock(deadends []string, target string) int { if target == "0000" { return 0 } targetNum, visited := strToInt(target), make([]bool, 10000) visited[0] = true for _, deadend := range deadends { num := strToInt(deadend) if num == 0 { return -1 } visited[num] = true } depth, curDepth, nextDepth := 0, []int16{0}, make([]int16, 0) var nextNum int16 for len(curDepth) > 0 { nextDepth = nextDepth[0:0] for _, curNum := range curDepth { for incrementer := int16(1000); incrementer > 0; incrementer /= 10 { digit := (curNum / incrementer) % 10 if digit == 9 { nextNum = curNum - 9*incrementer } else { nextNum = curNum + incrementer } if nextNum == targetNum { return depth + 1 } if !visited[nextNum] { visited[nextNum] = true nextDepth = append(nextDepth, nextNum) } if digit == 0 { nextNum = curNum + 9*incrementer } else { nextNum = curNum - incrementer } if nextNum == targetNum { return depth + 1 } if !visited[nextNum] { visited[nextNum] = true nextDepth = append(nextDepth, nextNum) } } } curDepth, nextDepth = nextDepth, curDepth depth++ } return -1 } func strToInt(str string) int16 { return int16(str[0]-'0')*1000 + int16(str[1]-'0')*100 + int16(str[2]-'0')*10 + int16(str[3]-'0') }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0752.Open-the-Lock/752. Open the Lock_test.go
leetcode/0752.Open-the-Lock/752. Open the Lock_test.go
package leetcode import ( "fmt" "testing" ) type question752 struct { para752 ans752 } // para 是参数 // one 代表第一个参数 type para752 struct { deadends []string target string } // ans 是答案 // one 代表第一个答案 type ans752 struct { one int } func Test_Problem752(t *testing.T) { qs := []question752{ { para752{[]string{"0201", "0101", "0102", "1212", "2002"}, "0202"}, ans752{6}, }, { para752{[]string{"8888"}, "0009"}, ans752{1}, }, { para752{[]string{"8887", "8889", "8878", "8898", "8788", "8988", "7888", "9888"}, "8888"}, ans752{-1}, }, } fmt.Printf("------------------------Leetcode Problem 752------------------------\n") for _, q := range qs { _, p := q.ans752, q.para752 fmt.Printf("【input】:%v 【output】:%v\n", p, openLock(p.deadends, p.target)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9990363.Max-Sum-of-Rectangle-No-Larger-Than-K/363. Max Sum of Rectangle No Larger Than K_test.go
leetcode/9990363.Max-Sum-of-Rectangle-No-Larger-Than-K/363. Max Sum of Rectangle No Larger Than K_test.go
package leetcode import ( "fmt" "testing" ) type question363 struct { para363 ans363 } // para 是参数 // one 代表第一个参数 type para363 struct { one [][]int k int } // ans 是答案 // one 代表第一个答案 type ans363 struct { one int } func Test_Problem363(t *testing.T) { qs := []question363{ { para363{[][]int{{1, 0, 1}, {0, -2, 3}}, 2}, ans363{2}, }, { para363{[][]int{{2, 2, -1}}, 0}, ans363{-1}, }, { para363{[][]int{{5, -4, -3, 4}, {-3, -4, 4, 5}, {5, 1, 5, -4}}, 8}, ans363{8}, }, { para363{[][]int{{5, -4, -3, 4}, {-3, -4, 4, 5}, {5, 1, 5, -4}}, 3}, ans363{2}, }, } fmt.Printf("------------------------Leetcode Problem 363------------------------\n") for _, q := range qs { _, p := q.ans363, q.para363 fmt.Printf("【input】:%v 【output】:%v\n", p, maxSumSubmatrix(p.one, p.k)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/9990363.Max-Sum-of-Rectangle-No-Larger-Than-K/363. Max Sum of Rectangle No Larger Than K.go
leetcode/9990363.Max-Sum-of-Rectangle-No-Larger-Than-K/363. Max Sum of Rectangle No Larger Than K.go
package leetcode import ( "math" ) // 解法一 二分搜索 func maxSumSubmatrix(matrix [][]int, k int) int { // 转换为前缀和 for i := 0; i < len(matrix); i++ { for j := 1; j < len(matrix[0]); j++ { matrix[i][j] += matrix[i][j-1] } } sum, absMax, absMaxFound := make([]int, len(matrix)), 0, false for y1 := 0; y1 < len(matrix[0]); y1++ { for y2 := y1; y2 < len(matrix[0]); y2++ { for x := 0; x < len(matrix); x++ { if y1 == 0 { sum[x] = matrix[x][y2] } else { sum[x] = matrix[x][y2] - matrix[x][y1-1] } } curMax := kadaneK(sum, k) if !absMaxFound || curMax > absMax { absMax = curMax absMaxFound = true } } } return absMax } func kadaneK(a []int, k int) int { sum, sums, maxSoFar := 0, []int{}, math.MinInt32 for _, v := range a { // 第一次循环会先插入 0,因为 sum 有可能等于 k sums = insertSort(sums, sum) sum += v pos := binarySearchOfKadane(sums, sum-k) if pos < len(sums) && sum-sums[pos] > maxSoFar { maxSoFar = sum - sums[pos] } } return maxSoFar } func binarySearchOfKadane(a []int, v int) int { low, high := 0, len(a) for low < high { mid := low + (high-low)>>1 if a[mid] < v { low = mid + 1 } else { high = mid } } return low } func insertSort(a []int, v int) []int { // 类似插入排序,将元素按照从小到大的顺序插入数组 p := binarySearchOfKadane(a, v) a = append(a, 0) // 把 p 后面的元素全部往后移,把 p 位置空出来放 v copy(a[p+1:], a[p:len(a)-1]) a[p] = v return a } // 解法二 暴力解法,超时 func maxSumSubmatrix1(matrix [][]int, k int) int { minNum := math.MaxInt64 for row := range matrix { for col := 1; col < len(matrix[row]); col++ { if matrix[row][col] < minNum { minNum = matrix[row][col] } } } for row := range matrix { for col := 1; col < len(matrix[row]); col++ { matrix[row][col] += matrix[row][col-1] } } for i := k; ; i-- { if findSumSubmatrix(matrix, i) > 0 { return i } } } func findSumSubmatrix(matrix [][]int, target int) int { m, n, res := len(matrix), len(matrix[0]), 0 for i := 0; i < n; i++ { for j := i; j < n; j++ { counterMap, sum := make(map[int]int, m), 0 counterMap[0] = 1 // 题目保证一定有解,所以这里初始化是 1 for row := 0; row < m; row++ { if i > 0 { sum += matrix[row][j] - matrix[row][i-1] } else { sum += matrix[row][j] } res += counterMap[sum-target] counterMap[sum]++ } } } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0995.Minimum-Number-of-K-Consecutive-Bit-Flips/995. Minimum Number of K Consecutive Bit Flips_test.go
leetcode/0995.Minimum-Number-of-K-Consecutive-Bit-Flips/995. Minimum Number of K Consecutive Bit Flips_test.go
package leetcode import ( "fmt" "testing" ) type question995 struct { para995 ans995 } // para 是参数 // one 代表第一个参数 type para995 struct { one []int k int } // ans 是答案 // one 代表第一个答案 type ans995 struct { one int } func Test_Problem995(t *testing.T) { qs := []question995{ { para995{[]int{0, 1, 0}, 1}, ans995{2}, }, { para995{[]int{1, 1, 0}, 2}, ans995{-1}, }, { para995{[]int{0, 0, 0, 1, 0, 1, 1, 0}, 3}, ans995{3}, }, } fmt.Printf("------------------------Leetcode Problem 995------------------------\n") for _, q := range qs { _, p := q.ans995, q.para995 fmt.Printf("【input】:%v 【output】:%v\n", p, minKBitFlips(p.one, p.k)) } fmt.Printf("\n\n\n") }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0995.Minimum-Number-of-K-Consecutive-Bit-Flips/995. Minimum Number of K Consecutive Bit Flips.go
leetcode/0995.Minimum-Number-of-K-Consecutive-Bit-Flips/995. Minimum Number of K Consecutive Bit Flips.go
package leetcode func minKBitFlips(A []int, K int) int { flippedTime, count := 0, 0 for i := 0; i < len(A); i++ { if i >= K && A[i-K] == 2 { flippedTime-- } // 下面这个判断包含了两种情况: // 如果 flippedTime 是奇数,且 A[i] == 1 就需要翻转 // 如果 flippedTime 是偶数,且 A[i] == 0 就需要翻转 if flippedTime%2 == A[i] { if i+K > len(A) { return -1 } A[i] = 2 flippedTime++ 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/1145.Binary-Tree-Coloring-Game/1145. Binary Tree Coloring Game.go
leetcode/1145.Binary-Tree-Coloring-Game/1145. Binary Tree Coloring Game.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 btreeGameWinningMove(root *TreeNode, n int, x int) bool { var left, right int dfsBtreeGameWinningMove(root, &left, &right, x) up := n - left - right - 1 n /= 2 return left > n || right > n || up > n } func dfsBtreeGameWinningMove(node *TreeNode, left, right *int, x int) int { if node == nil { return 0 } l, r := dfsBtreeGameWinningMove(node.Left, left, right, x), dfsBtreeGameWinningMove(node.Right, left, right, x) if node.Val == x { *left, *right = l, r } return l + r + 1 }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1145.Binary-Tree-Coloring-Game/1145. Binary Tree Coloring Game_test.go
leetcode/1145.Binary-Tree-Coloring-Game/1145. Binary Tree Coloring Game_test.go
package leetcode import ( "fmt" "testing" "github.com/halfrost/LeetCode-Go/structures" ) type question1145 struct { para1145 ans1145 } // para 是参数 // one 代表第一个参数 type para1145 struct { root []int n int x int } // ans 是答案 // one 代表第一个答案 type ans1145 struct { one bool } func Test_Problem1145(t *testing.T) { qs := []question1145{ { para1145{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 11, 3}, ans1145{true}, }, } fmt.Printf("------------------------Leetcode Problem 1145------------------------\n") for _, q := range qs { _, p := q.ans1145, q.para1145 tree := structures.Ints2TreeNode(p.root) fmt.Printf("【input】:%v 【output】:%v\n", p, btreeGameWinningMove(tree, p.n, p.x)) } 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/0819.Most-Common-Word/819. Most Common Word.go
leetcode/0819.Most-Common-Word/819. Most Common Word.go
package leetcode import "strings" func mostCommonWord(paragraph string, banned []string) string { freqMap, start := make(map[string]int), -1 for i, c := range paragraph { if c == ' ' || c == '!' || c == '?' || c == '\'' || c == ',' || c == ';' || c == '.' { if start > -1 { word := strings.ToLower(paragraph[start:i]) freqMap[word]++ } start = -1 } else { if start == -1 { start = i } } } if start != -1 { word := strings.ToLower(paragraph[start:]) freqMap[word]++ } // Strip the banned words from the freqmap for _, bannedWord := range banned { delete(freqMap, bannedWord) } // Find most freq word mostFreqWord, mostFreqCount := "", 0 for word, freq := range freqMap { if freq > mostFreqCount { mostFreqWord = word mostFreqCount = freq } } return mostFreqWord }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false
halfrost/LeetCode-Go
https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0819.Most-Common-Word/819. Most Common Word_test.go
leetcode/0819.Most-Common-Word/819. Most Common Word_test.go
package leetcode import ( "fmt" "testing" ) type question819 struct { para819 ans819 } // para 是参数 // one 代表第一个参数 type para819 struct { one string b []string } // ans 是答案 // one 代表第一个答案 type ans819 struct { one string } func Test_Problem819(t *testing.T) { qs := []question819{ { para819{"Bob hit a ball, the hit BALL flew far after it was hit.", []string{"hit"}}, ans819{"ball"}, }, } fmt.Printf("------------------------Leetcode Problem 819------------------------\n") for _, q := range qs { _, p := q.ans819, q.para819 fmt.Printf("【input】:%v 【output】:%v\n", p, mostCommonWord(p.one, p.b)) } 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/0474.Ones-and-Zeroes/474. Ones and Zeroes.go
leetcode/0474.Ones-and-Zeroes/474. Ones and Zeroes.go
package leetcode import "strings" func findMaxForm(strs []string, m int, n int) int { dp := make([][]int, m+1) for i := 0; i < m+1; i++ { dp[i] = make([]int, n+1) } for _, s := range strs { zero := strings.Count(s, "0") one := len(s) - zero if zero > m || one > n { continue } for i := m; i >= zero; i-- { for j := n; j >= one; j-- { dp[i][j] = max(dp[i][j], 1+dp[i-zero][j-one]) } } } return dp[m][n] } 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/0474.Ones-and-Zeroes/474. Ones and Zeroes_test.go
leetcode/0474.Ones-and-Zeroes/474. Ones and Zeroes_test.go
package leetcode import ( "fmt" "testing" ) type question474 struct { para474 ans474 } // para 是参数 // one 代表第一个参数 type para474 struct { strs []string m int n int } // ans 是答案 // one 代表第一个答案 type ans474 struct { one int } func Test_Problem474(t *testing.T) { qs := []question474{ { para474{[]string{"10", "0001", "111001", "1", "0"}, 5, 3}, ans474{4}, }, { para474{[]string{"10", "0", "1"}, 1, 1}, ans474{2}, }, { para474{[]string{}, 0, 0}, ans474{0}, }, } fmt.Printf("------------------------Leetcode Problem 474------------------------\n") for _, q := range qs { _, p := q.ans474, q.para474 fmt.Printf("【input】:%v 【output】:%v\n", p, findMaxForm(p.strs, p.m, 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/1295.Find-Numbers-with-Even-Number-of-Digits/1295. Find Numbers with Even Number of Digits_test.go
leetcode/1295.Find-Numbers-with-Even-Number-of-Digits/1295. Find Numbers with Even Number of Digits_test.go
package leetcode import ( "fmt" "testing" ) type question1295 struct { para1295 ans1295 } // para 是参数 // one 代表第一个参数 type para1295 struct { one []int } // ans 是答案 // one 代表第一个答案 type ans1295 struct { one int } func Test_Problem1295(t *testing.T) { qs := []question1295{ { para1295{[]int{12, 345, 2, 6, 7896}}, ans1295{2}, }, { para1295{[]int{555, 901, 482, 1771}}, ans1295{1}, }, } fmt.Printf("------------------------Leetcode Problem 1295------------------------\n") for _, q := range qs { _, p := q.ans1295, q.para1295 fmt.Printf("【input】:%v 【output】:%v\n", p, findNumbers(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/1295.Find-Numbers-with-Even-Number-of-Digits/1295. Find Numbers with Even Number of Digits.go
leetcode/1295.Find-Numbers-with-Even-Number-of-Digits/1295. Find Numbers with Even Number of Digits.go
package leetcode import "strconv" func findNumbers(nums []int) int { res := 0 for _, n := range nums { res += 1 - len(strconv.Itoa(n))%2 } return res }
go
MIT
d78a9e0927a302038992428433d2eb450efd93a2
2026-01-07T08:36:06.754118Z
false