Skip to content

Commit d03376b

Browse files
committed
commit offer-38
1 parent 7aa0160 commit d03376b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ LeetCode
33

44
### LeetCode Algorithm
55

6-
76
| # | Title | Solution | Difficulty | Tag |
87
|---| ----- | -------- | ---------- | ---------- |
98
|5|[最长回文子串](https://leetcode-cn.com/problems/longest-palindromic-substring/)|[Go](./5-longestPalindrome/5-longestPalindrome.go)|中等|动态规划|
@@ -50,6 +49,12 @@ LeetCode
5049
|680|[验证回文字符串 Ⅱ](https://leetcode-cn.com/problems/valid-palindrome-ii/)|[Go](./680-validPalindrome/680-validPalindrome.go)|简单||
5150
|1332|[删除回文子序列](https://leetcode-cn.com/problems/remove-palindromic-subsequences/)|[Go](./1332-removePalindromeSub/1332-removePalindromeSub.go)|简单||
5251

52+
### 剑指 Offer
53+
54+
| # | Title | Solution | Difficulty | Tag |
55+
|---| ----- | -------- | ---------- | ---------- |
56+
|38|[字符串的排列](https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/)|[Go](./offer/38/38-permutation.go)|中等|递归|
57+
5358
### SQL
5459

5560
| # | Title | Solution | Difficulty | Tag |

offer/38/38-permutation.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/
2+
package main
3+
4+
import "fmt"
5+
6+
func permutation(s string) []string {
7+
l := len(s)
8+
if l == 1 {
9+
return []string{s}
10+
}
11+
12+
var ret []string
13+
m := make(map[byte]int) // 字符去重
14+
for i := 0; i < l; i++ {
15+
if _, ok := m[s[i]]; ok {
16+
continue
17+
}
18+
ss := permutation(s[0:i] + s[i+1:]) // 递归
19+
for _, v := range ss {
20+
ret = append(ret, string(s[i])+v)
21+
}
22+
m[s[i]] = 1
23+
}
24+
return ret
25+
}
26+
27+
func main() {
28+
s := []string{"a", "ab", "abc", "abcd", "aab"}
29+
for _, v := range s {
30+
fmt.Println("ret:", permutation(v))
31+
}
32+
}

0 commit comments

Comments
 (0)