https://leetcode.com/problems/beautiful-arrangement/#/description
找出数组每个元素均满足nums[i] % i == 0 || i % nums[i] == 0的数组个数
全局count + cache回溯
public class Solution {
int count = 0;
public int countArrangement(int N) {
int[] cache = new int[N + 1];
trace(cache, 1, N);
return count;
}
private void trace(int[] cache, int pos, int N) {
if (pos > N) {
count++;
return;
}
for (int i = 1; i <= N; i++) {
if (cache[i] == 0 && (i % pos == 0 || pos % i == 0)) {
cache[i] = 1;
trace(cache, pos + 1, N);
cache[i] = 0;
}
}
}
}