题解 | #字符串的排列#
字符串的排列
https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
package main
import (
	
	"sort"
	"strings"
)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param str string字符串
 * @return string字符串一维数组
 */
func Permutation( str string ) []string {
    // write code here
    var ans []string
    // 排序成字典序
    strs := strings.Split(str, "")
    sort.Strings(strs)
    sortrdStr := strings.Join(strs, "")
    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("", sortrdStr)
    return ans
}

 查看1道真题和解析
查看1道真题和解析