C++_DFS


  • 0
    class Solution {
    public:
    int countArrangement(int N) {
        int res = 0;
        vector<bool> numbers(N+1, false);
        find(N, res, numbers);
        return res;
     }
    
     void find(int N, int& res, vector<bool>& numbers){
        if(N == 1){
            res++;
            return;
        }
        for(int i = 1; i < numbers.size(); ++i){
            if(numbers[i]) continue;
            if(N%i ==0 || i%N == 0){
                numbers[i] = true;
                find(N-1, res, numbers);
                numbers[i] = false;
            }
        }
        return;
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.