原题链接在这里:https://leetcode.com/problems/beautiful-arrangement/description/

题目:

Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 <= i <= N) in this array:

  1. The number at the ith position is divisible by i.
  2. i is divisible by the number at the ith position.

Now given N, how many beautiful arrangements can you construct?

Example 1:

Input: 2
Output: 2
Explanation:

The first beautiful arrangement is [1, 2]:

Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1).

Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2).

The second beautiful arrangement is [2, 1]:

Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1).

Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1.

Note:

  1. N is a positive integer and will not exceed 15.

题解:

典型的backtracking. 用visited记录用过的位置, 挨个position往后permute. 遇到不合规矩的直接返回, 看能不能走到N.

Time Complexity: exponential. 每次走到最后才遇到不和规矩的backtrack回来.

Space: O(N).

AC Java:

 class Solution {
int res = 0;
public int countArrangement(int N) {
if(N <= 0){
return 0;
} boolean [] visited = new boolean[N+1];
dfs(visited, 1, N);
return res;
} private void dfs(boolean [] visited, int pos, int N){
if(pos > N){
res++;
return;
} for(int i = 1; i<=N; i++){
if(!visited[i] && (i%pos==0 || pos%i==0)){
visited[i] = true;
dfs(visited, pos+1, N);
visited[i] = false;
}
}
}
}

跟上Beautiful Arrangement II.

LeetCode Beautiful Arrangement的更多相关文章

  1. [LeetCode] Beautiful Arrangement II 优美排列之二

    Given two integers n and k, you need to construct a list which contains n different positive integer ...

  2. [LeetCode] Beautiful Arrangement 优美排列

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...

  3. LeetCode Beautiful Arrangement II

    原题链接在这里:https://leetcode.com/problems/beautiful-arrangement-ii/description/ 题目: Given two integers n ...

  4. 【LeetCode】526. Beautiful Arrangement 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. [Swift]LeetCode526. 优美的排列 | Beautiful Arrangement

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...

  6. 526. Beautiful Arrangement

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...

  7. LC 526. Beautiful Arrangement

    uppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constr ...

  8. LC 667. Beautiful Arrangement II

    Given two integers n and k, you need to construct a list which contains n different positive integer ...

  9. 【LeetCode】667. Beautiful Arrangement II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. UI组件之UIImage

    UIImageView:图像视图,用于在应用程序中显示图片 UIImage:是将图片文件转换为程序中的图片对象 UIImageView是UIImage的载体 方法一:用此方法创建图片对象,会将图片ca ...

  2. Django ORM --- 建表、查询、删除基础

    1.什么是ORM ORM的全称是Object Relational Mapping,即对象关系映射.它的实现思想就是将关系数据库中表的数据映射成为对象,以对象的形式展现,这样开发人员就可以把对数据库的 ...

  3. CSS3 3D旋转按钮对话框

    在线演示 本地下载

  4. 大话设计模式之PHP篇 - 观察者模式

    定义观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,这个主题对象在状态发生改变时,会通知所有观察者对象,使他们能够自动更新自己. <?php /*主题类或称为通知类 ...

  5. H3C光模块相关命令和检测方法

    <Sysname>  dis transceiver interface GigabitEthernet 1/0/28  查看 GigabitEthernet1/0/28 transcei ...

  6. 基于Spring mvc 的Websocket 开发

    1.Pom.xml <dependency> <groupId>org.springframework</groupId> <artifactId>sp ...

  7. eclipse添加删除插件-eclipse marketplace

    源文地址:http://jingyan.baidu.com/article/cdddd41c5c883353cb00e19e.html 在有些版本的eclips上并没有eclipse marketpl ...

  8. 【转载】Java类加载原理解析

    Java类加载原理解析 原文出处:http://www.blogjava.net/zhuxing/archive/2008/08/08/220841.html 1       基本信息 摘要: 每个j ...

  9. BZOJ4654/UOJ223 [Noi2016]国王饮水记

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  10. java项目 里的DAO,model,service, IMPL含义

    在一般工程中 基本上都会出现上述的字眼首先 DAO 提供了应用程序与数据库之间的操作规范 和操作 用于通常数据库的增删查改 一般如果使用框架 都是由框架自动生成,提高访问效率和便于快速开发.hiber ...