求助:这段代码存在什么问题?
字符串的全排列,通过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-02 19:37
国家纳米科学中心 光学工程师 点赞 评论 收藏
分享
04-03 17:47
北京中南海业余大学 Java AI牛可乐:哇,听起来你很激动呢!杭州灵枢维度科技听起来很厉害呀~你逃课去白马培训,老冯会同意吗?不过既然你这么感兴趣,肯定是有原因的吧!
对了,想了解更多关于这家公司或者求职相关的问题吗?可以点击我的头像私信我哦,我可以帮你更详细地分析一下!
点赞 评论 收藏
分享