[leetcode-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.
思路:
回溯法。。。
int counts(int n,vector<int>& intvec)
{
if (n <= ) return ;
int res = ;
for (int i = ; i < n;i++)
{
if (intvec[i]%n == || n %intvec[i] ==)
{
swap(intvec[i], intvec[n - ]);
res += counts(n - , intvec);
swap(intvec[i], intvec[n - ]);
}
}
return res;
}
int countArrangement(int N)
{
vector<int> intvec;
for (int i = ; i < N; i++)intvec.push_back(i + );
return counts(N, intvec);
}
参考:
https://discuss.leetcode.com/topic/79921/my-c-elegant-solution-with-back-tracking
[leetcode-526-Beautiful Arrangement]的更多相关文章
- 【LeetCode】526. Beautiful Arrangement 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- [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 Beautiful Arrangement
原题链接在这里:https://leetcode.com/problems/beautiful-arrangement/description/ 题目: Suppose you have N inte ...
- [Swift]LeetCode526. 优美的排列 | Beautiful Arrangement
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
- 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 ...
随机推荐
- centos6.5软件安装:RPM,SRPM与yum功能
鸟哥的linxu私房菜读书笔记 前言: Linux上软件的安装可以以原始码的方式来安装软件,也就是利用厂商释出的 Tarball 来进行软件的安装.不过,你应该很容易发现,那就是每次安装软件都需要侦测 ...
- Zookeeper ZAB 协议分析
前言 ZAB 协议是为分布式协调服务 ZooKeeper 专门设计的一种支持崩溃恢复的原子广播协议.在 ZooKeeper 中,主要依赖 ZAB 协议来实现分布式数据一致性,基于该协议,ZooKeep ...
- html学习笔记 - 标签
单标签 : <!DOCTYPE html> 解析类型标签 <!-- xxx --> 注释标签 <br /> 换行标签 <hr /> 分割线标签 < ...
- SimpleDateFormat使用和线程安全问题
SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (date -> text).语法分析 (text -> date)和标准化. Simpl ...
- 三、 添加视图View(ASP.NET MVC5 系列)
在这一章节我们可以修改HelloWorldController类,通过使用视图模板来封装处理产生给客户端的HTML响应. 我们将使用Razor View engine来创建视图文件.基于Razor的视 ...
- 笔记本shift变粘贴,粘滞键设置已关闭
之前手贱吧,拿湿抹布擦了擦笔记本电脑的自带键盘,然后部分按键失灵了. 本想着反正也都是在寝室用的,趁机找借口买了个机械键盘,啪啪啪... 刚开始好好的,后来发现一按shift就会粘贴,百度了下都说是粘 ...
- 刨根究底字符编码之五——简体汉字编码方案(GB2312、GBK、GB18030、GB13000)以及全角、半角、CJK
简体汉字编码方案(GB2312.GBK.GB18030.GB13000)以及全角.半角.CJK 一.概述 1. 英文字母再加一些其他标点字符之类的也不会超过256个,用一个字节来表示一个字符就足够 ...
- 【2017-05-25】WebForm母版页
母版页:可以把界面的部分代码进行重用 添加新项-母版页 在母版页中界面代码不要写在 <asp:ContentPlaceHolder ID="head" runat=" ...
- PHP实现记录日志(文件)
PHP实现记录日志(文件) php php 记录日志 项目中经常会记录些操作信息,或是打印些关键变量,或者是导入excel文件,提现记录,都需记录.经常遇到,封装一个方法,有不好的地方或补充请留言. ...
- Akka(5): ConsistentHashing Router - 可选定Routee的任务分配模式
上一篇讨论里我们介绍了几种任务分配(Routing)模式.Akka提供的几种现成智能化Routing模式大多数是通过对用户屏蔽具体的运算Routee选择方式来简化Router使用,提高智能程度,所以我 ...