526. Beautiful Arrangement
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.

题目大意:N个数1~N,求它有多少种排列方式,满足:对于每一位,i位上的数字能被i整除,或者i能被i位上的数字整除~
分析:深度优先搜索,从N开始一直到0,用visit标记当前元素是否访问过,当当前下标为1的时候表示当前深度优先的一条路径满足条件,result+1,否则遍历visit数组看有没有没被访问过的元素,如果满足(i % index == 0 || index % i == 0)就标记为已经访问过,然后继续向下遍历dfs(index-1)~最后返回result的结果~

class Solution {
public:int countArrangement(int N) {visit.resize(N+1);dfs(N);return result;}
private:int result = 0;vector<bool> visit;void dfs(int index) {if (index == 1) {result++;return;}for (int i = 1; i < visit.size(); i++) {if (visit[i] == false && (i % index == 0 || index % i == 0)) {visit[i] = true;dfs(index-1);visit[i] = false;}}}
};

LeetCode 526. Beautiful Arrangement相关推荐

  1. leetcode 526. Beautiful Arrangement | 526. 优美的排列(回溯)

    题目 https://leetcode.com/problems/beautiful-arrangement/ 题解 首先分析,全排列一个一个试的话(如下图),时间复杂度O(n^2),当n=15时,总 ...

  2. LeetCode笔记:526. Beautiful Arrangement

    问题: Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is c ...

  3. 【Leetcode】526. Beautiful Arrangement

    Description: Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array ...

  4. leetcode 667. Beautiful Arrangement II | 667. 优美的排列 II(Java)

    题目 https://leetcode.com/problems/beautiful-arrangement-ii/ 题解 偏智力题,评论区有人说,A typical question testing ...

  5. leetcode 526优美的数列

    题目 leetcode 526优美的数列 假设有从 1 到 N 的 N 个整数,如果从这 N 个数字中成功构造出一个数组,使得数组的第 i 位 (1 <= i <= N) 满足如下两个条件 ...

  6. 【leetcode】667. Beautiful Arrangement II

    题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive in ...

  7. leetcode 932. Beautiful Array | 932. 漂亮数组(分治法)

    题目 https://leetcode.com/problems/beautiful-array/ 题解 没想出来,最后参考了[分治]通俗的官解分析 的思路. 推理过程 草稿 class Soluti ...

  8. LeetCode 526. 优美的排列(回溯)

    1. 题目 假设有从 1 到 N 的 N 个整数,如果从这 N 个数字中成功构造出一个数组,使得数组的第 i 位 (1 <= i <= N) 满足如下两个条件中的一个,我们就称这个数组为一 ...

  9. Java实现 LeetCode 526 优美的排列(DFS)

    526. 优美的排列 假设有从 1 到 N 的 N 个整数,如果从这 N 个数字中成功构造出一个数组,使得数组的第 i 位 (1 <= i <= N) 满足如下两个条件中的一个,我们就称这 ...

最新文章

  1. java搜索文件夹中文件是否存在_java中判断文件文件夹是否存在的方法(附代码)...
  2. 造假露馅!曾创下融资纪录的科技公司,被曝用印度码农冒充AI,挣了1个多亿...
  3. 数据结构(数据库)设计规范
  4. linux系统命令基础知识
  5. bytebuffer怎么转成string_002.ABB机器人高级编程--String飞升篇
  6. python主进程 子进程_pool主进程捕获子进程异常
  7. 一些弹出框的用法例子(二)
  8. 电脑一族,打电脑时候的健康的坐姿
  9. 计算机内存插在主板的哪个槽,四个内存插槽,这是正确的安装顺序
  10. DevOps 火爆,要学这么多技术!
  11. java编程详解 pdf_Java高并发编程详解:多线程与架构设计 高清pdf扫描版[154MB]
  12. UA PHYS515A 电磁学II 静电学问题8 球坐标系中的Laplace方程与球谐函数
  13. java gui 测试工具_开发者眼中最好的22款GUI测试工具(上)
  14. 手机android安装程序,Android手机程序安装指南
  15. SAP CO-PA(盈利能力分析)
  16. c语言三种结构化程序设计方法,C语言中用于结构化程序设计的三种基本结构是什么...
  17. tlwr840n虚拟服务器,TP-Link TL-WR840N 300M无线路由器设置
  18. EF(Entity Framework、EF Core)
  19. speak-tts 文字转换语音的使用播放、暂停、恢复
  20. 活跃地址下降,成交量大幅上涨,疑似青黄不接——陀螺财经周报第八期

热门文章

  1. 借助 OpenGL* ES 2.0 实现动态分辨率渲染
  2. 菜鸟的学习之路(8) — 数组(Array)
  3. 通过JDBC和Hibernate对Clob和Blob的操作
  4. file表单元素怎样设置浏览按钮的样式
  5. oracle的磁盘测试工具orion
  6. python GIL 全局锁,多核cpu下的多线程性能究竟如何?
  7. 对象池common-pool2源码分析之对象状态
  8. Ubuntu 修复windows启动项
  9. hdu2000——ASCII码排序
  10. 惠普OMEN游戏本驱动曝内核级漏洞,影响数百万Windows 计算机