当前位置:网站首页>[day 27] given an integer n, print out the full permutation from 1 to n | Full Permutation template
[day 27] given an integer n, print out the full permutation from 1 to n | Full Permutation template
2022-06-27 13:10:00 【Executory peduncle】
Learning Guide
order 、 Column Preface
This column opens , The purpose is to help everyone better master learning Java, Especially some Java Learners' It is difficult to find systematic algorithm learning materials on the Internet to help you get started with algorithms , At the same time, if you have any questions about the contents of the column, you can add my wechat at the end of the article to give you a one-to-one explanation .
But the most important thing is to think independently , For all the content of this column , Be able to fully master , Write the code completely by yourself , There must be no problem getting started with the algorithm .
The learning of algorithm must not be short of summary , Here I recommend that you can go to University algorithm community Punch in the learned knowledge , To consolidate and review .
The only way to learn algorithms well must be Topic sea strategy , Only by accumulating a lot of practice can you practice your skills . Any topic of the column I will start with 【 Title Description 】【 Their thinking 】【 Template code 】【 Code parsing 】 Wait for four plates to explain .
order 、 Preface of this chapter
The frequency of full array investigation is still a little high ,java Library functions are not included , So you need to write all the permutation functions by yourself , There are many different versions of Full Permutation functions , Here we mainly give two versions of functions that consider order and do not consider order . stay n n n In small cases , We can also use n n n Of for Loop to enumerate .
One 、【 Example 1】
1、 Title Description
Given an integer n ( 1 ≤ n ≤ 9 ) n(1 \leq n \leq 9) n(1≤n≤9), Please print out [ 1 , n ] [1,n] [1,n] The whole arrangement , There is no need to consider the order .
2、 Their thinking
- ( 1 ) (1) (1) Two kinds of fully arranged templates are given , Just shine on your back .
3、 Template code
No dictionary order output template is required
import java.util.*;
public class Main{
static int[] arr;
static int n;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
arr=new int[n];
for (int i = 0; i <n; i++) {
arr[i]=i+1;
}
dfs(0);
}
static void dfs(int k){
if (k==n){
// Realization check Logic
System.out.println(Arrays.toString(arr));
}
for (int i = k; i <n; i++) {
swap(i,k);
dfs(k+1);
swap(i,k);
}
}
static void swap(int a,int b){
int c=arr[a];
arr[a]=arr[b];
arr[b]=c;
}
}
Output templates in dictionary order
import java.util.*;
public class Main{
static List<List<Integer>> list=new ArrayList<>();
public static void main(String[] args)throws Exception{
List<Integer> path=new ArrayList<>();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
dfs(n,path);
for(int j=0;j<list.size();j++){
for(int i=0;i<list.get(j).size();i++){
System.out.print(list.get(j).get(i)+" ");
}
System.out.println();
}
}
public static void dfs(int n,List<Integer> path){
if(path.size()==n){
list.add(new ArrayList(path));
return;
}
for(int i=1;i<=n;i++){
if(path.contains(i)){
continue;
}
path.add(i);
dfs(n,path);
path.remove(path.size()-1);
}
}
}
4、 Code parsing
- ( 1 ) (1) (1) The first kind of unordered templates is what we often use , Through the recursive exchange of two positions to achieve the enumeration of the full arrangement .
dfsVariablekIt means that the subscript is determined to bekNumber of numbers , Whenk==nWe have found a situation , At this time, you can write according to the requirements of the topiccheckfunction , Here we just need to print . - ( 2 ) (2) (2) The first template usually helps us enumerate the answers , Find an arrangement that meets the requirements , Especially the Blue Bridge Cup in previous years , For details, please refer to my blue bridge article .
- ( 3 ) (3) (3) Templates output in dictionary order are rarely used , The general topic survey is just for you to print .

3、 ... and 、 Recommendation column
Four 、 After-school exercises
| Serial number | Topic link | Difficulty rating |
|---|---|---|
| 1 | Book arrangement | 2 |
边栏推荐
- OpenHGNN发布0.3版本
- What kind of air conditioner is this?
- Deploy redis sentinel mode using bitnamiredis Sentinel
- 【周赛复盘】LeetCode第81场双周赛
- Istio微服务治理网格流量管理核心资源控制器详解
- script defer async模式
- 每日刷题记录 (六)
- [tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (III)
- Yuweng information, a well-known information security manufacturer, joined the dragon lizard community to build an open source ecosystem
- 阿胖的操作记录
猜你喜欢

ensp云朵配置

夏日里的清凉

Quanzhi A13 tossing memo
![[tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (I)](/img/ce/b58e436e739a96b3ba6d2d33cf8675.png)
[tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (I)

【周赛复盘】LeetCode第81场双周赛

hue新建账号报错解决方案

Implementation of recruitment website based on SSM

阿胖的操作记录

Industry insight - how should brand e-commerce reshape growth under the new retail format?
![[dynamic programming] - Knapsack Problem](/img/27/c48284f15e3f80305d7ce7c02d4378.png)
[dynamic programming] - Knapsack Problem
随机推荐
Infiltration learning diary day20
What is the next step in the recommendation system? Alispacetime aggregates GNN, and the effect is to sling lightgcn!
The world's fastest download tool XDM
Cloud native (30) | kubernetes' app store Helm
Database Series: MySQL index optimization and performance improvement summary (comprehensive version)
Centos7命令行安装Oracle11g
Quanzhi A13 tossing memo
Three traversal methods of binary tree
It is so simple to remove the payment restrictions on VIP, YuQue and Zhihu in Baidu Library
Shake hands with life and make peace
诗歌一首看看
内网学习笔记(8)
Steps for win10 to completely and permanently turn off automatic updates
【动态规划】—— 背包问题
Realization of hospital medical record management system based on JSP
What kind of air conditioner is this?
Today's sleep quality record 78 points
Esp32s3 iperf routine test esp32s3 throughput test
Deeply convinced plan X - system foundation summary
Openfeign service interface call