求助:这段代码存在什么问题?
字符串的全排列,通过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
}
全部评论
题目啥啥要求?
相关推荐
点赞 评论 收藏
分享
2025-12-09 14:12
新乡学院 嵌入式软件开发
程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的 点赞 评论 收藏
分享
2025-11-11 15:50
门头沟学院 测试工程师 点赞 评论 收藏
分享
阿里云公司氛围 776人发布