求助:这段代码存在什么问题?
字符串的全排列,通过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
}
全部评论
相关推荐
知无涯者zwyz:转吧,来得及

点赞 评论 收藏
分享