原题链接在这里: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. 使用新浪IP库获取IP详细地址

    使用新浪IP库获取IP详细地址 <?php class Tool{ /** * 获取IP的归属地( 新浪IP库 ) * * @param $ip String IP地址:112.65.102.1 ...

  2. 012_Eclipse中使用 HDFS URL API 事例介绍

    本事例其实和使用hdfs FileSystem API差不多,FileSystem API也是通过解释成URL在hdfs上面执行的,性质相同,但是实际中用 的fFileSystem会多一点,源码如下: ...

  3. linux中安装php

    1.在php官网找到对应的php版本下载下来(php官网地址http://php.net) 2.把下载下来的安装包放入到linux系统中 3.解压php压缩包,通过tar -zxvf  +  下载下来 ...

  4. uCOS-II的学习笔记(共九期)和例子(共六个)

    源:uCOS-II的学习笔记(共九期)和例子(共六个) 第一篇 :学习UCOS前的准备工作http://blog.sina.com.cn/s/blog_98ee3a930100w0eu.html 第二 ...

  5. 六、golang中的结构体和方法、接口

    结构体: 1.用来自定义复杂数据结构 2.struct里面可以包含多个字段(属性) 3.struct类型可以定义方法,注意和函数的区分 4.strucr类型是值类型 5.struct类型可以嵌套 6. ...

  6. iis和apache共享80端口

    Windows server 2003服务器上安装有默认 IIS 6和Apache两个服务器,IIS运行的一个.net程序,apache运行php程序,现在想让它们同时都能通过80端口访问,设置起来还 ...

  7. INSPIRED启示录 读书笔记 - 第15章 特约用户

    产品开发伙伴 为了解决两个问题——既深入洞察目标用户的需求,又赢得用户对产品的推荐,建议征集特约用户协助完成产品研发 在项目的开始阶段物色至少六位积极.活跃.乐于分享的目标户,要求是他们在产品的目标用 ...

  8. 通过公钥解密密文思路(256bits RSA)

    256bit RSA公钥安全系数极低,只需要几分钟即可破解密文,本文综合其他文章记录了一次解密256bits RSA加密的密文的过程,仅作为备忘. 1.分解公钥,分解出n与e: 1.1使用openss ...

  9. 什么是OOM?如何解决OOM问题!

    1.什么是OOM? 程序申请内存过大,虚拟机无法满足我们,然后自杀了.这个现象通常出现在大图片的APP开发,或者需要用到很多图片的时候.通俗来讲就是我们的APP需要申请一块内存来存放图片的时候,系统认 ...

  10. Struts的url-pattern配置问题

    一,servlet容器对url的匹配过程: 当一个请求发送到servlet容器的时候,容器先会将请求的url减去当前应用上下文的路径作为servlet的映射url,比如我访问的是http://loca ...