c++ dfs


  • 1
    T
    class Solution {
    public:
        void search(int &mask, int number, int N, int &count) {
            if (!number) {
                ++count; return;
            }
            for (int indx = 1; indx <= N; ++indx) {
                if (mask & (1 << indx) || number%indx && indx%number) continue;
                mask |= (1 << indx);
                search(mask, number-1, N, count);
                mask &= ~(1 << indx);
            }
        }
        int countArrangement(int N) {
            int count = 0, mask = 0;
            search(mask, N, N, count);
            return count;
        }
    };
    

Log in to reply
 

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