前言
近期更新的比较频
正文
问题来源
本问题来自leetcode上的面试题08.08题。
问题描述
有重复字符串的排列组合。编写一种方法,计算某字符串的所有排列组合。
示例 1:
输入:S = "qqe"
输出:["eqq","qeq","qqe"]
示例 1:
输入:S = "ab"
输出:["ab", "ba"]
分析:
0706做了相同的题目,然后秒了
func permutation(S string) []string {
res := make([]string, 0)
b := []byte(S)
dfs(0, b, &res)
return res
}
func dfs(i int, b []byte, res *[]string) {
if i == len(b) {
*res = append(*res, string(b))
}
for t := i; t < len(b); t++ {
if canSwap(i, t, b) {
b[t], b[i] = b[i], b[t]
dfs(i+1, b, res)
b[t], b[i] = b[i], b[t]
}
}
}
func canSwap(i, j int, b []byte) bool {
for t := i; t < j; t++ {
if b[t] == b[j] {
return false
}
}
return true
}
总结:
勤思考。
结语
不管怎么样好好加油。