Full arrangement of n (stack and recursion)

Description
gives you a number n, please list all permutations of 1, 2, 3, 4...n-1, n.

Input
Input a number n
Ouput
output the corresponding full arrangement
Sample Input

3

Sample Output

[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]

Algorithm idea: first push 1-n into the stack in turn, this is the first set of arrangement, output; then loop while(!st.isEmpty()) pops the top element of the stack each time, and then find out whether there is a number outside the stack If there is one that is larger than the popped number, push the large number into the stack, and then find the numbers that are not in the stack from 1-n, push them into the stack in turn, arrange the output once, and end
Insert picture description here

Here is a simulation of the full arrangement of 3
out->1 2 3
(1, 2) 3 pops, (1) 3 pops, 2 pops, (1, 3 pops) 2, (1, 3, 2 pops) )
Out->1 3 2
(1,3) 2 pops, (1) 2 pops, 3 pops, () 2 pops, 3 pops, 1 pops
(2 pushes) 3, 1 (2 , 1 into the stack) 3 (
2, 1, 3 into the stack) out->2 1 3
(2, 1) 3 out of the stack (2) 3 out of the stack, 1 out of the stack (2, 3 into the stack) 1 (2, 3, 1 push)
out->2 3 1
(2, 3) 1 pop (2) 1 pop, 3 pop () 1 pop, 3 pop, 2 pop
(3 push) 1, 2 (3,1 into the stack) 2 (3,1, 2 into the stack)
out->3 1 2
(3,1) 2 out of the stack (3) 2 out of the stack, 1 out of the stack (3, 2 into the stack) 1 ( 3, 2, 1 into the stack)
out->3 2 1

Use stack code:

import java.util.Scanner;
import java.util.Stack;


public class Main {
    
    
	
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
    
    
			int n = sc.nextInt();
			Stack<Integer> st = new Stack<Integer>();
			for(int i=1;i<=n;i++){
    
    
				st.push(i);
			}
			System.out.println(st.toString());
			while(!st.isEmpty()){
    
    
				int i = st.pop();
				//System.out.println(i);
				for(;i<n;i++){
    
    
					/*
					 * st.search(x) 查找栈中有没有这个数,如果有返回它距离栈顶的距离
					 * 否则返回-1
					 */
					if(st.search(i+1)<0){
    
    
						st.push(i+1); //将大于弹出的这个数入栈
						//接着将未入栈的从小到大依次入栈
						for(int j=1;j<=n;j++){
    
    
							if(st.search(j)<0){
    
    
								st.push(j);
							}
						}
						//全部入栈排完了一组,输出结束
						System.out.println(st.toString());
						break;
					}
				}
			}
		}
	}

}

Recursion

import java.util.Scanner;


public class Main {
    
    
	static int n;   //对1-n进行全排
	static int flat[]; //做标记
	static int p[];  //存放排好的数
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		int count = 1;
		while(sc.hasNext()){
    
    
			n = sc.nextInt();
			flat = new int[n];
			p = new int[n];
			System.out.println("Case "+count+++":");
			DFS(0);
		}
	}
	public static void DFS(int step){
    
     //第step步开始
		if(step==n){
    
     //出口
			System.out.print("["+p[0]);
			for(int i=1;i<n;i++){
    
    
				System.out.print(", "+p[i]);
			}
			System.out.println("]");
		}else{
    
    
			for(int i=0;i<n;i++){
    
    
				if(flat[i]==0){
    
     //第i个数没用过
					p[step] = i+1; //第step放i+1
					flat[i] = 1;   //标记为用过
					DFS(step+1);   //接着排下一个位置
					flat[i] = 0;   //排完后清除标记,回溯
				}
			}
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_45880043/article/details/109178776