题解 | #字符串的排列#
字符串的排列
https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
package main
import "sort"
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串
* @return string字符串一维数组
*/
func Permutation( str string ) []string {
// write code here
var ans []string
// 排序成字典序
t:=[]byte(str)
sort.Slice(t,func(i,j int)bool{
return t[i]<t[j]
})
var dfs func(curr string, store string)
dfs = func(curr string, store string) {
// 1. 是否满足条件,记录结果
// 2. 终止条件
if len(store) == 0 {
ans = append(ans, curr)
return
}
// 3. 继续搜索
for i := 0; i < len(store); i++ {
// 重复跳过
if i > 0 && store[i] == store[i-1] {
continue
}
dfs(curr + string(store[i]), store[:i] + store[i+1:])
}
}
dfs("", string(t))
return ans
}


查看7道真题和解析