当前位置:网站首页>0 backtracking / dynamic programming medium leetcode526. Beautiful arrangement
0 backtracking / dynamic programming medium leetcode526. Beautiful arrangement
2022-07-23 12:56:00 【18 ARU】
526. A beautiful arrangement
Suppose there is a follow-up 1 To n Of n It's an integer . Use these integers to construct an array perm( Subscript from 1 Start ), As long as the following conditions are met One of , The array is just a A beautiful arrangement :
perm[i] It can be i to be divisible by
i It can be perm[i] to be divisible by
Give you an integer n , Return constructable Beautifully arranged Of Number .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/beautiful-arrangement
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
Back to the mind
Every number on the table does this , Traverse all numbers that can be divided by the current subscript or are divided by the current subscript , Then one bit forward and continue to traverse backward , Until each position has a number that meets the requirements .
class Solution {
public int countArrangement(int n) {
return dfs(n,1,1);
}
public int dfs(int n, int index, int visited) {
if (index == n + 1){
return 1;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if ((index % i == 0 || i % index == 0) && (visited >> i & 1) == 0) {
ans += dfs(n,index+1,visited | 1 << i);
}
}
return ans;
}
}
边栏推荐
- 静态路由配置实例学习记录
- @RequiredArgsConstructor注解使用
- C#随机生成一个分数,判断其成绩等级(优、良、中、差、不及格)
- Link expansion configuration of OSPF
- FTP实验及概述
- Routing and interface technology -- Summary of direct network
- Analysis ideas of strong consistency and weak consistency and concurrency skills of distributed scenarios
- @Requiredargsconstructor annotation use
- Unity shader missing problem
- Explain various network protocols in detail
猜你喜欢
随机推荐
2小时1000人吃饭,要多少台座椅?
Understanding of LSM tree (log structured merge tree)
HCIP---OSPF细节讲解
Unity3d: ugui source code, rebuild optimization
Explain various network protocols in detail
简洁描述raft与paxos在设计上的共同点和不同点
ftp部署
OSPF和RIP的路由扩展配置
MySQL性能优化,索引优化
psutil监控的简单使用
默认路由配置实例学习记录
路由与交换技术——静态路由
OSPF综合实验
Analyze redis cluster
在二叉排序树中删除节点
How to write a web page with a common text editor
FTP experiment and overview
第五周作业
详解各种网络协议
学习日记——(路由与交换技术)ACL访问控制列表









