From 7e939c7cc9dc6a218193f794fdc8be7ca3f9ccb6 Mon Sep 17 00:00:00 2001 From: Mars Liu Date: Thu, 25 Nov 2021 01:20:41 +0800 Subject: [PATCH] add permutation --- .../5.\345\207\275\346\225\260/config.json" | 3 +- .../permutation.json" | 7 + .../permutation.md" | 205 ++++++++++++++++++ 3 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 "data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/permutation.json" create mode 100644 "data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/permutation.md" diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/config.json" index 619ab8a..7faf1d7 100644 --- "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/config.json" +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/config.json" @@ -7,6 +7,7 @@ "children": [], "export": [ "call.json", - "parser.json" + "parser.json", + "permutation.json" ] } \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/permutation.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/permutation.json" new file mode 100644 index 0000000..040ed99 --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/permutation.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "permutation.md", + "notebook_enable": false, + "exercise_id": "a52d29ac72cf48669824894ba7f7427a" +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/permutation.md" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/permutation.md" new file mode 100644 index 0000000..33c4494 --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/permutation.md" @@ -0,0 +1,205 @@ +# 排列 + +给定一个几十个字符内的短字符串,简单起见我们假设这个字符串没有重复字符,那么生成其全排列的代码是: + +## 答案 + +```java +package net.csdn; + +import java.util.ArrayList; +import java.util.List; + +public class Main { + + public static String drop(String token, int idx){ + if(idx==0){ + return token.substring(1); + } + if(idx>=token.length()-1){ + return token.substring(0, idx); + } + return token.substring(0, idx) + token.substring(idx+1); + } + + public static List permutation(String token){ + if(token.length() < 2){ + return List.of(token); + } + if(token.length()==2){ + String item = token.charAt(1) + String.valueOf(token.charAt(0)); + return List.of(token, item); + } + List result = new ArrayList<>(); + for(int idx=0;idx= token.length() - 1) { + return token.substring(0, idx); + } + return token.substring(0, idx) + token.substring(idx + 1); + } + + public List permutation(String token) { + if (token.length() < 2) { + return List.of(token); + } + if (token.length() == 2) { + String item = token.charAt(1) + String.valueOf(token.charAt(0)); + return List.of(token, item); + } + List result = new ArrayList<>(); + for (int idx = 0; idx < token.length(); idx++) { + char chr = token.charAt(idx); + String next = drop(token, idx); + + for (var rest : permutation(next)) { + result.add(chr + rest); + } + } + return result; + } + + public static void main(String[] args) { + String token = args[0]; + for (var item : permutation(token)) { + System.out.println(item); + } + } +} +``` + +### 方法引用不对 + +```java +package net.csdn; + +import java.util.ArrayList; +import java.util.List; + +public class Main { + + public static String drop(String token, int idx){ + if(idx==0){ + return token.substring(1); + } + if(idx>=token.length()-1){ + return token.substring(0, idx); + } + return token.substring(0, idx) + token.substring(idx+1); + } + + public static List permutation(String token){ + if(token.length() < 2){ + return List.of(token); + } + if(token.length()==2){ + String item = token.charAt(1) + String.valueOf(token.charAt(0)); + return List.of(token, item); + } + List result = new ArrayList<>(); + for(int idx=0;idx=token.length()-1){ + return token.substring(0, idx); + } + return token.substring(0, idx) + token.substring(idx+1); + } + + List permutation(String token){ + if(token.length() < 2){ + return List.of(token); + } + if(token.length()==2){ + String item = token.charAt(1) + String.valueOf(token.charAt(0)); + return List.of(token, item); + } + List result = new ArrayList<>(); + for(int idx=0;idx