u
This commit is contained in:
@@ -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}))
|
||||||
|
}
|
||||||
@@ -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"))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user