File tree 4 files changed +109
-0
lines changed
4 files changed +109
-0
lines changed Original file line number Diff line number Diff line change @@ -48,6 +48,28 @@ class Solution:
48
48
return list (permutations(nums))
49
49
```
50
50
51
+ ``` python
52
+ class Solution :
53
+ def permute (self , nums : List[int ]) -> List[List[int ]]:
54
+ def dfs (i ):
55
+ if i == n:
56
+ ans.append(t[:])
57
+ return
58
+ for j in range (n):
59
+ if not vis[j]:
60
+ vis[j] = True
61
+ t[i] = nums[j]
62
+ dfs(i + 1 )
63
+ vis[j] = False
64
+
65
+ n = len (nums)
66
+ vis = [False ] * n
67
+ t = [0 ] * n
68
+ ans = []
69
+ dfs(0 )
70
+ return ans
71
+ ```
72
+
51
73
``` java
52
74
class Solution {
53
75
private List<List<Integer > > ans = new ArrayList<> ();
Original file line number Diff line number Diff line change @@ -198,6 +198,36 @@ function totalNQueens(n: number): number {
198
198
}
199
199
```
200
200
201
+ ``` cs
202
+ public class Solution {
203
+ public int TotalNQueens (int n ) {
204
+ bool [] cols = new bool [10 ];
205
+ bool [] dg = new bool [20 ];
206
+ bool [] udg = new bool [20 ];
207
+ int ans = 0 ;
208
+
209
+ void dfs (int i ) {
210
+ if (i == n ) {
211
+ ans ++ ;
212
+ return ;
213
+ }
214
+ for (int j = 0 ; j < n ; j ++ ) {
215
+ int a = i + j , b = i - j + n ;
216
+ if (cols [j ] || dg [a ] || udg [b ]) {
217
+ continue ;
218
+ }
219
+ cols [j ] = dg [a ] = udg [b ] = true ;
220
+ dfs (i + 1 );
221
+ cols [j ] = dg [a ] = udg [b ] = false ;
222
+ }
223
+ }
224
+
225
+ dfs (0 );
226
+ return ans ;
227
+ }
228
+ }
229
+ ```
230
+
201
231
<!-- tabs: end -->
202
232
203
233
<!-- end -->
Original file line number Diff line number Diff line change @@ -190,6 +190,36 @@ function totalNQueens(n: number): number {
190
190
}
191
191
```
192
192
193
+ ``` cs
194
+ public class Solution {
195
+ public int TotalNQueens (int n ) {
196
+ bool [] cols = new bool [10 ];
197
+ bool [] dg = new bool [20 ];
198
+ bool [] udg = new bool [20 ];
199
+ int ans = 0 ;
200
+
201
+ void dfs (int i ) {
202
+ if (i == n ) {
203
+ ans ++ ;
204
+ return ;
205
+ }
206
+ for (int j = 0 ; j < n ; j ++ ) {
207
+ int a = i + j , b = i - j + n ;
208
+ if (cols [j ] || dg [a ] || udg [b ]) {
209
+ continue ;
210
+ }
211
+ cols [j ] = dg [a ] = udg [b ] = true ;
212
+ dfs (i + 1 );
213
+ cols [j ] = dg [a ] = udg [b ] = false ;
214
+ }
215
+ }
216
+
217
+ dfs (0 );
218
+ return ans ;
219
+ }
220
+ }
221
+ ```
222
+
193
223
<!-- tabs: end -->
194
224
195
225
<!-- end -->
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public int TotalNQueens ( int n ) {
3
+ bool [ ] cols = new bool [ 10 ] ;
4
+ bool [ ] dg = new bool [ 20 ] ;
5
+ bool [ ] udg = new bool [ 20 ] ;
6
+ int ans = 0 ;
7
+
8
+ void dfs ( int i ) {
9
+ if ( i == n ) {
10
+ ans ++ ;
11
+ return ;
12
+ }
13
+ for ( int j = 0 ; j < n ; j ++ ) {
14
+ int a = i + j , b = i - j + n ;
15
+ if ( cols [ j ] || dg [ a ] || udg [ b ] ) {
16
+ continue ;
17
+ }
18
+ cols [ j ] = dg [ a ] = udg [ b ] = true ;
19
+ dfs ( i + 1 ) ;
20
+ cols [ j ] = dg [ a ] = udg [ b ] = false ;
21
+ }
22
+ }
23
+
24
+ dfs ( 0 ) ;
25
+ return ans ;
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments