forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_526.java
33 lines (27 loc) · 890 Bytes
/
_526.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.fishercoder.solutions;
public class _526 {
public static class Solution1 {
/**
* A good post to look at: https://discuss.leetcode.com/topic/79916/java-solution-backtracking
* and there's a generic template afterwards for backtracking problems
*/
int count = 0;
public int countArrangement(int N) {
backtracking(N, new int[N + 1], 1);
return count;
}
private void backtracking(int N, int[] used, int pos) {
if (pos > N) {
count++;
return;
}
for (int i = 1; i <= N; i++) {
if (used[i] == 0 && (i % pos == 0 || pos % i == 0)) {
used[i] = 1;
backtracking(N, used, pos + 1);
used[i] = 0;
}
}
}
}
}