@@ -45,7 +45,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.07.Permutation%20I
4545
4646### 方法一:DFS(回溯)
4747
48- 我们设计一个函数 $dfs(i)$ 表示已经填完了前 $i$ 个位置,现在需要填第 $i+1$ 个位置。枚举所有可能的字符,如果这个字符没有被填过,就填入这个字符,然后继续填下一个位置,直到填完所有的位置。
48+ 我们设计一个函数 $\textit{ dfs} (i)$ 表示已经填完了前 $i$ 个位置,现在需要填第 $i+1$ 个位置。枚举所有可能的字符,如果这个字符没有被填过,就填入这个字符,然后继续填下一个位置,直到填完所有的位置。
4949
5050时间复杂度 $O(n \times n!)$,其中 $n$ 是字符串的长度。一共有 $n!$ 个排列,每个排列需要 $O(n)$ 的时间来构造。
5151
@@ -57,22 +57,20 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.07.Permutation%20I
5757class Solution :
5858 def permutation (self , S : str ) -> List[str ]:
5959 def dfs (i : int ):
60- if i = = n:
60+ if i > = n:
6161 ans.append(" " .join(t))
6262 return
6363 for j, c in enumerate (S):
64- if vis[j]:
65- continue
66- vis[j] = True
67- t.append(c)
68- dfs(i + 1 )
69- t.pop()
70- vis[j] = False
64+ if not vis[j]:
65+ vis[j] = True
66+ t[i] = c
67+ dfs(i + 1 )
68+ vis[j] = False
7169
70+ ans = []
7271 n = len (S)
7372 vis = [False ] * n
74- ans = []
75- t = []
73+ t = list (S)
7674 dfs(0 )
7775 return ans
7876```
@@ -82,30 +80,31 @@ class Solution:
8280``` java
8381class Solution {
8482 private char [] s;
85- private boolean [] vis = new boolean [' z' + 1 ];
83+ private char [] t;
84+ private boolean [] vis;
8685 private List<String > ans = new ArrayList<> ();
87- private StringBuilder t = new StringBuilder ();
8886
8987 public String [] permutation (String S ) {
9088 s = S . toCharArray();
89+ int n = s. length;
90+ vis = new boolean [n];
91+ t = new char [n];
9192 dfs(0 );
9293 return ans. toArray(new String [0 ]);
9394 }
9495
9596 private void dfs (int i ) {
96- if (i = = s. length) {
97- ans. add(t . toString( ));
97+ if (i > = s. length) {
98+ ans. add(new String (t ));
9899 return ;
99100 }
100- for (char c : s) {
101- if (vis[c]) {
102- continue ;
101+ for (int j = 0 ; j < s. length; ++ j) {
102+ if (! vis[j]) {
103+ vis[j] = true ;
104+ t[i] = s[j];
105+ dfs(i + 1 );
106+ vis[j] = false ;
103107 }
104- vis[c] = true ;
105- t. append(c);
106- dfs(i + 1 );
107- t. deleteCharAt(t. length() - 1 );
108- vis[c] = false ;
109108 }
110109 }
111110}
@@ -119,51 +118,49 @@ public:
119118 vector<string > permutation(string S) {
120119 int n = S.size();
121120 vector<bool > vis(n);
121+ string t = S;
122122 vector<string > ans;
123- string t;
124- function<void(int)> dfs = [ &] (int i) {
123+ auto dfs = [ &] (this auto&& dfs, int i) {
125124 if (i >= n) {
126- ans.push_back (t);
125+ ans.emplace_back (t);
127126 return;
128127 }
129128 for (int j = 0; j < n; ++j) {
130- if (vis[ j] ) {
131- continue;
129+ if (!vis[ j] ) {
130+ vis[ j] = true;
131+ t[ i] = S[ j] ;
132+ dfs(i + 1);
133+ vis[ j] = false;
132134 }
133- vis[ j] = true;
134- t.push_back(S[ j] );
135- dfs(i + 1);
136- t.pop_back();
137- vis[ j] = false;
138135 }
139136 };
140137 dfs(0);
141138 return ans;
142139 }
143140};
141+
144142```
145143
146144#### Go
147145
148146```go
149147func permutation(S string) (ans []string) {
150- t := []byte{}
151- vis := make([]bool, len(S))
148+ t := []byte(S)
149+ n := len(t)
150+ vis := make([]bool, n)
152151 var dfs func(int)
153152 dfs = func(i int) {
154- if i >= len(S) {
153+ if i >= n {
155154 ans = append(ans, string(t))
156155 return
157156 }
158157 for j := range S {
159- if vis[j] {
160- continue
158+ if !vis[j] {
159+ vis[j] = true
160+ t[i] = S[j]
161+ dfs(i + 1)
162+ vis[j] = false
161163 }
162- vis[j] = true
163- t = append(t, S[j])
164- dfs(i + 1)
165- t = t[:len(t)-1]
166- vis[j] = false
167164 }
168165 }
169166 dfs(0)
@@ -178,7 +175,7 @@ function permutation(S: string): string[] {
178175 const n = S .length ;
179176 const vis: boolean [] = Array (n ).fill (false );
180177 const ans: string [] = [];
181- const t: string [] = [] ;
178+ const t: string [] = Array ( n ). fill ( ' ' ) ;
182179 const dfs = (i : number ) => {
183180 if (i >= n ) {
184181 ans .push (t .join (' ' ));
@@ -189,9 +186,8 @@ function permutation(S: string): string[] {
189186 continue ;
190187 }
191188 vis [j ] = true ;
192- t . push ( S [j ]) ;
189+ t [ i ] = S [j ];
193190 dfs (i + 1 );
194- t .pop ();
195191 vis [j ] = false ;
196192 }
197193 };
@@ -211,7 +207,7 @@ var permutation = function (S) {
211207 const n = S .length ;
212208 const vis = Array (n).fill (false );
213209 const ans = [];
214- const t = [] ;
210+ const t = Array (n). fill ( ' ' ) ;
215211 const dfs = i => {
216212 if (i >= n) {
217213 ans .push (t .join (' ' ));
@@ -222,9 +218,8 @@ var permutation = function (S) {
222218 continue ;
223219 }
224220 vis[j] = true ;
225- t . push ( S [j]) ;
221+ t[i] = S [j];
226222 dfs (i + 1 );
227- t .pop ();
228223 vis[j] = false ;
229224 }
230225 };
@@ -237,33 +232,30 @@ var permutation = function (S) {
237232
238233``` swift
239234class Solution {
240- private var s: [Character ] = []
241- private var vis: [Bool ] = Array (repeating : false , count : 128 )
242- private var ans: [String ] = []
243- private var t: String = " "
244-
245235 func permutation (_ S : String ) -> [String ] {
246- s = Array (S)
247- dfs (0 )
248- return ans
249- }
250-
251- private func dfs (_ i : Int ) {
252- if i == s.count {
253- ans.append (t)
254- return
255- }
256- for c in s {
257- let index = Int (c.asciiValue ! )
258- if vis[index] {
259- continue
236+ var ans: [String ] = []
237+ let s = Array (S)
238+ var t = s
239+ var vis = Array (repeating : false , count : s.count )
240+ let n = s.count
241+
242+ func dfs (_ i : Int ) {
243+ if i >= n {
244+ ans.append (String (t))
245+ return
246+ }
247+ for j in 0 ..< n {
248+ if ! vis[j] {
249+ vis[j] = true
250+ t[i] = s[j]
251+ dfs (i + 1 )
252+ vis[j] = false
253+ }
260254 }
261- vis[index] = true
262- t.append (c)
263- dfs (i + 1 )
264- t.removeLast ()
265- vis[index] = false
266255 }
256+
257+ dfs (0 )
258+ return ans
267259 }
268260}
269261```
0 commit comments