求助:这段代码存在什么问题?
字符串的全排列,通过0%,求解
func permutation(s string) []string {
// write code here
str := []byte(s)
n := len(str)
used := make([]bool, n)
ans := make([]string, 0)
help := make(map[string]bool)
var dfs func(str []byte, path []byte)
dfs = func(str []byte, path []byte) {
if len(path) == n {
tmp := make([]byte, len(path))
copy(tmp, path)
if _, ok := help[string(tmp)]; ok {
return
}
ans = append(ans, string(tmp))
help[string(tmp)] = true
return
}
for i := 0; i < n; i++ {
if !used[i] {
path = append(path, str[i])
used[i] = true
dfs(str, path)
used[i] = false
path = path[:len(path)-1]
}
}
}
path := []byte{}
dfs(str, path)
return ans
}
func permutation(s string) []string {
// write code here
str := []byte(s)
n := len(str)
used := make([]bool, n)
ans := make([]string, 0)
help := make(map[string]bool)
var dfs func(str []byte, path []byte)
dfs = func(str []byte, path []byte) {
if len(path) == n {
tmp := make([]byte, len(path))
copy(tmp, path)
if _, ok := help[string(tmp)]; ok {
return
}
ans = append(ans, string(tmp))
help[string(tmp)] = true
return
}
for i := 0; i < n; i++ {
if !used[i] {
path = append(path, str[i])
used[i] = true
dfs(str, path)
used[i] = false
path = path[:len(path)-1]
}
}
}
path := []byte{}
dfs(str, path)
return ans
}
全部评论
题目啥啥要求?
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
04-25 16:03
安阳学院 后端工程师 代码飞升AL:同学院本建议你换一个项目 就算你不去特意搜也应该知道点评不能写吧 保持投递不要停 然后快速弄一个项目换上去 公司就别挑了 我第一段120一天 快速跳就行
点赞 评论 收藏
分享
