This commit is contained in:
2026-07-02 16:37:27 +08:00
parent 8f176c1836
commit 4850991edd
2 changed files with 78 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
package top100liked
import (
"fmt"
"testing"
)
// https://leetcode.cn/problems/subsets/?envType=study-plan-v2&envId=top-100-liked
func subsets(nums []int) [][]int {
res := [][]int{{}}
var helper func(nums []int, length int, start int, r []int)
helper = func(nums []int, length int, start int, r []int) {
if len(r) == length {
cp := make([]int, length)
copy(cp, r)
res = append(res, cp)
return
}
for i := start; i < len(nums); i++ {
r = append(r, nums[i])
helper(nums, length, i+1, r)
r = r[:len(r)-1]
}
}
for i := range len(nums) {
helper(nums, i+1, 0, []int{})
}
return res
}
func Test(t *testing.T) {
fmt.Printf("%+v", subsets([]int{1, 2, 3}))
}
+44
View File
@@ -0,0 +1,44 @@
package top100liked
import (
"fmt"
"testing"
)
// https://leetcode.cn/problems/letter-combinations-of-a-phone-number?envType=study-plan-v2&envId=top-100-liked
var m = map[byte][]byte{
'2': []byte("abc"),
'3': []byte("def"),
'4': []byte("ghi"),
'5': []byte("jkl"),
'6': []byte("mno"),
'7': []byte("pqrs"),
'8': []byte("tuv"),
'9': []byte("wxyz"),
}
func letterCombinations(digits string) []string {
res := []string{}
var helper func(idx int, r []byte)
helper = func(idx int, r []byte) {
if idx == len(digits) {
res = append(res, string(r))
return
}
for j := range m[digits[idx]] {
r = append(r, m[digits[idx]][j])
helper(idx+1, r)
r = r[:len(r)-1]
}
}
helper(0, []byte{})
return res
}
func Test(t *testing.T) {
fmt.Printf("%+v", letterCombinations("23"))
fmt.Printf("%+v", letterCombinations("2"))
}