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/0451.Sort-Characters-By-Frequency/451. Sort Characters By Frequency_test.go | leetcode/0451.Sort-Characters-By-Frequency/451. Sort Characters By Frequency_test.go | package leetcode
import (
"fmt"
"testing"
)
type question451 struct {
para451
ans451
}
// para 是参数
// one 代表第一个参数
type para451 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans451 struct {
one string
}
func Test_Problem451(t *testing.T) {
qs := []question451{
{
para451{"tree"},
ans451{"eert"},
},
{
para451{"cccaaa"},
ans451{"cccaaa"},
},
{
para451{"Aabb"},
ans451{"bbAa"},
},
}
fmt.Printf("------------------------Leetcode Problem 451------------------------\n")
for _, q := range qs {
_, p := q.ans451, q.para451
fmt.Printf("【input】:%v 【output】:%v\n", p, frequencySort(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/0451.Sort-Characters-By-Frequency/451. Sort Characters By Frequency.go | leetcode/0451.Sort-Characters-By-Frequency/451. Sort Characters By Frequency.go | package leetcode
import (
"sort"
)
func frequencySort(s string) string {
if s == "" {
return ""
}
sMap := map[byte]int{}
cMap := map[int][]byte{}
sb := []byte(s)
for _, b := range sb {
sMap[b]++
}
for key, value := range sMap {
cMap[value] = append(cMap[value], key)
}
var keys []int
for k := range cMap {
keys = append(keys, k)
}
sort.Sort(sort.Reverse(sort.IntSlice(keys)))
res := make([]byte, 0)
for _, k := range keys {
for i := 0; i < len(cMap[k]); i++ {
for j := 0; j < k; j++ {
res = append(res, cMap[k][i])
}
}
}
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/0898.Bitwise-ORs-of-Subarrays/898. Bitwise ORs of Subarrays_test.go | leetcode/0898.Bitwise-ORs-of-Subarrays/898. Bitwise ORs of Subarrays_test.go | package leetcode
import (
"fmt"
"testing"
)
type question898 struct {
para898
ans898
}
// para 是参数
// one 代表第一个参数
type para898 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans898 struct {
one int
}
func Test_Problem898(t *testing.T) {
qs := []question898{
{
para898{[]int{0}},
ans898{1},
},
{
para898{[]int{1, 1, 2}},
ans898{3},
},
{
para898{[]int{1, 2, 4}},
ans898{6},
},
}
fmt.Printf("------------------------Leetcode Problem 898------------------------\n")
for _, q := range qs {
_, p := q.ans898, q.para898
fmt.Printf("【input】:%v 【output】:%v\n", p, subarrayBitwiseORs(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/0898.Bitwise-ORs-of-Subarrays/898. Bitwise ORs of Subarrays.go | leetcode/0898.Bitwise-ORs-of-Subarrays/898. Bitwise ORs of Subarrays.go | package leetcode
// 解法一 array 优化版
func subarrayBitwiseORs(A []int) int {
res, cur, isInMap := []int{}, []int{}, make(map[int]bool)
cur = append(cur, 0)
for _, v := range A {
var cur2 []int
for _, vv := range cur {
tmp := v | vv
if !inSlice(cur2, tmp) {
cur2 = append(cur2, tmp)
}
}
if !inSlice(cur2, v) {
cur2 = append(cur2, v)
}
cur = cur2
for _, vv := range cur {
if _, ok := isInMap[vv]; !ok {
isInMap[vv] = true
res = append(res, vv)
}
}
}
return len(res)
}
func inSlice(A []int, T int) bool {
for _, v := range A {
if v == T {
return true
}
}
return false
}
// 解法二 map 版
func subarrayBitwiseORs1(A []int) int {
res, t := map[int]bool{}, map[int]bool{}
for _, num := range A {
r := map[int]bool{}
r[num] = true
for n := range t {
r[(num | n)] = true
}
t = r
for n := range t {
res[n] = true
}
}
return len(res)
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0739.Daily-Temperatures/739. Daily Temperatures_test.go | leetcode/0739.Daily-Temperatures/739. Daily Temperatures_test.go | package leetcode
import (
"fmt"
"testing"
)
type question739 struct {
para739
ans739
}
// para 是参数
// one 代表第一个参数
type para739 struct {
s []int
}
// ans 是答案
// one 代表第一个答案
type ans739 struct {
one []int
}
func Test_Problem739(t *testing.T) {
qs := []question739{
{
para739{[]int{73, 74, 75, 71, 69, 72, 76, 73}},
ans739{[]int{1, 1, 4, 2, 1, 1, 0, 0}},
},
}
fmt.Printf("------------------------Leetcode Problem 739------------------------\n")
for _, q := range qs {
_, p := q.ans739, q.para739
fmt.Printf("【input】:%v 【output】:%v\n", p, dailyTemperatures(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/0739.Daily-Temperatures/739. Daily Temperatures.go | leetcode/0739.Daily-Temperatures/739. Daily Temperatures.go | package leetcode
// 解法一 普通做法
func dailyTemperatures(T []int) []int {
res, j := make([]int, len(T)), 0
for i := 0; i < len(T); i++ {
for j = i + 1; j < len(T); j++ {
if T[j] > T[i] {
res[i] = j - i
break
}
}
}
return res
}
// 解法二 单调栈
func dailyTemperatures1(T []int) []int {
res := make([]int, len(T))
var toCheck []int
for i, t := range T {
for len(toCheck) > 0 && T[toCheck[len(toCheck)-1]] < t {
idx := toCheck[len(toCheck)-1]
res[idx] = i - idx
toCheck = toCheck[:len(toCheck)-1]
}
toCheck = append(toCheck, i)
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0984.String-Without-AAA-or-BBB/984. String Without AAA or BBB_test.go | leetcode/0984.String-Without-AAA-or-BBB/984. String Without AAA or BBB_test.go | package leetcode
import (
"fmt"
"testing"
)
type question984 struct {
para984
ans984
}
// para 是参数
// one 代表第一个参数
type para984 struct {
a int
b int
}
// ans 是答案
// one 代表第一个答案
type ans984 struct {
one string
}
func Test_Problem984(t *testing.T) {
qs := []question984{
{
para984{1, 2},
ans984{"abb"},
},
{
para984{4, 1},
ans984{"aabaa"},
},
}
fmt.Printf("------------------------Leetcode Problem 984------------------------\n")
for _, q := range qs {
_, p := q.ans984, q.para984
fmt.Printf("【input】:%v 【output】:%v\n", p, strWithout3a3b(p.a, 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/0984.String-Without-AAA-or-BBB/984. String Without AAA or BBB.go | leetcode/0984.String-Without-AAA-or-BBB/984. String Without AAA or BBB.go | package leetcode
func strWithout3a3b(A int, B int) string {
ans, a, b := "", "a", "b"
if B < A {
A, B = B, A
a, b = b, a
}
Dif := B - A
if A == 1 && B == 1 { // ba
ans = b + a
} else if A == 1 && B < 5 { // bbabb
for i := 0; i < B-2; i++ {
ans = ans + b
}
ans = b + b + a + ans
} else if (B-A)/A >= 1 { //bbabbabb
for i := 0; i < A; i++ {
ans = ans + b + b + a
B = B - 2
}
for i := 0; i < B; i++ {
ans = ans + b
}
} else { //bbabbabbabbabababa
for i := 0; i < Dif; i++ {
ans = ans + b + b + a
B -= 2
A--
}
for i := 0; i < B; i++ {
ans = ans + b + a
}
}
return ans
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0643.Maximum-Average-Subarray-I/643. Maximum Average Subarray I_test.go | leetcode/0643.Maximum-Average-Subarray-I/643. Maximum Average Subarray I_test.go | package leetcode
import (
"fmt"
"testing"
)
type question643 struct {
para643
ans643
}
// para 是参数
// one 代表第一个参数
type para643 struct {
nums []int
k int
}
// ans 是答案
// one 代表第一个答案
type ans643 struct {
one float64
}
func Test_Problem643(t *testing.T) {
qs := []question643{
{
para643{[]int{1, 12, -5, -6, 50, 3}, 4},
ans643{12.75},
},
}
fmt.Printf("------------------------Leetcode Problem 643------------------------\n")
for _, q := range qs {
_, p := q.ans643, q.para643
fmt.Printf("【input】:%v 【output】:%v\n", p, findMaxAverage(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/0643.Maximum-Average-Subarray-I/643. Maximum Average Subarray I.go | leetcode/0643.Maximum-Average-Subarray-I/643. Maximum Average Subarray I.go | package leetcode
func findMaxAverage(nums []int, k int) float64 {
sum := 0
for _, v := range nums[:k] {
sum += v
}
maxSum := sum
for i := k; i < len(nums); i++ {
sum = sum - nums[i-k] + nums[i]
maxSum = max(maxSum, sum)
}
return float64(maxSum) / float64(k)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0732.My-Calendar-III/732. My Calendar III_test.go | leetcode/0732.My-Calendar-III/732. My Calendar III_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem732(t *testing.T) {
obj := Constructor732()
fmt.Printf("book = %v\n\n", obj.Book(10, 20)) // returns 1
fmt.Printf("book = %v\n\n", obj.Book(50, 60)) // returns 1
fmt.Printf("book = %v\n\n", obj.Book(10, 40)) // returns 2
fmt.Printf("book = %v\n\n", obj.Book(5, 15)) // returns 3
fmt.Printf("book = %v\n\n", obj.Book(5, 10)) // returns 3
fmt.Printf("book = %v\n\n", obj.Book(25, 55)) // returns 3
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0732.My-Calendar-III/732. My Calendar III.go | leetcode/0732.My-Calendar-III/732. My Calendar III.go | package leetcode
// SegmentTree732 define
type SegmentTree732 struct {
start, end, count int
left, right *SegmentTree732
}
// MyCalendarThree define
type MyCalendarThree struct {
st *SegmentTree732
maxHeight int
}
// Constructor732 define
func Constructor732() MyCalendarThree {
st := &SegmentTree732{
start: 0,
end: 1e9,
}
return MyCalendarThree{
st: st,
}
}
// Book define
func (mct *MyCalendarThree) Book(start int, end int) int {
mct.st.book(start, end, &mct.maxHeight)
return mct.maxHeight
}
func (st *SegmentTree732) book(start, end int, maxHeight *int) {
if start == end {
return
}
if start == st.start && st.end == end {
st.count++
if st.count > *maxHeight {
*maxHeight = st.count
}
if st.left == nil {
return
}
}
if st.left == nil {
if start == st.start {
st.left = &SegmentTree732{start: start, end: end, count: st.count}
st.right = &SegmentTree732{start: end, end: st.end, count: st.count}
st.left.book(start, end, maxHeight)
return
}
st.left = &SegmentTree732{start: st.start, end: start, count: st.count}
st.right = &SegmentTree732{start: start, end: st.end, count: st.count}
st.right.book(start, end, maxHeight)
return
}
if start >= st.right.start {
st.right.book(start, end, maxHeight)
} else if end <= st.left.end {
st.left.book(start, end, maxHeight)
} else {
st.left.book(start, st.left.end, maxHeight)
st.right.book(st.right.start, end, maxHeight)
}
}
/**
* Your MyCalendarThree object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Book(start,end);
*/
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0278.First-Bad-Version/278. First Bad Version.go | leetcode/0278.First-Bad-Version/278. First Bad Version.go | package leetcode
import "sort"
/**
* Forward declaration of isBadVersion API.
* @param version your guess about first bad version
* @return true if current version is bad
* false if current version is good
* func isBadVersion(version int) bool;
*/
func firstBadVersion(n int) int {
return sort.Search(n, func(version int) bool { return isBadVersion(version) })
}
func isBadVersion(version int) bool {
return true
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0278.First-Bad-Version/278. First Bad Version_test.go | leetcode/0278.First-Bad-Version/278. First Bad Version_test.go | package leetcode
import (
"fmt"
"testing"
)
type question278 struct {
para278
ans278
}
// para 是参数
// one 代表第一个参数
type para278 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans278 struct {
one int
}
func Test_Problem278(t *testing.T) {
qs := []question278{
{
para278{5},
ans278{4},
},
{
para278{1},
ans278{1},
},
}
fmt.Printf("------------------------Leetcode Problem 278------------------------\n")
for _, q := range qs {
_, p := q.ans278, q.para278
fmt.Printf("【input】:%v 【output】:%v\n", p, firstBadVersion(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/0566.Reshape-the-Matrix/566. Reshape the Matrix.go | leetcode/0566.Reshape-the-Matrix/566. Reshape the Matrix.go | package leetcode
func matrixReshape(nums [][]int, r int, c int) [][]int {
if canReshape(nums, r, c) {
return reshape(nums, r, c)
}
return nums
}
func canReshape(nums [][]int, r, c int) bool {
row := len(nums)
colume := len(nums[0])
if row*colume == r*c {
return true
}
return false
}
func reshape(nums [][]int, r, c int) [][]int {
newShape := make([][]int, r)
for index := range newShape {
newShape[index] = make([]int, c)
}
rowIndex, colIndex := 0, 0
for _, row := range nums {
for _, col := range row {
if colIndex == c {
colIndex = 0
rowIndex++
}
newShape[rowIndex][colIndex] = col
colIndex++
}
}
return newShape
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0566.Reshape-the-Matrix/566. Reshape the Matrix_test.go | leetcode/0566.Reshape-the-Matrix/566. Reshape the Matrix_test.go | package leetcode
import (
"fmt"
"testing"
)
type question566 struct {
para566
ans566
}
// para 是参数
// one 代表第一个参数
type para566 struct {
nums [][]int
r int
c int
}
// ans 是答案
// one 代表第一个答案
type ans566 struct {
one [][]int
}
func Test_Problem566(t *testing.T) {
qs := []question566{
{
para566{[][]int{{1, 2}, {3, 4}}, 1, 4},
ans566{[][]int{{1, 2, 3, 4}}},
},
{
para566{[][]int{{1, 2}, {3, 4}}, 2, 4},
ans566{[][]int{{1, 2}, {3, 4}}},
},
}
fmt.Printf("------------------------Leetcode Problem 566------------------------\n")
for _, q := range qs {
_, p := q.ans566, q.para566
fmt.Printf("【input】:%v 【output】:%v\n", p, matrixReshape(p.nums, p.r, p.c))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0781.Rabbits-in-Forest/781. Rabbits in Forest_test.go | leetcode/0781.Rabbits-in-Forest/781. Rabbits in Forest_test.go | package leetcode
import (
"fmt"
"testing"
)
type question781 struct {
para781
ans781
}
// para 是参数
// one 代表第一个参数
type para781 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans781 struct {
one int
}
func Test_Problem781(t *testing.T) {
qs := []question781{
{
para781{[]int{1, 1, 2}},
ans781{5},
},
{
para781{[]int{10, 10, 10}},
ans781{11},
},
{
para781{[]int{}},
ans781{0},
},
}
fmt.Printf("------------------------Leetcode Problem 781------------------------\n")
for _, q := range qs {
_, p := q.ans781, q.para781
fmt.Printf("【input】:%v 【output】:%v\n", p, numRabbits(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/0781.Rabbits-in-Forest/781. Rabbits in Forest.go | leetcode/0781.Rabbits-in-Forest/781. Rabbits in Forest.go | package leetcode
func numRabbits(ans []int) int {
total, m := 0, make(map[int]int)
for _, v := range ans {
if m[v] == 0 {
m[v] += v
total += v + 1
} else {
m[v]--
}
}
return total
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0567.Permutation-in-String/567. Permutation in String.go | leetcode/0567.Permutation-in-String/567. Permutation in String.go | package leetcode
func checkInclusion(s1 string, s2 string) bool {
var freq [256]int
if len(s2) == 0 || len(s2) < len(s1) {
return false
}
for i := 0; i < len(s1); i++ {
freq[s1[i]-'a']++
}
left, right, count := 0, 0, len(s1)
for right < len(s2) {
if freq[s2[right]-'a'] >= 1 {
count--
}
freq[s2[right]-'a']--
right++
if count == 0 {
return true
}
if right-left == len(s1) {
if freq[s2[left]-'a'] >= 0 {
count++
}
freq[s2[left]-'a']++
left++
}
}
return false
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0567.Permutation-in-String/567. Permutation in String_test.go | leetcode/0567.Permutation-in-String/567. Permutation in String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question567 struct {
para567
ans567
}
// para 是参数
// one 代表第一个参数
type para567 struct {
s string
p string
}
// ans 是答案
// one 代表第一个答案
type ans567 struct {
one bool
}
func Test_Problem567(t *testing.T) {
qs := []question567{
{
para567{"ab", "abab"},
ans567{true},
},
{
para567{"abc", "cbaebabacd"},
ans567{true},
},
{
para567{"abc", ""},
ans567{false},
},
{
para567{"abc", "abacbabc"},
ans567{true},
},
{
para567{"ab", "eidboaoo"},
ans567{false},
},
}
fmt.Printf("------------------------Leetcode Problem 567------------------------\n")
for _, q := range qs {
_, p := q.ans567, q.para567
fmt.Printf("【input】:%v 【output】:%v\n", p, checkInclusion(p.s, p.p))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0658.Find-K-Closest-Elements/658. Find K Closest Elements.go | leetcode/0658.Find-K-Closest-Elements/658. Find K Closest Elements.go | package leetcode
import "sort"
// 解法一 库函数二分搜索
func findClosestElements(arr []int, k int, x int) []int {
return arr[sort.Search(len(arr)-k, func(i int) bool { return x-arr[i] <= arr[i+k]-x }):][:k]
}
// 解法二 手撸二分搜索
func findClosestElements1(arr []int, k int, x int) []int {
low, high := 0, len(arr)-k
for low < high {
mid := low + (high-low)>>1
if x-arr[mid] > arr[mid+k]-x {
low = mid + 1
} else {
high = mid
}
}
return arr[low : low+k]
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0658.Find-K-Closest-Elements/658. Find K Closest Elements_test.go | leetcode/0658.Find-K-Closest-Elements/658. Find K Closest Elements_test.go | package leetcode
import (
"fmt"
"testing"
)
type question658 struct {
para658
ans658
}
// para 是参数
// one 代表第一个参数
type para658 struct {
arr []int
k int
x int
}
// ans 是答案
// one 代表第一个答案
type ans658 struct {
one []int
}
func Test_Problem658(t *testing.T) {
qs := []question658{
{
para658{[]int{1, 2, 3, 4, 5}, 4, 3},
ans658{[]int{1, 2, 3, 4}},
},
{
para658{[]int{1, 2, 3, 4, 5}, 4, -1},
ans658{[]int{1, 2, 3, 4}},
},
}
fmt.Printf("------------------------Leetcode Problem 658------------------------\n")
for _, q := range qs {
_, p := q.ans658, q.para658
fmt.Printf("【input】:%v 【output】:%v\n", p, findClosestElements(p.arr, p.k, 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/0438.Find-All-Anagrams-in-a-String/438. Find All Anagrams in a String.go | leetcode/0438.Find-All-Anagrams-in-a-String/438. Find All Anagrams in a String.go | package leetcode
func findAnagrams(s string, p string) []int {
var freq [256]int
result := []int{}
if len(s) == 0 || len(s) < len(p) {
return result
}
for i := 0; i < len(p); i++ {
freq[p[i]-'a']++
}
left, right, count := 0, 0, len(p)
for right < len(s) {
if freq[s[right]-'a'] >= 1 {
count--
}
freq[s[right]-'a']--
right++
if count == 0 {
result = append(result, left)
}
if right-left == len(p) {
if freq[s[left]-'a'] >= 0 {
count++
}
freq[s[left]-'a']++
left++
}
}
return result
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0438.Find-All-Anagrams-in-a-String/438. Find All Anagrams in a String_test.go | leetcode/0438.Find-All-Anagrams-in-a-String/438. Find All Anagrams in a String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question438 struct {
para438
ans438
}
// para 是参数
// one 代表第一个参数
type para438 struct {
s string
p string
}
// ans 是答案
// one 代表第一个答案
type ans438 struct {
one []int
}
func Test_Problem438(t *testing.T) {
qs := []question438{
{
para438{"abab", "ab"},
ans438{[]int{0, 1, 2}},
},
{
para438{"cbaebabacd", "abc"},
ans438{[]int{0, 6}},
},
{
para438{"", "abc"},
ans438{[]int{}},
},
{
para438{"abacbabc", "abc"},
ans438{[]int{1, 2, 3, 5}},
},
}
fmt.Printf("------------------------Leetcode Problem 438------------------------\n")
for _, q := range qs {
_, p := q.ans438, q.para438
fmt.Printf("【input】:%v 【output】:%v\n", p, findAnagrams(p.s, p.p))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0135.Candy/135. Candy.go | leetcode/0135.Candy/135. Candy.go | package leetcode
func candy(ratings []int) int {
candies := make([]int, len(ratings))
for i := 1; i < len(ratings); i++ {
if ratings[i] > ratings[i-1] {
candies[i] += candies[i-1] + 1
}
}
for i := len(ratings) - 2; i >= 0; i-- {
if ratings[i] > ratings[i+1] && candies[i] <= candies[i+1] {
candies[i] = candies[i+1] + 1
}
}
total := 0
for _, candy := range candies {
total += candy + 1
}
return total
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0135.Candy/135. Candy_test.go | leetcode/0135.Candy/135. Candy_test.go | package leetcode
import (
"fmt"
"testing"
)
type question135 struct {
para135
ans135
}
// para 是参数
// one 代表第一个参数
type para135 struct {
ratings []int
}
// ans 是答案
// one 代表第一个答案
type ans135 struct {
one int
}
func Test_Problem135(t *testing.T) {
qs := []question135{
{
para135{[]int{1, 0, 2}},
ans135{5},
},
{
para135{[]int{1, 2, 2}},
ans135{4},
},
}
fmt.Printf("------------------------Leetcode Problem 135------------------------\n")
for _, q := range qs {
_, p := q.ans135, q.para135
fmt.Printf("【input】:%v 【output】:%v\n", p, candy(p.ratings))
}
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/1111.Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/1111. Maximum Nesting Depth of Two Valid Parentheses Strings.go | leetcode/1111.Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/1111. Maximum Nesting Depth of Two Valid Parentheses Strings.go | package leetcode
// 解法一 二分思想
func maxDepthAfterSplit(seq string) []int {
stack, maxDepth, res := 0, 0, []int{}
for _, v := range seq {
if v == '(' {
stack++
maxDepth = max(stack, maxDepth)
} else {
stack--
}
}
stack = 0
for i := 0; i < len(seq); i++ {
if seq[i] == '(' {
stack++
if stack <= maxDepth/2 {
res = append(res, 0)
} else {
res = append(res, 1)
}
} else {
if stack <= maxDepth/2 {
res = append(res, 0)
} else {
res = append(res, 1)
}
stack--
}
}
return res
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
// 解法二 模拟
func maxDepthAfterSplit1(seq string) []int {
stack, top, res := make([]int, len(seq)), -1, make([]int, len(seq))
for i, r := range seq {
if r == ')' {
res[i] = res[stack[top]]
top--
continue
}
top++
stack[top] = i
res[i] = top % 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/1111.Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/1111. Maximum Nesting Depth of Two Valid Parentheses Strings_test.go | leetcode/1111.Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/1111. Maximum Nesting Depth of Two Valid Parentheses Strings_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1111 struct {
para1111
ans1111
}
// para 是参数
// one 代表第一个参数
type para1111 struct {
one string
}
// ans 是答案
// one 代表第一个答案
type ans1111 struct {
one []int
}
func Test_Problem1111(t *testing.T) {
qs := []question1111{
{
para1111{"(()())"},
ans1111{[]int{0, 1, 1, 1, 1, 0}},
},
{
para1111{"()(())()"},
ans1111{[]int{0, 0, 0, 1, 1, 0, 1, 1}},
},
}
fmt.Printf("------------------------Leetcode Problem 1111------------------------\n")
for _, q := range qs {
_, p := q.ans1111, q.para1111
fmt.Printf("【input】:%v 【output】:%v\n", p, maxDepthAfterSplit(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/1446.Consecutive-Characters/1446.Consecutive Characters_test.go | leetcode/1446.Consecutive-Characters/1446.Consecutive Characters_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1446 struct {
para1446
ans1446
}
// para 是参数
type para1446 struct {
s string
}
// ans 是答案
type ans1446 struct {
ans int
}
func Test_Problem1446(t *testing.T) {
qs := []question1446{
{
para1446{"leetcode"},
ans1446{2},
},
{
para1446{"abbcccddddeeeeedcba"},
ans1446{5},
},
{
para1446{"triplepillooooow"},
ans1446{5},
},
{
para1446{"hooraaaaaaaaaaay"},
ans1446{11},
},
{
para1446{"tourist"},
ans1446{1},
},
}
fmt.Printf("------------------------Leetcode Problem 1446------------------------\n")
for _, q := range qs {
_, p := q.ans1446, q.para1446
fmt.Printf("【input】:%v 【output】:%v\n", p.s, maxPower(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/1446.Consecutive-Characters/1446.Consecutive Characters.go | leetcode/1446.Consecutive-Characters/1446.Consecutive Characters.go | package leetcode
func maxPower(s string) int {
cur, cnt, ans := s[0], 1, 1
for i := 1; i < len(s); i++ {
if cur == s[i] {
cnt++
} else {
if cnt > ans {
ans = cnt
}
cur = s[i]
cnt = 1
}
}
if cnt > ans {
ans = cnt
}
return ans
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0257.Binary-Tree-Paths/257. Binary Tree Paths_test.go | leetcode/0257.Binary-Tree-Paths/257. Binary Tree Paths_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question257 struct {
para257
ans257
}
// para 是参数
// one 代表第一个参数
type para257 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans257 struct {
one []string
}
func Test_Problem257(t *testing.T) {
qs := []question257{
{
para257{[]int{}},
ans257{[]string{""}},
},
{
para257{[]int{1, 2, 3, structures.NULL, 5, structures.NULL, structures.NULL}},
ans257{[]string{"1->2->5", "1->3"}},
},
{
para257{[]int{1, 2, 3, 4, 5, 6, 7}},
ans257{[]string{"1->2->4", "1->2->5", "1->2->4", "1->2->5", "1->3->6", "1->3->7"}},
},
}
fmt.Printf("------------------------Leetcode Problem 257------------------------\n")
for _, q := range qs {
_, p := q.ans257, q.para257
fmt.Printf("【input】:%v ", p)
root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", binaryTreePaths(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/0257.Binary-Tree-Paths/257. Binary Tree Paths.go | leetcode/0257.Binary-Tree-Paths/257. Binary Tree Paths.go | package leetcode
import (
"strconv"
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func binaryTreePaths(root *TreeNode) []string {
if root == nil {
return []string{}
}
res := []string{}
if root.Left == nil && root.Right == nil {
return []string{strconv.Itoa(root.Val)}
}
tmpLeft := binaryTreePaths(root.Left)
for i := 0; i < len(tmpLeft); i++ {
res = append(res, strconv.Itoa(root.Val)+"->"+tmpLeft[i])
}
tmpRight := binaryTreePaths(root.Right)
for i := 0; i < len(tmpRight); i++ {
res = append(res, strconv.Itoa(root.Val)+"->"+tmpRight[i])
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0110.Balanced-Binary-Tree/110. Balanced Binary Tree.go | leetcode/0110.Balanced-Binary-Tree/110. Balanced 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 isBalanced(root *TreeNode) bool {
if root == nil {
return true
}
leftHight := depth(root.Left)
rightHight := depth(root.Right)
return abs(leftHight-rightHight) <= 1 && isBalanced(root.Left) && isBalanced(root.Right)
}
func depth(root *TreeNode) int {
if root == nil {
return 0
}
return max(depth(root.Left), depth(root.Right)) + 1
}
func abs(a int) int {
if a > 0 {
return a
}
return -a
}
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/0110.Balanced-Binary-Tree/110. Balanced Binary Tree_test.go | leetcode/0110.Balanced-Binary-Tree/110. Balanced Binary Tree_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question110 struct {
para110
ans110
}
// para 是参数
// one 代表第一个参数
type para110 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans110 struct {
one bool
}
func Test_Problem110(t *testing.T) {
qs := []question110{
{
para110{[]int{3, 4, 4, 5, structures.NULL, structures.NULL, 5, 6, structures.NULL, structures.NULL, 6}},
ans110{false},
},
{
para110{[]int{1, 2, 2, structures.NULL, 3, 3}},
ans110{true},
},
{
para110{[]int{}},
ans110{true},
},
{
para110{[]int{1}},
ans110{true},
},
{
para110{[]int{1, 2, 3}},
ans110{true},
},
{
para110{[]int{1, 2, 2, 3, 4, 4, 3}},
ans110{true},
},
{
para110{[]int{1, 2, 2, structures.NULL, 3, structures.NULL, 3}},
ans110{true},
},
}
fmt.Printf("------------------------Leetcode Problem 110------------------------\n")
for _, q := range qs {
_, p := q.ans110, q.para110
fmt.Printf("【input】:%v ", p)
rootOne := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", isBalanced(rootOne))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0126.Word-Ladder-II/126. Word Ladder II.go | leetcode/0126.Word-Ladder-II/126. Word Ladder II.go | package leetcode
func findLadders(beginWord string, endWord string, wordList []string) [][]string {
result, wordMap := make([][]string, 0), make(map[string]bool)
for _, w := range wordList {
wordMap[w] = true
}
if !wordMap[endWord] {
return result
}
// create a queue, track the path
queue := make([][]string, 0)
queue = append(queue, []string{beginWord})
// queueLen is used to track how many slices in queue are in the same level
// if found a result, I still need to finish checking current level cause I need to return all possible paths
queueLen := 1
// use to track strings that this level has visited
// when queueLen == 0, remove levelMap keys in wordMap
levelMap := make(map[string]bool)
for len(queue) > 0 {
path := queue[0]
queue = queue[1:]
lastWord := path[len(path)-1]
for i := 0; i < len(lastWord); i++ {
for c := 'a'; c <= 'z'; c++ {
nextWord := lastWord[:i] + string(c) + lastWord[i+1:]
if nextWord == endWord {
path = append(path, endWord)
result = append(result, path)
continue
}
if wordMap[nextWord] {
// different from word ladder, don't remove the word from wordMap immediately
// same level could reuse the key.
// delete from wordMap only when currently level is done.
levelMap[nextWord] = true
newPath := make([]string, len(path))
copy(newPath, path)
newPath = append(newPath, nextWord)
queue = append(queue, newPath)
}
}
}
queueLen--
// if queueLen is 0, means finish traversing current level. if result is not empty, return result
if queueLen == 0 {
if len(result) > 0 {
return result
}
for k := range levelMap {
delete(wordMap, k)
}
// clear levelMap
levelMap = make(map[string]bool)
queueLen = len(queue)
}
}
return result
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0126.Word-Ladder-II/126. Word Ladder II_test.go | leetcode/0126.Word-Ladder-II/126. Word Ladder II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question126 struct {
para126
ans126
}
// para 是参数
// one 代表第一个参数
type para126 struct {
b string
e string
w []string
}
// ans 是答案
// one 代表第一个答案
type ans126 struct {
one [][]string
}
func Test_Problem126(t *testing.T) {
qs := []question126{
{
para126{"hit", "cog", []string{"hot", "dot", "dog", "lot", "log", "cog"}},
ans126{[][]string{{"hit", "hot", "dot", "dog", "cog"}, {"hit", "hot", "lot", "log", "cog"}}},
},
{
para126{"hit", "cog", []string{"hot", "dot", "dog", "lot", "log"}},
ans126{[][]string{}},
},
}
fmt.Printf("------------------------Leetcode Problem 126------------------------\n")
for _, q := range qs {
_, p := q.ans126, q.para126
fmt.Printf("【input】:%v 【output】:%v\n", p, findLadders(p.b, p.e, p.w))
}
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/1078.Occurrences-After-Bigram/1078. Occurrences After Bigram_test.go | leetcode/1078.Occurrences-After-Bigram/1078. Occurrences After Bigram_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1078 struct {
para1078
ans1078
}
// para 是参数
// one 代表第一个参数
type para1078 struct {
t string
f string
s string
}
// ans 是答案
// one 代表第一个答案
type ans1078 struct {
one []string
}
func Test_Problem1078(t *testing.T) {
qs := []question1078{
{
para1078{"alice is a good girl she is a good student", "a", "good"},
ans1078{[]string{"girl", "student"}},
},
{
para1078{"we will we will rock you", "we", "will"},
ans1078{[]string{"we", "rock"}},
},
}
fmt.Printf("------------------------Leetcode Problem 1078------------------------\n")
for _, q := range qs {
_, p := q.ans1078, q.para1078
fmt.Printf("【input】:%v 【output】:%v\n", p, findOcurrences(p.t, p.f, 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/1078.Occurrences-After-Bigram/1078. Occurrences After Bigram.go | leetcode/1078.Occurrences-After-Bigram/1078. Occurrences After Bigram.go | package leetcode
import "strings"
func findOcurrences(text string, first string, second string) []string {
var res []string
words := strings.Split(text, " ")
if len(words) < 3 {
return []string{}
}
for i := 2; i < len(words); i++ {
if words[i-2] == first && words[i-1] == second {
res = append(res, words[i])
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0284.Peeking-Iterator/284. Peeking Iterator.go | leetcode/0284.Peeking-Iterator/284. Peeking Iterator.go | package leetcode
//Below is the interface for Iterator, which is already defined for you.
type Iterator struct {
}
func (this *Iterator) hasNext() bool {
// Returns true if the iteration has more elements.
return true
}
func (this *Iterator) next() int {
// Returns the next element in the iteration.
return 0
}
type PeekingIterator struct {
nextEl int
hasEl bool
iter *Iterator
}
func Constructor(iter *Iterator) *PeekingIterator {
return &PeekingIterator{
iter: iter,
}
}
func (this *PeekingIterator) hasNext() bool {
if this.hasEl {
return true
}
return this.iter.hasNext()
}
func (this *PeekingIterator) next() int {
if this.hasEl {
this.hasEl = false
return this.nextEl
} else {
return this.iter.next()
}
}
func (this *PeekingIterator) peek() int {
if this.hasEl {
return this.nextEl
}
this.hasEl = true
this.nextEl = this.iter.next()
return this.nextEl
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0284.Peeking-Iterator/284. Peeking Iterator_test.go | leetcode/0284.Peeking-Iterator/284. Peeking Iterator_test.go | package leetcode
import (
"testing"
)
func Test_Problem284(t *testing.T) {
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1217.Minimum-Cost-to-Move-Chips-to-The-Same-Position/1217. Minimum Cost to Move Chips to The Same Position_test.go | leetcode/1217.Minimum-Cost-to-Move-Chips-to-The-Same-Position/1217. Minimum Cost to Move Chips to The Same Position_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1217 struct {
para1217
ans1217
}
// para 是参数
// one 代表第一个参数
type para1217 struct {
arr []int
}
// ans 是答案
// one 代表第一个答案
type ans1217 struct {
one int
}
func Test_Problem1217(t *testing.T) {
qs := []question1217{
{
para1217{[]int{1, 2, 3}},
ans1217{1},
},
{
para1217{[]int{2, 2, 2, 3, 3}},
ans1217{2},
},
}
fmt.Printf("------------------------Leetcode Problem 1217------------------------\n")
for _, q := range qs {
_, p := q.ans1217, q.para1217
fmt.Printf("【input】:%v 【output】:%v\n", p, minCostToMoveChips(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/1217.Minimum-Cost-to-Move-Chips-to-The-Same-Position/1217. Minimum Cost to Move Chips to The Same Position.go | leetcode/1217.Minimum-Cost-to-Move-Chips-to-The-Same-Position/1217. Minimum Cost to Move Chips to The Same Position.go | package leetcode
func minCostToMoveChips(chips []int) int {
odd, even := 0, 0
for _, c := range chips {
if c%2 == 0 {
even++
} else {
odd++
}
}
return min(odd, even)
}
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/0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal/105. Construct Binary Tree from Preorder and Inorder Traversal.go | leetcode/0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal/105. Construct Binary Tree from Preorder and Inorder Traversal.go | package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// 解法一, 直接传入需要的 slice 范围作为输入, 可以避免申请对应 inorder 索引的内存, 内存使用(leetcode test case) 4.7MB -> 4.3MB.
func buildTree(preorder []int, inorder []int) *TreeNode {
if len(preorder) == 0 {
return nil
}
root := &TreeNode{Val: preorder[0]}
for pos, node := range inorder {
if node == root.Val {
root.Left = buildTree(preorder[1:pos+1], inorder[:pos])
root.Right = buildTree(preorder[pos+1:], inorder[pos+1:])
}
}
return root
}
// 解法二
func buildTree1(preorder []int, inorder []int) *TreeNode {
inPos := make(map[int]int)
for i := 0; i < len(inorder); i++ {
inPos[inorder[i]] = i
}
return buildPreIn2TreeDFS(preorder, 0, len(preorder)-1, 0, inPos)
}
func buildPreIn2TreeDFS(pre []int, preStart int, preEnd int, inStart int, inPos map[int]int) *TreeNode {
if preStart > preEnd {
return nil
}
root := &TreeNode{Val: pre[preStart]}
rootIdx := inPos[pre[preStart]]
leftLen := rootIdx - inStart
root.Left = buildPreIn2TreeDFS(pre, preStart+1, preStart+leftLen, inStart, inPos)
root.Right = buildPreIn2TreeDFS(pre, preStart+leftLen+1, preEnd, rootIdx+1, inPos)
return root
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal/105. Construct Binary Tree from Preorder and Inorder Traversal_test.go | leetcode/0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal/105. Construct Binary Tree from Preorder and Inorder Traversal_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question105 struct {
para105
ans105
}
// para 是参数
// one 代表第一个参数
type para105 struct {
preorder []int
inorder []int
}
// ans 是答案
// one 代表第一个答案
type ans105 struct {
one []int
}
func Test_Problem105(t *testing.T) {
qs := []question105{
{
para105{[]int{3, 9, 20, 15, 7}, []int{9, 3, 15, 20, 7}},
ans105{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
},
}
fmt.Printf("------------------------Leetcode Problem 105------------------------\n")
for _, q := range qs {
_, p := q.ans105, q.para105
fmt.Printf("【input】:%v ", p)
fmt.Printf("【output】:%v \n", structures.Tree2ints(buildTree(p.preorder, p.inorder)))
}
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/0357.Count-Numbers-with-Unique-Digits/357. Count Numbers with Unique Digits.go | leetcode/0357.Count-Numbers-with-Unique-Digits/357. Count Numbers with Unique Digits.go | package leetcode
// 暴力打表法
func countNumbersWithUniqueDigits1(n int) int {
res := []int{1, 10, 91, 739, 5275, 32491, 168571, 712891, 2345851, 5611771, 8877691}
if n >= 10 {
return res[10]
}
return res[n]
}
// 打表方法
func countNumbersWithUniqueDigits(n int) int {
if n == 0 {
return 1
}
res, uniqueDigits, availableNumber := 10, 9, 9
for n > 1 && availableNumber > 0 {
uniqueDigits = uniqueDigits * availableNumber
res += uniqueDigits
availableNumber--
n--
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0357.Count-Numbers-with-Unique-Digits/357. Count Numbers with Unique Digits_test.go | leetcode/0357.Count-Numbers-with-Unique-Digits/357. Count Numbers with Unique Digits_test.go | package leetcode
import (
"fmt"
"testing"
)
type question357 struct {
para357
ans357
}
// para 是参数
// one 代表第一个参数
type para357 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans357 struct {
one int
}
func Test_Problem357(t *testing.T) {
qs := []question357{
{
para357{1},
ans357{10},
},
{
para357{2},
ans357{91},
},
{
para357{3},
ans357{739},
},
{
para357{4},
ans357{5275},
},
{
para357{5},
ans357{32491},
},
{
para357{6},
ans357{168571},
},
// 如需多个测试,可以复制上方元素。
}
fmt.Printf("------------------------Leetcode Problem 357------------------------\n")
for _, q := range qs {
_, p := q.ans357, q.para357
fmt.Printf("【input】:%v 【output】:%v\n", p, countNumbersWithUniqueDigits(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/0287.Find-the-Duplicate-Number/287. Find the Duplicate Number.go | leetcode/0287.Find-the-Duplicate-Number/287. Find the Duplicate Number.go | package leetcode
import "sort"
// 解法一 快慢指针
func findDuplicate(nums []int) int {
slow := nums[0]
fast := nums[nums[0]]
for fast != slow {
slow = nums[slow]
fast = nums[nums[fast]]
}
walker := 0
for walker != slow {
walker = nums[walker]
slow = nums[slow]
}
return walker
}
// 解法二 二分搜索
func findDuplicate1(nums []int) int {
low, high := 0, len(nums)-1
for low < high {
mid, count := low+(high-low)>>1, 0
for _, num := range nums {
if num <= mid {
count++
}
}
if count > mid {
high = mid
} else {
low = mid + 1
}
}
return low
}
// 解法三
func findDuplicate2(nums []int) int {
if len(nums) == 0 {
return 0
}
sort.Ints(nums)
diff := -1
for i := 0; i < len(nums); i++ {
if nums[i]-i-1 >= diff {
diff = nums[i] - i - 1
} else {
return nums[i]
}
}
return 0
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0287.Find-the-Duplicate-Number/287. Find the Duplicate Number_test.go | leetcode/0287.Find-the-Duplicate-Number/287. Find the Duplicate Number_test.go | package leetcode
import (
"fmt"
"testing"
)
type question287 struct {
para287
ans287
}
// para 是参数
// one 代表第一个参数
type para287 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans287 struct {
one int
}
func Test_Problem287(t *testing.T) {
qs := []question287{
{
para287{[]int{1, 3, 4, 2, 2}},
ans287{2},
},
{
para287{[]int{3, 1, 3, 4, 2}},
ans287{3},
},
{
para287{[]int{2, 2, 2, 2, 2}},
ans287{2},
},
}
fmt.Printf("------------------------Leetcode Problem 287------------------------\n")
for _, q := range qs {
_, p := q.ans287, q.para287
fmt.Printf("【input】:%v 【output】:%v\n", p, findDuplicate(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/0953.Verifying-an-Alien-Dictionary/953. Verifying an Alien Dictionary_test.go | leetcode/0953.Verifying-an-Alien-Dictionary/953. Verifying an Alien Dictionary_test.go | package leetcode
import (
"fmt"
"testing"
)
type question953 struct {
para953
ans953
}
// para 是参数
// one 代表第一个参数
type para953 struct {
one []string
two string
}
// ans 是答案
// one 代表第一个答案
type ans953 struct {
one bool
}
func Test_Problem953(t *testing.T) {
qs := []question953{
{
para953{[]string{"hello", "leetcode"}, "hlabcdefgijkmnopqrstuvwxyz"},
ans953{true},
},
{
para953{[]string{"word", "world", "row"}, "worldabcefghijkmnpqstuvxyz"},
ans953{false},
},
{
para953{[]string{"apple", "app"}, "abcdefghijklmnopqrstuvwxyz"},
ans953{false},
},
}
fmt.Printf("------------------------Leetcode Problem 953------------------------\n")
for _, q := range qs {
_, p := q.ans953, q.para953
fmt.Printf("【input】:%v 【output】:%v\n", p, isAlienSorted(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/0953.Verifying-an-Alien-Dictionary/953. Verifying an Alien Dictionary.go | leetcode/0953.Verifying-an-Alien-Dictionary/953. Verifying an Alien Dictionary.go | package leetcode
func isAlienSorted(words []string, order string) bool {
if len(words) < 2 {
return true
}
hash := make(map[byte]int)
for i := 0; i < len(order); i++ {
hash[order[i]] = i
}
for i := 0; i < len(words)-1; i++ {
pointer, word, wordplus := 0, words[i], words[i+1]
for pointer < len(word) && pointer < len(wordplus) {
if hash[word[pointer]] > hash[wordplus[pointer]] {
return false
}
if hash[word[pointer]] < hash[wordplus[pointer]] {
break
} else {
pointer = pointer + 1
}
}
if pointer < len(word) && pointer >= len(wordplus) {
return false
}
}
return true
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0205.Isomorphic-Strings/205. Isomorphic Strings_test.go | leetcode/0205.Isomorphic-Strings/205. Isomorphic Strings_test.go | package leetcode
import (
"fmt"
"testing"
)
type question205 struct {
para205
ans205
}
// para 是参数
// one 代表第一个参数
type para205 struct {
one string
two string
}
// ans 是答案
// one 代表第一个答案
type ans205 struct {
one bool
}
func Test_Problem205(t *testing.T) {
qs := []question205{
{
para205{"egg", "add"},
ans205{true},
},
{
para205{"foo", "bar"},
ans205{false},
},
{
para205{"paper", "title"},
ans205{true},
},
{
para205{"", ""},
ans205{true},
},
}
fmt.Printf("------------------------Leetcode Problem 205------------------------\n")
for _, q := range qs {
_, p := q.ans205, q.para205
fmt.Printf("【input】:%v 【output】:%v\n", p, isIsomorphic(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/0205.Isomorphic-Strings/205. Isomorphic Strings.go | leetcode/0205.Isomorphic-Strings/205. Isomorphic Strings.go | package leetcode
func isIsomorphic(s string, t string) bool {
strList := []byte(t)
patternByte := []byte(s)
if (s == "" && t != "") || (len(patternByte) != len(strList)) {
return false
}
pMap := map[byte]byte{}
sMap := map[byte]byte{}
for index, b := range patternByte {
if _, ok := pMap[b]; !ok {
if _, ok = sMap[strList[index]]; !ok {
pMap[b] = strList[index]
sMap[strList[index]] = b
} else {
if sMap[strList[index]] != b {
return false
}
}
} else {
if pMap[b] != strList[index] {
return false
}
}
}
return true
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0083.Remove-Duplicates-from-Sorted-List/83. Remove Duplicates from Sorted List.go | leetcode/0083.Remove-Duplicates-from-Sorted-List/83. Remove Duplicates from Sorted 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 deleteDuplicates(head *ListNode) *ListNode {
cur := head
if head == nil {
return nil
}
if head.Next == nil {
return head
}
for cur.Next != nil {
if cur.Next.Val == cur.Val {
cur.Next = cur.Next.Next
} else {
cur = cur.Next
}
}
return head
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0083.Remove-Duplicates-from-Sorted-List/83. Remove Duplicates from Sorted List_test.go | leetcode/0083.Remove-Duplicates-from-Sorted-List/83. Remove Duplicates from Sorted List_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question83 struct {
para83
ans83
}
// para 是参数
// one 代表第一个参数
type para83 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans83 struct {
one []int
}
func Test_Problem83(t *testing.T) {
qs := []question83{
{
para83{[]int{1, 1, 2}},
ans83{[]int{1, 2}},
},
{
para83{[]int{1, 1, 2, 2, 3, 3, 3}},
ans83{[]int{1, 2, 3}},
},
{
para83{[]int{1, 1, 1, 1, 1, 1, 1, 1}},
ans83{[]int{1}},
},
}
fmt.Printf("------------------------Leetcode Problem 83------------------------\n")
for _, q := range qs {
_, p := q.ans83, q.para83
fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(deleteDuplicates(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/0230.Kth-Smallest-Element-in-a-BST/230. Kth Smallest Element in a BST_test.go | leetcode/0230.Kth-Smallest-Element-in-a-BST/230. Kth Smallest Element in a BST_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question230 struct {
para230
ans230
}
// para 是参数
// one 代表第一个参数
type para230 struct {
one []int
k int
}
// ans 是答案
// one 代表第一个答案
type ans230 struct {
one int
}
func Test_Problem230(t *testing.T) {
qs := []question230{
{
para230{[]int{}, 0},
ans230{0},
},
{
para230{[]int{3, 1, 4, structures.NULL, 2}, 1},
ans230{1},
},
{
para230{[]int{5, 3, 6, 2, 4, structures.NULL, structures.NULL, 1}, 3},
ans230{3},
},
}
fmt.Printf("------------------------Leetcode Problem 230------------------------\n")
for _, q := range qs {
_, p := q.ans230, q.para230
fmt.Printf("【input】:%v ", p)
root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", kthSmallest(root, 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/0230.Kth-Smallest-Element-in-a-BST/230. Kth Smallest Element in a BST.go | leetcode/0230.Kth-Smallest-Element-in-a-BST/230. Kth Smallest Element in a BST.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 kthSmallest(root *TreeNode, k int) int {
res, count := 0, 0
inorder230(root, k, &count, &res)
return res
}
func inorder230(node *TreeNode, k int, count *int, ans *int) {
if node != nil {
inorder230(node.Left, k, count, ans)
*count++
if *count == k {
*ans = node.Val
return
}
inorder230(node.Right, k, count, ans)
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0022.Generate-Parentheses/22. Generate Parentheses_test.go | leetcode/0022.Generate-Parentheses/22. Generate Parentheses_test.go | package leetcode
import (
"fmt"
"testing"
)
type question22 struct {
para22
ans22
}
// para 是参数
// one 代表第一个参数
type para22 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans22 struct {
one []string
}
func Test_Problem22(t *testing.T) {
qs := []question22{
{
para22{3},
ans22{[]string{
"((()))",
"(()())",
"(())()",
"()(())",
"()()()",
}},
},
// 如需多个测试,可以复制上方元素。
}
fmt.Printf("------------------------Leetcode Problem 22------------------------\n")
for _, q := range qs {
_, p := q.ans22, q.para22
fmt.Printf("【input】:%v 【output】:%v\n", p, generateParenthesis(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/0022.Generate-Parentheses/22. Generate Parentheses.go | leetcode/0022.Generate-Parentheses/22. Generate Parentheses.go | package leetcode
func generateParenthesis(n int) []string {
if n == 0 {
return []string{}
}
res := []string{}
findGenerateParenthesis(n, n, "", &res)
return res
}
func findGenerateParenthesis(lindex, rindex int, str string, res *[]string) {
if lindex == 0 && rindex == 0 {
*res = append(*res, str)
return
}
if lindex > 0 {
findGenerateParenthesis(lindex-1, rindex, str+"(", res)
}
if rindex > 0 && lindex < rindex {
findGenerateParenthesis(lindex, rindex-1, str+")", res)
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0885.Spiral-Matrix-III/885. Spiral Matrix III_test.go | leetcode/0885.Spiral-Matrix-III/885. Spiral Matrix III_test.go | package leetcode
import (
"fmt"
"testing"
)
type question885 struct {
para885
ans885
}
// para 是参数
// one 代表第一个参数
type para885 struct {
R int
C int
r0 int
c0 int
}
// ans 是答案
// one 代表第一个答案
type ans885 struct {
one [][]int
}
func Test_Problem885(t *testing.T) {
qs := []question885{
{
para885{1, 4, 0, 0},
ans885{[][]int{{0, 0}, {0, 1}, {0, 2}, {0, 3}}},
},
{
para885{5, 6, 1, 4},
ans885{[][]int{{1, 4}, {1, 5}, {2, 5}, {2, 4}, {2, 3}, {1, 3}, {0, 3}, {0, 4}, {0, 5}, {3, 5}, {3, 4},
{3, 3}, {3, 2}, {2, 2}, {1, 2}, {0, 2}, {4, 5}, {4, 4}, {4, 3}, {4, 2}, {4, 1}, {3, 1}, {2, 1},
{1, 1}, {0, 1}, {4, 0}, {3, 0}, {2, 0}, {1, 0}, {0, 0}}},
},
}
fmt.Printf("------------------------Leetcode Problem 885------------------------\n")
for _, q := range qs {
_, p := q.ans885, q.para885
fmt.Printf("【input】:%v 【output】:%v\n", p, spiralMatrixIII(p.R, p.C, p.r0, p.c0))
}
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/0885.Spiral-Matrix-III/885. Spiral Matrix III.go | leetcode/0885.Spiral-Matrix-III/885. Spiral Matrix III.go | package leetcode
func spiralMatrixIII(R int, C int, r0 int, c0 int) [][]int {
res, round, spDir := [][]int{}, 0, [][]int{
{0, 1}, // 朝右
{1, 0}, // 朝下
{0, -1}, // 朝左
{-1, 0}, // 朝上
}
res = append(res, []int{r0, c0})
for i := 0; len(res) < R*C; i++ {
for j := 0; j < i/2+1; j++ {
r0 += spDir[round%4][0]
c0 += spDir[round%4][1]
if 0 <= r0 && r0 < R && 0 <= c0 && c0 < C {
res = append(res, []int{r0, c0})
}
}
round++
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0802.Find-Eventual-Safe-States/802. Find Eventual Safe States_test.go | leetcode/0802.Find-Eventual-Safe-States/802. Find Eventual Safe States_test.go | package leetcode
import (
"fmt"
"testing"
)
type question802 struct {
para802
ans802
}
// para 是参数
// one 代表第一个参数
type para802 struct {
graph [][]int
}
// ans 是答案
// one 代表第一个答案
type ans802 struct {
one []int
}
func Test_Problem802(t *testing.T) {
qs := []question802{
{
para802{[][]int{{1, 2}, {2, 3}, {5}, {0}, {5}, {}, {}}},
ans802{[]int{2, 4, 5, 6}},
},
}
fmt.Printf("------------------------Leetcode Problem 802------------------------\n")
for _, q := range qs {
_, p := q.ans802, q.para802
fmt.Printf("【input】:%v 【output】:%v\n", p, eventualSafeNodes(p.graph))
}
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/0802.Find-Eventual-Safe-States/802. Find Eventual Safe States.go | leetcode/0802.Find-Eventual-Safe-States/802. Find Eventual Safe States.go | package leetcode
func eventualSafeNodes(graph [][]int) []int {
res, color := []int{}, make([]int, len(graph))
for i := range graph {
if dfsEventualSafeNodes(graph, i, color) {
res = append(res, i)
}
}
return res
}
// colors: WHITE 0, GRAY 1, BLACK 2;
func dfsEventualSafeNodes(graph [][]int, idx int, color []int) bool {
if color[idx] > 0 {
return color[idx] == 2
}
color[idx] = 1
for i := range graph[idx] {
if !dfsEventualSafeNodes(graph, graph[idx][i], color) {
return false
}
}
color[idx] = 2
return true
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0628.Maximum-Product-of-Three-Numbers/628. Maximum Product of Three Numbers.go | leetcode/0628.Maximum-Product-of-Three-Numbers/628. Maximum Product of Three Numbers.go | package leetcode
import (
"math"
"sort"
)
// 解法一 排序,时间复杂度 O(n log n)
func maximumProduct(nums []int) int {
if len(nums) == 0 {
return 0
}
res := 1
if len(nums) <= 3 {
for i := 0; i < len(nums); i++ {
res = res * nums[i]
}
return res
}
sort.Ints(nums)
if nums[len(nums)-1] <= 0 {
return 0
}
return max(nums[0]*nums[1]*nums[len(nums)-1], nums[len(nums)-1]*nums[len(nums)-2]*nums[len(nums)-3])
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
// 解法二 模拟,时间复杂度 O(n)
func maximumProduct1(nums []int) int {
max := make([]int, 0)
max = append(max, math.MinInt64, math.MinInt64, math.MinInt64)
min := make([]int, 0)
min = append(min, math.MaxInt64, math.MaxInt64)
for _, num := range nums {
if num > max[0] {
max[0], max[1], max[2] = num, max[0], max[1]
} else if num > max[1] {
max[1], max[2] = num, max[1]
} else if num > max[2] {
max[2] = num
}
if num < min[0] {
min[0], min[1] = num, min[0]
} else if num < min[1] {
min[1] = num
}
}
maxProduct1, maxProduct2 := min[0]*min[1]*max[0], max[0]*max[1]*max[2]
if maxProduct1 > maxProduct2 {
return maxProduct1
}
return maxProduct2
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0628.Maximum-Product-of-Three-Numbers/628. Maximum Product of Three Numbers_test.go | leetcode/0628.Maximum-Product-of-Three-Numbers/628. Maximum Product of Three Numbers_test.go | package leetcode
import (
"fmt"
"testing"
)
type question628 struct {
para628
ans628
}
// para 是参数
// one 代表第一个参数
type para628 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans628 struct {
one int
}
func Test_Problem628(t *testing.T) {
qs := []question628{
{
para628{[]int{3, -1, 4}},
ans628{-12},
},
{
para628{[]int{1, 2}},
ans628{2},
},
{
para628{[]int{1, 2, 3}},
ans628{6},
},
{
para628{[]int{1, 2, 3, 4}},
ans628{24},
},
{
para628{[]int{-2}},
ans628{-2},
},
{
para628{[]int{0}},
ans628{0},
},
{
para628{[]int{2, 3, -2, 4}},
ans628{-24},
},
{
para628{[]int{-2, 0, -1}},
ans628{0},
},
{
para628{[]int{-2, 0, -1, 2, 3, 1, 10}},
ans628{60},
},
}
fmt.Printf("------------------------Leetcode Problem 628------------------------\n")
for _, q := range qs {
_, p := q.ans628, q.para628
fmt.Printf("【input】:%v 【output】:%v\n", p, maximumProduct(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/0525.Contiguous-Array/525. Contiguous Array.go | leetcode/0525.Contiguous-Array/525. Contiguous Array.go | package leetcode
func findMaxLength(nums []int) int {
dict := map[int]int{}
dict[0] = -1
count, res := 0, 0
for i := 0; i < len(nums); i++ {
if nums[i] == 0 {
count--
} else {
count++
}
if idx, ok := dict[count]; ok {
res = max(res, i-idx)
} else {
dict[count] = i
}
}
return res
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0525.Contiguous-Array/525. Contiguous Array_test.go | leetcode/0525.Contiguous-Array/525. Contiguous Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question525 struct {
para525
ans525
}
// para 是参数
// one 代表第一个参数
type para525 struct {
nums []int
}
// ans 是答案
// one 代表第一个答案
type ans525 struct {
one int
}
func Test_Problem525(t *testing.T) {
qs := []question525{
{
para525{[]int{0, 1}},
ans525{2},
},
{
para525{[]int{0, 1, 0}},
ans525{2},
},
}
fmt.Printf("------------------------Leetcode Problem 525------------------------\n")
for _, q := range qs {
_, p := q.ans525, q.para525
fmt.Printf("【input】:%v 【output】:%v\n", p, findMaxLength(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/0134.Gas-Station/Gas.go | leetcode/0134.Gas-Station/Gas.go | package leetcode
func canCompleteCircuit(gas []int, cost []int) int {
totalGas := 0
totalCost := 0
currGas := 0
start := 0
for i := 0; i < len(gas); i++ {
totalGas += gas[i]
totalCost += cost[i]
currGas += gas[i] - cost[i]
if currGas < 0 {
start = i + 1
currGas = 0
}
}
if totalGas < totalCost {
return -1
}
return start
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0134.Gas-Station/Gas_test.go | leetcode/0134.Gas-Station/Gas_test.go | package leetcode
import (
"fmt"
"testing"
)
type question134 struct {
para134
ans134
}
// para 是参数
// one 代表第一个参数
type para134 struct {
one []int
two []int
}
// ans 是答案
// one 代表第一个答案
type ans134 struct {
one int
}
func Test_Problem134(t *testing.T) {
qs := []question134{
{
para134{[]int{1, 2, 3, 4, 5}, []int{3, 4, 5, 1, 2}},
ans134{3},
},
{
para134{[]int{2, 3, 4}, []int{3, 4, 3}},
ans134{-1},
},
}
fmt.Printf("------------------------Leetcode Problem 134------------------------\n")
for _, q := range qs {
_, p := q.ans134, q.para134
fmt.Printf("【input】:%v, %v 【output】:%v\n", p.one, p.two, canCompleteCircuit(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/0143.Reorder-List/143. Reorder List_test.go | leetcode/0143.Reorder-List/143. Reorder List_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question143 struct {
para143
ans143
}
// para 是参数
// one 代表第一个参数
type para143 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans143 struct {
one []int
}
func Test_Problem143(t *testing.T) {
qs := []question143{
{
para143{[]int{1, 2, 3, 4, 5}},
ans143{[]int{1, 5, 2, 4, 3}},
},
{
para143{[]int{1, 2, 3, 4}},
ans143{[]int{1, 4, 2, 3}},
},
{
para143{[]int{1}},
ans143{[]int{1}},
},
{
para143{[]int{}},
ans143{[]int{}},
},
}
fmt.Printf("------------------------Leetcode Problem 143------------------------\n")
for _, q := range qs {
_, p := q.ans143, q.para143
fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(reorderList(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/0143.Reorder-List/143. Reorder List.go | leetcode/0143.Reorder-List/143. Reorder 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 reorderList(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
}
// 反转链表后半部分 1->2->3->4->5->6 to 1->2->3->6->5->4
preMiddle := p1
preCurrent := p1.Next
for preCurrent.Next != nil {
current := preCurrent.Next
preCurrent.Next = current.Next
current.Next = preMiddle.Next
preMiddle.Next = current
}
// 重新拼接链表 1->2->3->6->5->4 to 1->6->2->5->3->4
p1 = head
p2 = preMiddle.Next
for p1 != preMiddle {
preMiddle.Next = p2.Next
p2.Next = p1.Next
p1.Next = p2
p1 = p2.Next
p2 = preMiddle.Next
}
return head
}
// 解法二 数组
func reorderList1(head *ListNode) *ListNode {
array := listToArray(head)
length := len(array)
if length == 0 {
return head
}
cur := head
last := head
for i := 0; i < len(array)/2; i++ {
tmp := &ListNode{Val: array[length-1-i], Next: cur.Next}
cur.Next = tmp
cur = tmp.Next
last = tmp
}
if length%2 == 0 {
last.Next = nil
} else {
cur.Next = nil
}
return head
}
func listToArray(head *ListNode) []int {
array := []int{}
if head == nil {
return array
}
cur := head
for cur != nil {
array = append(array, cur.Val)
cur = cur.Next
}
return array
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0220.Contains-Duplicate-III/220. Contains Duplicate III_test.go | leetcode/0220.Contains-Duplicate-III/220. Contains Duplicate III_test.go | package leetcode
import (
"fmt"
"testing"
)
type question220 struct {
para220
ans220
}
// para 是参数
// one 代表第一个参数
type para220 struct {
one []int
k int
t int
}
// ans 是答案
// one 代表第一个答案
type ans220 struct {
one bool
}
func Test_Problem220(t *testing.T) {
qs := []question220{
{
para220{[]int{7, 1, 3}, 2, 3},
ans220{true},
},
{
para220{[]int{-1, -1}, 1, -1},
ans220{false},
},
{
para220{[]int{1, 2, 3, 1}, 3, 0},
ans220{true},
},
{
para220{[]int{1, 0, 1, 1}, 1, 2},
ans220{true},
},
{
para220{[]int{1, 5, 9, 1, 5, 9}, 2, 3},
ans220{false},
},
{
para220{[]int{1, 2, 1, 1}, 1, 0},
ans220{true},
},
}
fmt.Printf("------------------------Leetcode Problem 220------------------------\n")
for _, q := range qs {
_, p := q.ans220, q.para220
fmt.Printf("【input】:%v 【output】:%v\n", p, containsNearbyAlmostDuplicate(p.one, p.k, p.t))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0220.Contains-Duplicate-III/220. Contains Duplicate III.go | leetcode/0220.Contains-Duplicate-III/220. Contains Duplicate III.go | package leetcode
// 解法一 桶排序
func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool {
if k <= 0 || t < 0 || len(nums) < 2 {
return false
}
buckets := map[int]int{}
for i := 0; i < len(nums); i++ {
// Get the ID of the bucket from element value nums[i] and bucket width t + 1
key := nums[i] / (t + 1)
// -7/9 = 0, but need -7/9 = -1
if nums[i] < 0 {
key--
}
if _, ok := buckets[key]; ok {
return true
}
// check the lower bucket, and have to check the value too
if v, ok := buckets[key-1]; ok && nums[i]-v <= t {
return true
}
// check the upper bucket, and have to check the value too
if v, ok := buckets[key+1]; ok && v-nums[i] <= t {
return true
}
// maintain k size of window
if len(buckets) >= k {
delete(buckets, nums[i-k]/(t+1))
}
buckets[key] = nums[i]
}
return false
}
// 解法二 滑动窗口 + 剪枝
func containsNearbyAlmostDuplicate1(nums []int, k int, t int) bool {
if len(nums) <= 1 {
return false
}
if k <= 0 {
return false
}
n := len(nums)
for i := 0; i < n; i++ {
count := 0
for j := i + 1; j < n && count < k; j++ {
if abs(nums[i]-nums[j]) <= t {
return true
}
count++
}
}
return false
}
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/2164.Sort-Even-and-Odd-Indices-Independently/2164. Sort Even and Odd Indices Independently_test.go | leetcode/2164.Sort-Even-and-Odd-Indices-Independently/2164. Sort Even and Odd Indices Independently_test.go | package leetcode
import (
"fmt"
"testing"
)
type question2164 struct {
para2164
ans2164
}
// para 是参数
// one 代表第一个参数
type para2164 struct {
nums []int
}
// ans 是答案
// one 代表第一个答案
type ans2164 struct {
one []int
}
func Test_Problem1(t *testing.T) {
qs := []question2164{
{
para2164{[]int{4, 1, 2, 3}},
ans2164{[]int{2, 3, 4, 1}},
},
{
para2164{[]int{2, 1}},
ans2164{[]int{2, 1}},
},
}
fmt.Printf("------------------------Leetcode Problem 2164------------------------\n")
for _, q := range qs {
_, p := q.ans2164, q.para2164
fmt.Printf("【input】:%v 【output】:%v\n", p, sortEvenOdd(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/2164.Sort-Even-and-Odd-Indices-Independently/2164. Sort Even and Odd Indices Independently.go | leetcode/2164.Sort-Even-and-Odd-Indices-Independently/2164. Sort Even and Odd Indices Independently.go | package leetcode
import (
"sort"
)
func sortEvenOdd(nums []int) []int {
odd, even, res := []int{}, []int{}, []int{}
for index, v := range nums {
if index%2 == 0 {
even = append(even, v)
} else {
odd = append(odd, v)
}
}
sort.Ints(even)
sort.Sort(sort.Reverse(sort.IntSlice(odd)))
indexO, indexE := 0, 0
for i := 0; i < len(nums); i++ {
if i%2 == 0 {
res = append(res, even[indexE])
indexE++
} else {
res = append(res, odd[indexO])
indexO++
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0088.Merge-Sorted-Array/88. Merge Sorted Array.go | leetcode/0088.Merge-Sorted-Array/88. Merge Sorted Array.go | package leetcode
func merge(nums1 []int, m int, nums2 []int, n int) {
for p := m + n; m > 0 && n > 0; p-- {
if nums1[m-1] <= nums2[n-1] {
nums1[p-1] = nums2[n-1]
n--
} else {
nums1[p-1] = nums1[m-1]
m--
}
}
for ; n > 0; n-- {
nums1[n-1] = nums2[n-1]
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0088.Merge-Sorted-Array/88. Merge Sorted Array_test.go | leetcode/0088.Merge-Sorted-Array/88. Merge Sorted Array_test.go | package leetcode
import (
"fmt"
"testing"
)
type question88 struct {
para88
ans88
}
// para 是参数
// one 代表第一个参数
type para88 struct {
one []int
m int
two []int
n int
}
// ans 是答案
// one 代表第一个答案
type ans88 struct {
one []int
}
func Test_Problem88(t *testing.T) {
qs := []question88{
{
para88{[]int{0}, 0, []int{1}, 1},
ans88{[]int{1}},
},
{
para88{[]int{1, 2, 3, 0, 0, 0}, 3, []int{2, 5, 6}, 3},
ans88{[]int{1, 2, 2, 3, 5, 6}},
},
}
fmt.Printf("------------------------Leetcode Problem 88------------------------\n")
for _, q := range qs {
_, p := q.ans88, q.para88
fmt.Printf("【intput】:%v,%v,%v,%v ", p.one, p.m, p.two, p.n)
merge(p.one, p.m, p.two, p.n)
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/1296.Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.Divide Array in Sets of K Consecutive Numbers_test.go | leetcode/1296.Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.Divide Array in Sets of K Consecutive Numbers_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1296 struct {
para1296
ans1296
}
// para 是参数
type para1296 struct {
nums []int
k int
}
// ans 是答案
type ans1296 struct {
ans bool
}
func Test_Problem1296(t *testing.T) {
qs := []question1296{
{
para1296{[]int{1, 2, 3, 3, 4, 4, 5, 6}, 4},
ans1296{true},
},
{
para1296{[]int{3, 2, 1, 2, 3, 4, 3, 4, 5, 9, 10, 11}, 3},
ans1296{true},
},
{
para1296{[]int{1, 2, 3, 4}, 3},
ans1296{false},
},
}
fmt.Printf("------------------------Leetcode Problem 1296------------------------\n")
for _, q := range qs {
_, p := q.ans1296, q.para1296
fmt.Printf("【input】:%v 【output】:%v\n", p, isPossibleDivide(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/1296.Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.Divide Array in Sets of K Consecutive Numbers.go | leetcode/1296.Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.Divide Array in Sets of K Consecutive Numbers.go | package leetcode
import "sort"
func isPossibleDivide(nums []int, k int) bool {
mp := make(map[int]int)
for _, v := range nums {
mp[v] += 1
}
sort.Ints(nums)
for _, num := range nums {
if mp[num] == 0 {
continue
}
for diff := 0; diff < k; diff++ {
if mp[num+diff] == 0 {
return false
}
mp[num+diff] -= 1
}
}
return true
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1748.Sum-of-Unique-Elements/1748. Sum of Unique Elements.go | leetcode/1748.Sum-of-Unique-Elements/1748. Sum of Unique Elements.go | package leetcode
func sumOfUnique(nums []int) int {
freq, res := make(map[int]int), 0
for _, v := range nums {
if _, ok := freq[v]; !ok {
freq[v] = 0
}
freq[v]++
}
for k, v := range freq {
if v == 1 {
res += k
}
}
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1748.Sum-of-Unique-Elements/1748. Sum of Unique Elements_test.go | leetcode/1748.Sum-of-Unique-Elements/1748. Sum of Unique Elements_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1748 struct {
para1748
ans1748
}
// para 是参数
// one 代表第一个参数
type para1748 struct {
nums []int
}
// ans 是答案
// one 代表第一个答案
type ans1748 struct {
one int
}
func Test_Problem1748(t *testing.T) {
qs := []question1748{
{
para1748{[]int{1, 2, 3, 2}},
ans1748{4},
},
{
para1748{[]int{1, 1, 1, 1, 1}},
ans1748{0},
},
{
para1748{[]int{1, 2, 3, 4, 5}},
ans1748{15},
},
}
fmt.Printf("------------------------Leetcode Problem 1748------------------------\n")
for _, q := range qs {
_, p := q.ans1748, q.para1748
fmt.Printf("【input】:%v 【output】:%v\n", p, sumOfUnique(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/0040.Combination-Sum-II/40. Combination Sum II_test.go | leetcode/0040.Combination-Sum-II/40. Combination Sum II_test.go | package leetcode
import (
"fmt"
"testing"
)
type question40 struct {
para40
ans40
}
// para 是参数
// one 代表第一个参数
type para40 struct {
n []int
k int
}
// ans 是答案
// one 代表第一个答案
type ans40 struct {
one [][]int
}
func Test_Problem40(t *testing.T) {
qs := []question40{
{
para40{[]int{10, 1, 2, 7, 6, 1, 5}, 8},
ans40{[][]int{{1, 7}, {1, 2, 5}, {2, 6}, {1, 1, 6}}},
},
{
para40{[]int{2, 5, 2, 1, 2}, 5},
ans40{[][]int{{1, 2, 2}, {5}}},
},
}
fmt.Printf("------------------------Leetcode Problem 40------------------------\n")
for _, q := range qs {
_, p := q.ans40, q.para40
fmt.Printf("【input】:%v 【output】:%v\n", p, combinationSum2(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/0040.Combination-Sum-II/40. Combination Sum II.go | leetcode/0040.Combination-Sum-II/40. Combination Sum II.go | package leetcode
import (
"sort"
)
func combinationSum2(candidates []int, target int) [][]int {
if len(candidates) == 0 {
return [][]int{}
}
c, res := []int{}, [][]int{}
sort.Ints(candidates) // 这里是去重的关键逻辑
findcombinationSum2(candidates, target, 0, c, &res)
return res
}
func findcombinationSum2(nums []int, target, index int, c []int, res *[][]int) {
if target == 0 {
b := make([]int, len(c))
copy(b, c)
*res = append(*res, b)
return
}
for i := index; i < len(nums); i++ {
if i > index && nums[i] == nums[i-1] { // 这里是去重的关键逻辑,本次不取重复数字,下次循环可能会取重复数字
continue
}
if target >= nums[i] {
c = append(c, nums[i])
findcombinationSum2(nums, target-nums[i], i+1, c, res)
c = c[:len(c)-1]
}
}
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0429.N-ary-Tree-Level-Order-Traversal/429. N-ary Tree Level Order Traversal.go | leetcode/0429.N-ary-Tree-Level-Order-Traversal/429. N-ary Tree Level Order Traversal.go | package leetcode
/**
* Definition for a Node.
* type Node struct {
* Val int
* Children []*Node
* }
*/
type Node struct {
Val int
Children []*Node
}
func levelOrder(root *Node) [][]int {
var res [][]int
var temp []int
if root == nil {
return res
}
queue := []*Node{root, nil}
for len(queue) > 1 {
node := queue[0]
queue = queue[1:]
if node == nil {
queue = append(queue, nil)
res = append(res, temp)
temp = []int{}
} else {
temp = append(temp, node.Val)
if len(node.Children) > 0 {
queue = append(queue, node.Children...)
}
}
}
res = append(res, temp)
return res
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0429.N-ary-Tree-Level-Order-Traversal/429. N-ary Tree Level Order Traversal_test.go | leetcode/0429.N-ary-Tree-Level-Order-Traversal/429. N-ary Tree Level Order Traversal_test.go | package leetcode
import (
"fmt"
"testing"
)
func Test_Problem429(t *testing.T) {
fmt.Printf("success\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0513.Find-Bottom-Left-Tree-Value/513. Find Bottom Left Tree Value_test.go | leetcode/0513.Find-Bottom-Left-Tree-Value/513. Find Bottom Left Tree Value_test.go | package leetcode
import (
"fmt"
"testing"
"github.com/halfrost/LeetCode-Go/structures"
)
type question513 struct {
para513
ans513
}
// para 是参数
// one 代表第一个参数
type para513 struct {
one []int
}
// ans 是答案
// one 代表第一个答案
type ans513 struct {
one int
}
func Test_Problem513(t *testing.T) {
qs := []question513{
{
para513{[]int{}},
ans513{0},
},
{
para513{[]int{1}},
ans513{1},
},
{
para513{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans513{15},
},
{
para513{[]int{1, 2, 3, 4, structures.NULL, structures.NULL, 5}},
ans513{4},
},
}
fmt.Printf("------------------------Leetcode Problem 513------------------------\n")
for _, q := range qs {
_, p := q.ans513, q.para513
fmt.Printf("【input】:%v ", p)
root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", findBottomLeftValue(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/0513.Find-Bottom-Left-Tree-Value/513. Find Bottom Left Tree Value.go | leetcode/0513.Find-Bottom-Left-Tree-Value/513. Find Bottom Left Tree Value.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
* }
*/
// 解法一 DFS
func findBottomLeftValue(root *TreeNode) int {
if root == nil {
return 0
}
res, maxHeight := 0, -1
findBottomLeftValueDFS(root, 0, &res, &maxHeight)
return res
}
func findBottomLeftValueDFS(root *TreeNode, curHeight int, res, maxHeight *int) {
if curHeight > *maxHeight && root.Left == nil && root.Right == nil {
*maxHeight = curHeight
*res = root.Val
}
if root.Left != nil {
findBottomLeftValueDFS(root.Left, curHeight+1, res, maxHeight)
}
if root.Right != nil {
findBottomLeftValueDFS(root.Right, curHeight+1, res, maxHeight)
}
}
// 解法二 BFS
func findBottomLeftValue1(root *TreeNode) int {
queue := []*TreeNode{root}
for len(queue) > 0 {
next := []*TreeNode{}
for _, node := range queue {
if node.Left != nil {
next = append(next, node.Left)
}
if node.Right != nil {
next = append(next, node.Right)
}
}
if len(next) == 0 {
return queue[0].Val
}
queue = next
}
return 0
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0991.Broken-Calculator/991. Broken Calculator.go | leetcode/0991.Broken-Calculator/991. Broken Calculator.go | package leetcode
func brokenCalc(X int, Y int) int {
res := 0
for Y > X {
res++
if Y&1 == 1 {
Y++
} else {
Y /= 2
}
}
return res + X - Y
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0991.Broken-Calculator/991. Broken Calculator_test.go | leetcode/0991.Broken-Calculator/991. Broken Calculator_test.go | package leetcode
import (
"fmt"
"testing"
)
type question991 struct {
para991
ans991
}
// para 是参数
// one 代表第一个参数
type para991 struct {
X int
Y int
}
// ans 是答案
// one 代表第一个答案
type ans991 struct {
one int
}
func Test_Problem991(t *testing.T) {
qs := []question991{
{
para991{2, 3},
ans991{2},
},
{
para991{5, 8},
ans991{2},
},
{
para991{3, 10},
ans991{3},
},
{
para991{1024, 1},
ans991{1023},
},
}
fmt.Printf("------------------------Leetcode Problem 991------------------------\n")
for _, q := range qs {
_, p := q.ans991, q.para991
fmt.Printf("【input】:%v 【output】:%v\n", p, brokenCalc(p.X, p.Y))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1649.Create-Sorted-Array-through-Instructions/1649. Create Sorted Array through Instructions_test.go | leetcode/1649.Create-Sorted-Array-through-Instructions/1649. Create Sorted Array through Instructions_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1649 struct {
para1649
ans1649
}
// para 是参数
// one 代表第一个参数
type para1649 struct {
instructions []int
}
// ans 是答案
// one 代表第一个答案
type ans1649 struct {
one int
}
func Test_Problem1649(t *testing.T) {
qs := []question1649{
{
para1649{[]int{1, 5, 6, 2}},
ans1649{1},
},
{
para1649{[]int{1, 2, 3, 6, 5, 4}},
ans1649{3},
},
{
para1649{[]int{1, 3, 3, 3, 2, 4, 2, 1, 2}},
ans1649{4},
},
}
fmt.Printf("------------------------Leetcode Problem 1649------------------------\n")
for _, q := range qs {
_, p := q.ans1649, q.para1649
fmt.Printf("【input】:%v 【output】:%v \n", p, createSortedArray(p.instructions))
}
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/1649.Create-Sorted-Array-through-Instructions/1649. Create Sorted Array through Instructions.go | leetcode/1649.Create-Sorted-Array-through-Instructions/1649. Create Sorted Array through Instructions.go | package leetcode
import (
"sort"
"github.com/halfrost/LeetCode-Go/template"
)
// 解法一 树状数组 Binary Indexed Tree
func createSortedArray(instructions []int) int {
bit, res := template.BinaryIndexedTree{}, 0
bit.Init(100001)
for i, v := range instructions {
less := bit.Query(v - 1)
greater := i - bit.Query(v)
res = (res + min(less, greater)) % (1e9 + 7)
bit.Add(v, 1)
}
return res
}
// 解法二 线段树 SegmentTree
func createSortedArray1(instructions []int) int {
if len(instructions) == 0 {
return 0
}
st, res, mod := template.SegmentCountTree{}, 0, 1000000007
numsMap, numsArray, tmpArray := discretization1649(instructions)
// 初始化线段树,节点内的值都赋值为 0,即计数为 0
st.Init(tmpArray, func(i, j int) int {
return 0
})
for i := 0; i < len(instructions); i++ {
strictlyLessThan := st.Query(0, numsMap[instructions[i]]-1)
strictlyGreaterThan := st.Query(numsMap[instructions[i]]+1, numsArray[len(numsArray)-1])
res = (res + min(strictlyLessThan, strictlyGreaterThan)) % mod
st.UpdateCount(numsMap[instructions[i]])
}
return res
}
func discretization1649(instructions []int) (map[int]int, []int, []int) {
tmpArray, numsArray, numsMap := []int{}, []int{}, map[int]int{}
for i := 0; i < len(instructions); i++ {
numsMap[instructions[i]] = instructions[i]
}
for _, v := range numsMap {
numsArray = append(numsArray, v)
}
sort.Ints(numsArray)
for i, num := range numsArray {
numsMap[num] = i
}
for i := range numsArray {
tmpArray = append(tmpArray, i)
}
return numsMap, numsArray, tmpArray
}
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/0329.Longest-Increasing-Path-in-a-Matrix/329. Longest Increasing Path in a Matrix.go | leetcode/0329.Longest-Increasing-Path-in-a-Matrix/329. Longest Increasing Path in a Matrix.go | package leetcode
import (
"math"
)
var dir = [][]int{
{-1, 0},
{0, 1},
{1, 0},
{0, -1},
}
func longestIncreasingPath(matrix [][]int) int {
cache, res := make([][]int, len(matrix)), 0
for i := 0; i < len(cache); i++ {
cache[i] = make([]int, len(matrix[0]))
}
for i, v := range matrix {
for j := range v {
searchPath(matrix, cache, math.MinInt64, i, j)
res = max(res, cache[i][j])
}
}
return res
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func isInIntBoard(board [][]int, x, y int) bool {
return x >= 0 && x < len(board) && y >= 0 && y < len(board[0])
}
func searchPath(board, cache [][]int, lastNum, x, y int) int {
if board[x][y] <= lastNum {
return 0
}
if cache[x][y] > 0 {
return cache[x][y]
}
count := 1
for i := 0; i < 4; i++ {
nx := x + dir[i][0]
ny := y + dir[i][1]
if isInIntBoard(board, nx, ny) {
count = max(count, searchPath(board, cache, board[x][y], nx, ny)+1)
}
}
cache[x][y] = 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/0329.Longest-Increasing-Path-in-a-Matrix/329. Longest Increasing Path in a Matrix_test.go | leetcode/0329.Longest-Increasing-Path-in-a-Matrix/329. Longest Increasing Path in a Matrix_test.go | package leetcode
import (
"fmt"
"testing"
)
type question329 struct {
para329
ans329
}
// para 是参数
// one 代表第一个参数
type para329 struct {
matrix [][]int
}
// ans 是答案
// one 代表第一个答案
type ans329 struct {
one int
}
func Test_Problem329(t *testing.T) {
qs := []question329{
{
para329{[][]int{{1}}},
ans329{1},
},
{
para329{[][]int{{}}},
ans329{0},
},
{
para329{[][]int{{9, 9, 4}, {6, 6, 8}, {2, 1, 1}}},
ans329{4},
},
{
para329{[][]int{{3, 4, 5}, {3, 2, 6}, {2, 2, 1}}},
ans329{4},
},
{
para329{[][]int{{1, 5, 9}, {10, 11, 13}, {12, 13, 15}}},
ans329{5},
},
{
para329{[][]int{{1, 5, 7}, {11, 12, 13}, {12, 13, 15}}},
ans329{5},
},
}
fmt.Printf("------------------------Leetcode Problem 329------------------------\n")
for _, q := range qs {
_, p := q.ans329, q.para329
fmt.Printf("【input】:%v 【output】:%v\n", p, longestIncreasingPath(p.matrix))
}
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/1641.Count-Sorted-Vowel-Strings/1641. Count Sorted Vowel Strings.go | leetcode/1641.Count-Sorted-Vowel-Strings/1641. Count Sorted Vowel Strings.go | package leetcode
// 解法一 打表
func countVowelStrings(n int) int {
res := []int{1, 5, 15, 35, 70, 126, 210, 330, 495, 715, 1001, 1365, 1820, 2380, 3060, 3876, 4845, 5985, 7315, 8855, 10626, 12650, 14950, 17550, 20475, 23751, 27405, 31465, 35960, 40920, 46376, 52360, 58905, 66045, 73815, 82251, 91390, 101270, 111930, 123410, 135751, 148995, 163185, 178365, 194580, 211876, 230300, 249900, 270725, 292825, 316251}
return res[n]
}
func makeTable() []int {
res, array := 0, []int{}
for i := 0; i < 51; i++ {
countVowelStringsDFS(i, 0, []string{}, []string{"a", "e", "i", "o", "u"}, &res)
array = append(array, res)
res = 0
}
return array
}
func countVowelStringsDFS(n, index int, cur []string, vowels []string, res *int) {
vowels = vowels[index:]
if len(cur) == n {
(*res)++
return
}
for i := 0; i < len(vowels); i++ {
cur = append(cur, vowels[i])
countVowelStringsDFS(n, i, cur, vowels, res)
cur = cur[:len(cur)-1]
}
}
// 解法二 数学方法 —— 组合
func countVowelStrings1(n int) int {
return (n + 1) * (n + 2) * (n + 3) * (n + 4) / 24
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1641.Count-Sorted-Vowel-Strings/1641. Count Sorted Vowel Strings_test.go | leetcode/1641.Count-Sorted-Vowel-Strings/1641. Count Sorted Vowel Strings_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1641 struct {
para1641
ans1641
}
// para 是参数
// one 代表第一个参数
type para1641 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans1641 struct {
one int
}
func Test_Problem1641(t *testing.T) {
qs := []question1641{
{
para1641{1},
ans1641{5},
},
{
para1641{2},
ans1641{15},
},
{
para1641{33},
ans1641{66045},
},
}
fmt.Printf("------------------------Leetcode Problem 1641------------------------\n")
for _, q := range qs {
_, p := q.ans1641, q.para1641
fmt.Printf("【input】:%v 【output】:%v \n", p, countVowelStrings(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/1573.Number-of-Ways-to-Split-a-String/1573. Number of Ways to Split a String_test.go | leetcode/1573.Number-of-Ways-to-Split-a-String/1573. Number of Ways to Split a String_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1573 struct {
para1573
ans1573
}
// para 是参数
// one 代表第一个参数
type para1573 struct {
s string
}
// ans 是答案
// one 代表第一个答案
type ans1573 struct {
one int
}
func Test_Problem1573(t *testing.T) {
qs := []question1573{
{
para1573{"10101"},
ans1573{4},
},
{
para1573{"1001"},
ans1573{0},
},
{
para1573{"0000"},
ans1573{3},
},
{
para1573{"100100010100110"},
ans1573{12},
},
}
fmt.Printf("------------------------Leetcode Problem 1573------------------------\n")
for _, q := range qs {
_, p := q.ans1573, q.para1573
fmt.Printf("【input】:%v 【output】:%v \n", p, numWays(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/1573.Number-of-Ways-to-Split-a-String/1573. Number of Ways to Split a String.go | leetcode/1573.Number-of-Ways-to-Split-a-String/1573. Number of Ways to Split a String.go | package leetcode
func numWays(s string) int {
ones := 0
for _, c := range s {
if c == '1' {
ones++
}
}
if ones%3 != 0 {
return 0
}
if ones == 0 {
return (len(s) - 1) * (len(s) - 2) / 2 % 1000000007
}
N, a, b, c, d, count := ones/3, 0, 0, 0, 0, 0
for i, letter := range s {
if letter == '0' {
continue
}
if letter == '1' {
count++
}
if count == N {
a = i
}
if count == N+1 {
b = i
}
if count == 2*N {
c = i
}
if count == 2*N+1 {
d = i
}
}
return (b - a) * (d - c) % 1000000007
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/0326.Power-of-Three/326. Power of Three_test.go | leetcode/0326.Power-of-Three/326. Power of Three_test.go | package leetcode
import (
"fmt"
"testing"
)
type question326 struct {
para326
ans326
}
// para 是参数
// one 代表第一个参数
type para326 struct {
one int
}
// ans 是答案
// one 代表第一个答案
type ans326 struct {
one bool
}
func Test_Problem326(t *testing.T) {
qs := []question326{
{
para326{27},
ans326{true},
},
{
para326{0},
ans326{false},
},
{
para326{9},
ans326{true},
},
{
para326{45},
ans326{false},
},
}
fmt.Printf("------------------------Leetcode Problem 326------------------------\n")
for _, q := range qs {
_, p := q.ans326, q.para326
fmt.Printf("【input】:%v 【output】:%v\n", p, isPowerOfThree(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/0326.Power-of-Three/326. Power of Three.go | leetcode/0326.Power-of-Three/326. Power of Three.go | package leetcode
// 解法一 数论
func isPowerOfThree(n int) bool {
// 1162261467 is 3^19, 3^20 is bigger than int
return n > 0 && (1162261467%n == 0)
}
// 解法二 打表法
func isPowerOfThree1(n int) bool {
// 1162261467 is 3^19, 3^20 is bigger than int
allPowerOfThreeMap := map[int]int{1: 1, 3: 3, 9: 9, 27: 27, 81: 81, 243: 243, 729: 729, 2187: 2187, 6561: 6561, 19683: 19683, 59049: 59049, 177147: 177147, 531441: 531441, 1594323: 1594323, 4782969: 4782969, 14348907: 14348907, 43046721: 43046721, 129140163: 129140163, 387420489: 387420489, 1162261467: 1162261467}
_, ok := allPowerOfThreeMap[n]
return ok
}
// 解法三 循环
func isPowerOfThree2(num int) bool {
for num >= 3 {
if num%3 == 0 {
num = num / 3
} else {
return false
}
}
return num == 1
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1694.Reformat-Phone-Number/1694. Reformat Phone Number.go | leetcode/1694.Reformat-Phone-Number/1694. Reformat Phone Number.go | package leetcode
import (
"strings"
)
func reformatNumber(number string) string {
parts, nums := []string{}, []rune{}
for _, r := range number {
if r != '-' && r != ' ' {
nums = append(nums, r)
}
}
threeDigits, twoDigits := len(nums)/3, 0
switch len(nums) % 3 {
case 1:
threeDigits--
twoDigits = 2
case 2:
twoDigits = 1
default:
twoDigits = 0
}
for i := 0; i < threeDigits; i++ {
s := ""
s += string(nums[0:3])
nums = nums[3:]
parts = append(parts, s)
}
for i := 0; i < twoDigits; i++ {
s := ""
s += string(nums[0:2])
nums = nums[2:]
parts = append(parts, s)
}
return strings.Join(parts, "-")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
halfrost/LeetCode-Go | https://github.com/halfrost/LeetCode-Go/blob/d78a9e0927a302038992428433d2eb450efd93a2/leetcode/1694.Reformat-Phone-Number/1694. Reformat Phone Number_test.go | leetcode/1694.Reformat-Phone-Number/1694. Reformat Phone Number_test.go | package leetcode
import (
"fmt"
"testing"
)
type question1694 struct {
para1694
ans1694
}
// para 是参数
// one 代表第一个参数
type para1694 struct {
number string
}
// ans 是答案
// one 代表第一个答案
type ans1694 struct {
one string
}
func Test_Problem1694(t *testing.T) {
qs := []question1694{
{
para1694{"1-23-45 6"},
ans1694{"123-456"},
},
{
para1694{"123 4-567"},
ans1694{"123-45-67"},
},
{
para1694{"123 4-5678"},
ans1694{"123-456-78"},
},
{
para1694{"12"},
ans1694{"12"},
},
{
para1694{"--17-5 229 35-39475 "},
ans1694{"175-229-353-94-75"},
},
{
para1694{"9964-"},
ans1694{"99-64"},
},
}
fmt.Printf("------------------------Leetcode Problem 1694------------------------\n")
for _, q := range qs {
_, p := q.ans1694, q.para1694
fmt.Printf("【input】:%v 【output】:%v\n", p, reformatNumber(p.number))
}
fmt.Printf("\n\n\n")
}
| go | MIT | d78a9e0927a302038992428433d2eb450efd93a2 | 2026-01-07T08:36:06.754118Z | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.