版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2017/02/28/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.
题意解析
所谓美丽的排布
是指,在i
位置上的数字能够整除i
或者被i
整除,i
从1开始。给定一个数字N
,计算有多少个这样的排布。
解法分析
这道题的主要解法就是回溯,回溯的核心是使用一个数组(假设叫used
),这个数组表示这个排布中的某一个数字(用数组下标表示)是否被使用,因为从1
开始,所以数组的长度为N+1
。
先从第1个位置找合适的数字,如果数字合适然后这个数字没有被使用则将used
数组对应下标设为已使用,以此类推。回溯的时候只需要将前面使用的标记为未使用就可以了。
解题代码
|
|