原题链接在这里: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 ...
随机推荐
-
认识Runtime2
我定义了一个Person类作为测试. 其中Person.h: // // Person.h // Test // // Created by zhanggui on 15/8/16. // Copyr ...
-
MySQL 部分函数使用
1.DATE_ADD 参考博客:MySQL日期时间函数大全 转 例:DATE_ADD(date,INTERVAL expr type) 2.日期转字符串 DATE_FORMAT 参考博客:MYSQL中 ...
-
jquery之event与originalEvent的关系、event事件对象用法浅析
在jquery中,最终传入事件处理程序的 event 其实已经被 jQuery 做过标准化处理, 其原有的事件对象则被保存于 event 对象的 originalEvent 属性之中, 每个 even ...
-
JavaScript实现样式表的简单切换
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
-
实现透明渐变的Activity
如果光是透明全屏的Activity的话,直接继承内置theme即可 <activity android:theme="@android:style/Theme.NoTitleBar.F ...
-
OC类的介绍
类的本质 类的本质其实也是一个对象(类对象) 类对象 类对象再程序运行时一直存在 类对象是一种数据结构,存储类的基本信息:类大小,类名称,类的版本以及消息与函数的映射表等 类对象所保存的信息在程序编译 ...
-
u盘安装ubuntu10.04 、11.04 server
10.04 先将 ubuntu server 的 iso 放到优盘上,然后在提示无法找到光驱时,按 alt+f2 打开一个新的 console 窗口,将 iso mount 上,具体操作如下: ls ...
-
Linux内核分析作业 NO.6
进程的描述和进程的创建 于佳心 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-100002900 ...
-
【POJ 2823】Sliding Window(单调队列/堆)
BUPT2017 wintertraining(16) #5 D POJ - 2823 题意 给定n,k,求滑窗[i,i+k-1]在(1<=i<=n)的最大值最小值. 题解 单调队列或堆. ...
-
flex布局简介
一.概述 浮动在移动布局中不再重要,flex盒模型越来越重要. flexbox经历过三个版本,主要区别是2009年到2012年之间的语法变化. 最新的语法和现在规范是同步的(例display:flex ...