How to implement full permutation in Java algorithm
Algorithm 1
is based on recursion and backtracking. When arranging 1, 2, and 3, first go back from 3 to 2 and find that there are no other possible situations, then go back to 1, arrange 1, 3, 2, and then go back up to when there are other situations, that is, the root node, and then When arranging 2 as the first position, repeat the above process to put all possible results into res.
Code:
import java.util.ArrayList; import java.util.List; public class h718_1 { static List<List<Integer>> res = new ArrayList<>(); public static void main(String[] args) { int[] arr = {1,2,3}; h718_1 h2 = new h718_1(); h2.dfs(arr,new ArrayList<>()); for (List<Integer> re : res) { System.out.println(re); } } public List<List<Integer>> dfs( int[] arr,List<Integer> list){ List<Integer> temp = new ArrayList<>(list); if (arr.length == list.size()){ res.add(temp); } for (int i=0;i<arr.length;i++){ if (temp.contains(arr[i])){ continue; } temp.add(arr[i]); dfs(arr,temp); temp.remove(temp.size()-1); } return res; } }
Algorithm 2
Realize full arrangement by exchanging positions: Assume that the set is {1, 2, 3, 4 };
Loop exchange positions: 1 and 1 exchange; 1 and 2 exchange; 1 and 3 exchange; 1 and 4 exchange;
Each exchange calls a smaller set recursively:
For example: the first exchange of 1 and 1 determines that 1 is in the first place, so it can be regarded as {1} recursive exchange {2,3,4};
The first exchange of 1 and 1 The exchange of 2 determines that 2 is in the first place, so it can be regarded as {2}. The recursive exchange {1,3,4};
The first exchange of 1 and 3 determines that 3 is in the first place, so it can be regarded as {3} Recursive exchange {1,2,4};
The first exchange of 1 and 4 determines that 4 is in the first place, so it can be regarded as {4} Recursive exchange {1,2,3};
And so on.
Code:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class h718_2 { static List<List<Integer>> res = new ArrayList<>(); public static void main(String[] args) { int[] arr = {1,2,3}; h718_2 h3 = new h718_2(); h3.pailie_swap(0,arr); } public void pailie_swap(int index, int[] arr){ if (arr.length==index){ System.out.println(Arrays.toString(arr)); return; } for (int i = index;i<arr.length;i++){ swap(i,index,arr); pailie_swap(index+1,arr); swap(i,index,arr); } } public void swap(int i,int j ,int[] arr){ int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } }
Algorithm 3
You can achieve full arrangement by adding elements:
First define a list and put the first element into; Then insert the remaining elements into all possible positions of the previous set elements to generate a new list;
For example: implement full arrangement of {1,2,3,4}
First define The first element added to a list is {1}; then the second element 2 can be inserted into the two positions before and after {1} to form a new list: {21, 12}, and the third element 3 is inserted into all the elements of the list respectively. The positions are: {321, 231, 213, 312, 132, 123}; and so on.
Code:
import java.util.ArrayList; public class h718_3 { public static void main(String[] args) { String aa = "123"; h718_3 h4 = new h718_3(); ArrayList<String> res = new ArrayList<>(); res = h4.getPermutation0(aa); for (String re : res) { System.out.println(re); } } public ArrayList<String> getPermutation0(String A) { int n = A.length(); ArrayList<String> res = new ArrayList<>(); res.add(A.charAt(0) + "");//初始化,包含第一个字符 for (int i = 1; i < n; i++) {//第二个字符插入到前面生成集合的每个元素里面 ArrayList<String> res_new = new ArrayList<>(); char c = A.charAt(i);//新字符 for (String str : res) {//访问上一趟集合中的每个字符串 // 插入到每个位置,形成一个新串 String newStr = c + str;//加在前面 res_new.add(newStr); newStr = str + c;//加在后面 res_new.add(newStr); //加在中间 for (int j = 1; j < str.length(); j++) { newStr = str.substring(0, j) + c + str.substring(j); res_new.add(newStr); } } res = res_new;//更新 } return res; } }
The above is the detailed content of How to implement full permutation in Java algorithm. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
