LeetCode Beautiful Arrangement
原题链接在这里: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:
- The number at the ith position is divisible by i.
- 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:
- 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;
}
}
}
}
LeetCode Beautiful Arrangement的更多相关文章
- [LeetCode] Beautiful Arrangement II 优美排列之二
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- [LeetCode] Beautiful Arrangement 优美排列
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
- LeetCode Beautiful Arrangement II
原题链接在这里:https://leetcode.com/problems/beautiful-arrangement-ii/description/ 题目: Given two integers n ...
- 【LeetCode】526. Beautiful Arrangement 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [Swift]LeetCode526. 优美的排列 | Beautiful Arrangement
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
- 526. Beautiful Arrangement
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
- LC 526. Beautiful Arrangement
uppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constr ...
- LC 667. Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- 【LeetCode】667. Beautiful Arrangement II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- UI组件之UIImage
UIImageView:图像视图,用于在应用程序中显示图片 UIImage:是将图片文件转换为程序中的图片对象 UIImageView是UIImage的载体 方法一:用此方法创建图片对象,会将图片ca ...
- Django ORM --- 建表、查询、删除基础
1.什么是ORM ORM的全称是Object Relational Mapping,即对象关系映射.它的实现思想就是将关系数据库中表的数据映射成为对象,以对象的形式展现,这样开发人员就可以把对数据库的 ...
- CSS3 3D旋转按钮对话框
在线演示 本地下载
- 大话设计模式之PHP篇 - 观察者模式
定义观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,这个主题对象在状态发生改变时,会通知所有观察者对象,使他们能够自动更新自己. <?php /*主题类或称为通知类 ...
- H3C光模块相关命令和检测方法
<Sysname> dis transceiver interface GigabitEthernet 1/0/28 查看 GigabitEthernet1/0/28 transcei ...
- 基于Spring mvc 的Websocket 开发
1.Pom.xml <dependency> <groupId>org.springframework</groupId> <artifactId>sp ...
- eclipse添加删除插件-eclipse marketplace
源文地址:http://jingyan.baidu.com/article/cdddd41c5c883353cb00e19e.html 在有些版本的eclips上并没有eclipse marketpl ...
- 【转载】Java类加载原理解析
Java类加载原理解析 原文出处:http://www.blogjava.net/zhuxing/archive/2008/08/08/220841.html 1 基本信息 摘要: 每个j ...
- BZOJ4654/UOJ223 [Noi2016]国王饮水记
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- java项目 里的DAO,model,service, IMPL含义
在一般工程中 基本上都会出现上述的字眼首先 DAO 提供了应用程序与数据库之间的操作规范 和操作 用于通常数据库的增删查改 一般如果使用框架 都是由框架自动生成,提高访问效率和便于快速开发.hiber ...