美文网首页
Beautiful Arrangement

Beautiful Arrangement

作者: Frank_Kivi | 来源:发表于2018-06-28 09:34 被阅读9次

    https://www.lintcode.com/problem/beautiful-arrangement/description

    public class Solution {
        private int result = 0;
        /**
         * @param N: The number of integers
         * @return: The number of beautiful arrangements you can construct
         */
        public int countArrangement(int N) {
            // Write your code here
            boolean[] visited = new boolean[N];
            treeWalk(visited, 0);
            return result;
        }
    
        private void treeWalk(boolean[] visited, int i) {
            if (i == visited.length) {
                result++;
                return;
            }
            for (int j = 0; j < visited.length; j++) {
                if (visited[j]) {
                    continue;
                }
                if ((j + 1) % (i + 1) == 0 || (i + 1) % (j + 1) == 0) {
                    visited[j] = true;
                    treeWalk(visited, i + 1);
                    visited[j] = false;
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Beautiful Arrangement

          本文链接:https://www.haomeiwen.com/subject/vznwyftx.html