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

  1. 第 i 位的数字能被 i 整除
  2. i 能被第 i 位上的数字整除

现在给定一个整数 N,请问可以构造多少个优美的排列?

示例1:

输入: 2
输出: 2
解释: 第 1 个优美的排列是 [1, 2]:
第 1 个位置(i=1)上的数字是1,1能被 i(i=1)整除
第 2 个位置(i=2)上的数字是2,2能被 i(i=2)整除 第 2 个优美的排列是 [2, 1]:
第 1 个位置(i=1)上的数字是2,2能被 i(i=1)整除
第 2 个位置(i=2)上的数字是1,i(i=2)能被 1 整除

说明:

  1. N 是一个正整数,并且不会超过15。

参考博客:Beautiful Arrangement

方法1:

思路: 对全排列进行改造。 void  dfs函数变为int dfs 函数。

现在 int dfs()函数的伪代码如下:

int dfs(int len){
if(len==某个数){
return ;
}
int res=;
循环过程——————{
res+= dfs(len+,);
}
return res;
}

同时因为是通过筛选,因此在交换过程中加入判断条件,代码如下:

JAVA

class Solution {
public int countArrangement(int N) {
int[] nums=new int[N];
for(int i=0;i<N;i++){
nums[i]=i+1;
}
return dfs(nums,0);
}
public void swap(int[] nums,int i,int j){
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
//DFS返回的是int类型。
public int dfs(int[] nums,int len){
if(len==nums.length){
return 1; //只要能够进行到回溯的终止条件,也就是出现了符合的排列,结果+1.
}
int res=0;
for(int i=len;i<nums.length;i++){
//nums[i] num[len] 进行交换,由于Len可能是0,因此+1.这里nums[len]%(i+1)==0反而不成立!
if(nums[len]%(len+1)==0||(len+1)%nums[i]==0){
swap(nums,i,len);
res+=dfs(nums,len+1);
swap(nums,i,len);
}
}
return res;
}
}

c++:

class Solution {
public:
int dfs(vector<int>& nums,int len){
if(len==nums.size()){
return 1;
}
int res=0;
for(int i=len;i<nums.size();i++){
if(nums[i]%(len+1)==0||(len+1)%nums[i]==0){
swap(nums[i],nums[len]);
res+=dfs(nums,len+1);
swap(nums[i],nums[len]);
}
}
return res;
}
int countArrangement(int N) {
vector<int> nums(N);
for(int i=0;i<N;i++)nums[i]=i+1;
return dfs(nums,0);
}
};

方法2:

class Solution {
public:
//思路:对每一位从1-N的数字进行判断,比如1 2 3 4 5 .
//一号位可以放置:1 2 3 4 5.二号位可以放置:1 2 4 三号位只能放置:3 四号位可以放置1 2 4
//同时已经放置过的数字不能再使用了,用vis[i]==1来表示。
//判断的条件为:if(vis[i]==0&&(i%len==0||len%i==0))
int countArrangement(int N) {
int count=;
vector<int> vis(N+,);
dfs(count,N,vis,);
return count;
}
void dfs(int& count,int N,vector<int>& vis,int len){
if(len>N){
count++;
return;
}
for(int i=;i<=N;i++){
if(vis[i]==&&(i%len==||len%i==)){
vis[i]=;
dfs(count,N,vis,len+);
vis[i]=;
}
}
} };

leetcode-优美的排列的更多相关文章

  1. Leetcode 667.优美的排列II

    优美的排列II 给定两个整数 n 和 k,你需要实现一个数组,这个数组包含从 1 到 n 的 n 个不同整数,同时满足以下条件: ① 如果这个数组是 [a1, a2, a3, ... , an] ,那 ...

  2. Leetcode 526.优美的排列

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

  3. Java实现 LeetCode 667 优美的排列 II(暴力)

    667. 优美的排列 II 给定两个整数 n 和 k,你需要实现一个数组,这个数组包含从 1 到 n 的 n 个不同整数,同时满足以下条件: ① 如果这个数组是 [a1, a2, a3, - , an ...

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

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

  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. leetcode.数组.667优美的排列II-Java

    1. 具体题目 给定两个整数 n 和 k,你需要实现一个数组,这个数组包含从 1 到 n 的 n 个不同整数,同时满足以下条件:① 如果这个数组是 [a1, a2, a3, ... , an] ,那么 ...

  7. [LeetCode] Arranging Coins 排列硬币

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  8. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

  9. 31,Leetcode下一个排列 - C++ 原地算法

    题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常 ...

  10. LeetCode.1175-质数排列(Prime Arrangements)

    这是小川的第413次更新,第446篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第264题(顺位题号是1175).返回1到n的排列数,以使质数处于质数索引(索引从1开始).(请 ...

随机推荐

  1. python接口自动化读取json,yaml配置文件+封装requests+unittest+HTMLRunner实现全自动化

    # coding=utf-8 import json import requests class TestApi(object): """ /* @param: @ses ...

  2. Linux TCP server 只能接受一个 TCP 连接

    #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <str ...

  3. lwip TCP client 客户端 & FreeRTOS

    static void tcpecho_thread(void *arg) { ip_addr_t serverIpAddr; struct netbuf *buf; void *data; u16_ ...

  4. jQuery 表单元素取值与赋值方法总结

    一.普通文本框的赋值与取值 1.1.1赋值 <h2>jQuery 表单元素取值与赋值方法总结</h2> <input type="text" clas ...

  5. C#中调用方法

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  6. BottomNavigationView的使用

    BottomNavigationView的使用 废话少说, 先看东西 依赖 implementation 'com.android.support:design:26.1.0' 布局 //用这个控件需 ...

  7. from表单提交之前数据判空

    在input标签中写onclick事件,不管返回是真是假都会继续提交表单. 使用onsubmit事件 <form action="login.html" method='po ...

  8. oracle中的greatest 函数和 least函数

    oracle中的greatest 函数和 least函数 原文地址:https://blog.csdn.net/sinat_32023305/article/details/78778596    g ...

  9. 【ZOJ 2996】(1+x)^n(二项式定理)

    Please calculate the coefficient modulo 2 of x^i in (1+x)^n. Input For each case, there are two inte ...

  10. html网站meta标签大全

    案例 一.天猫 <meta charset="utf-8"> <title>天猫TMALL</title> <meta name=&quo ...