输入一个字符串,打印出该字符串中字符的所有排列。例如:输入字符串abc,则打印出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab、cba。
这里需要注意的是防止重复的问题,所以需要使用Set来进行过滤,一个for循环就可以搞定,具体代码如下:
package com.example.demo.upload.controller;
import java.util.HashSet;
import java.util.Set;
public class test {
static Set<String> set=new HashSet<>();
public static void main(String[] args) {
String test="abcde";
permutation(test);
for(String str:set){
System.out.println(str);
}
}
public static void permutation(String test){
if (test==null)
return;
permutationtest(test.toCharArray(),0);
}
public static void permutationtest(char[] chars,int pos){
if (pos==chars.length-1){
if (!set.contains(chars.toString())){
set.add(String.valueOf(chars));
}
}
for (int i=pos;i<chars.length;i++){
char temp=chars[i];
chars[i]=chars[pos];
chars[pos]=temp;
permutationtest(chars,pos+1);
temp=chars[i];
chars[i]=chars[pos];
chars[pos]=temp;
}
}
}
返回结果如下:
D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.5\lib\idea_rt.jar=53214:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.5\bin" -Dfile.encoding=UTF-8 -classpath D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\charsets.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\deploy.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\ext\access-bridge-64.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\ext\cldrdata.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\ext\dnsns.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\ext\jaccess.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\ext\jfxrt.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\ext\localedata.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\ext\nashorn.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\ext\sunec.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\ext\sunjce_provider.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\ext\sunmscapi.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\ext\sunpkcs11.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\ext\zipfs.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\javaws.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\jce.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\jfr.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\jfxswt.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\jsse.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\management-agent.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\plugin.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\resources.jar;D:\sofeware\jdk1.8\JDK1.8\Java\jdk1.8.0_301\jre\lib\rt.jar;D:\project\fenpian\FragmentUpload-master\target\classes;C:\Users\ligx1\.m2\repository\org\springframework\boot\spring-boot-starter-thymeleaf\2.3.1.RELEASE\spring-boot-starter-thymeleaf-2.3.1.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\boot\spring-boot-starter\2.3.1.RELEASE\spring-boot-starter-2.3.1.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\boot\spring-boot\2.3.1.RELEASE\spring-boot-2.3.1.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.3.1.RELEASE\spring-boot-starter-logging-2.3.1.RELEASE.jar;C:\Users\ligx1\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\ligx1\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\ligx1\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;C:\Users\ligx1\.m2\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;C:\Users\ligx1\.m2\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;C:\Users\ligx1\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\ligx1\.m2\repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;C:\Users\ligx1\.m2\repository\org\thymeleaf\thymeleaf-spring5\3.0.11.RELEASE\thymeleaf-spring5-3.0.11.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\thymeleaf\thymeleaf\3.0.11.RELEASE\thymeleaf-3.0.11.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\attoparser\attoparser\2.0.5.RELEASE\attoparser-2.0.5.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\unbescape\unbescape\1.1.6.RELEASE\unbescape-1.1.6.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;C:\Users\ligx1\.m2\repository\org\thymeleaf\extras\thymeleaf-extras-java8time\3.0.4.RELEASE\thymeleaf-extras-java8time-3.0.4.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.3.1.RELEASE\spring-boot-starter-web-2.3.1.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.3.1.RELEASE\spring-boot-starter-json-2.3.1.RELEASE.jar;C:\Users\ligx1\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.11.0\jackson-databind-2.11.0.jar;C:\Users\ligx1\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.0\jackson-annotations-2.11.0.jar;C:\Users\ligx1\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.11.0\jackson-core-2.11.0.jar;C:\Users\ligx1\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.0\jackson-datatype-jdk8-2.11.0.jar;C:\Users\ligx1\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.0\jackson-datatype-jsr310-2.11.0.jar;C:\Users\ligx1\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.0\jackson-module-parameter-names-2.11.0.jar;C:\Users\ligx1\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.3.1.RELEASE\spring-boot-starter-tomcat-2.3.1.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.36\tomcat-embed-core-9.0.36.jar;C:\Users\ligx1\.m2\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;C:\Users\ligx1\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.36\tomcat-embed-websocket-9.0.36.jar;C:\Users\ligx1\.m2\repository\org\springframework\spring-web\5.2.7.RELEASE\spring-web-5.2.7.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\spring-beans\5.2.7.RELEASE\spring-beans-5.2.7.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\spring-webmvc\5.2.7.RELEASE\spring-webmvc-5.2.7.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\spring-aop\5.2.7.RELEASE\spring-aop-5.2.7.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\spring-context\5.2.7.RELEASE\spring-context-5.2.7.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\spring-expression\5.2.7.RELEASE\spring-expression-5.2.7.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\spring-core\5.2.7.RELEASE\spring-core-5.2.7.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\spring-jcl\5.2.7.RELEASE\spring-jcl-5.2.7.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\projectlombok\lombok\1.18.12\lombok-1.18.12.jar;C:\Users\ligx1\.m2\repository\commons-io\commons-io\2.4\commons-io-2.4.jar;C:\Users\ligx1\.m2\repository\commons-fileupload\commons-fileupload\1.3.3\commons-fileupload-1.3.3.jar;C:\Users\ligx1\.m2\repository\mysql\mysql-connector-java\8.0.22\mysql-connector-java-8.0.22.jar;C:\Users\ligx1\.m2\repository\com\baomidou\mybatis-plus-boot-starter\3.3.2\mybatis-plus-boot-starter-3.3.2.jar;C:\Users\ligx1\.m2\repository\com\baomidou\mybatis-plus\3.3.2\mybatis-plus-3.3.2.jar;C:\Users\ligx1\.m2\repository\com\baomidou\mybatis-plus-extension\3.3.2\mybatis-plus-extension-3.3.2.jar;C:\Users\ligx1\.m2\repository\com\baomidou\mybatis-plus-core\3.3.2\mybatis-plus-core-3.3.2.jar;C:\Users\ligx1\.m2\repository\com\baomidou\mybatis-plus-annotation\3.3.2\mybatis-plus-annotation-3.3.2.jar;C:\Users\ligx1\.m2\repository\com\github\jsqlparser\jsqlparser\3.1\jsqlparser-3.1.jar;C:\Users\ligx1\.m2\repository\org\mybatis\mybatis\3.5.4\mybatis-3.5.4.jar;C:\Users\ligx1\.m2\repository\org\mybatis\mybatis-spring\2.0.4\mybatis-spring-2.0.4.jar;C:\Users\ligx1\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.3.1.RELEASE\spring-boot-autoconfigure-2.3.1.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\2.3.1.RELEASE\spring-boot-starter-jdbc-2.3.1.RELEASE.jar;C:\Users\ligx1\.m2\repository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;C:\Users\ligx1\.m2\repository\org\springframework\spring-jdbc\5.2.7.RELEASE\spring-jdbc-5.2.7.RELEASE.jar;C:\Users\ligx1\.m2\repository\org\springframework\spring-tx\5.2.7.RELEASE\spring-tx-5.2.7.RELEASE.jar com.example.demo.upload.controller.test
adecb
abced
dabce
bedca
daceb
decab
bacde
aebcd
acdeb
ecbad
eadcb
cdaeb
cebda
caedb
dbcae
cbead
cadbe
dcabe
dceba
bdcea
dbaec
dcbea
baced
bcdae
eadbc
becad
aebdc
edacb
abecd
cedab
cbade
cbeda
cadeb
daecb
bcaed
dabec
bcade
bceda
debac
bdeac
edcba
ebadc
ceadb
ceabd
adcbe
abedc
daebc
cbdae
becda
dcaeb
decba
ebdac
baecd
eacbd
ebacd
edcab
acebd
abdce
acbde
aedbc
dcbae
bcdea
bdace
bdeca
baedc
aedcb
acbed
ecdab
eabcd
cbaed
cedba
dbeac
cdbae
bdaec
dbcea
debca
ebcad
badce
adceb
ebdca
ecabd
eacdb
edbac
ecbda
acdbe
cdabe
cdeba
cebad
cabed
acedb
adbce
abdec
aecbd
dceab
beacd
bdcae
edabc
ebcda
cdbea
cbdea
cabde
adebc
abcde
cdeab
dacbe
beadc
caebd
dbace
dbeca
deacb
badec
deabc
bcead
bedac
adbec
ecdba
eabdc
aecdb
ecadb
edbca
Process finished with exit code 0
|