import java.util.*; /* * Permutations without Dups: Write a method to compute all permutations of a string of unique characters. * */ public class Main { public static void main(String[] args) { System.out.println("Hello World!"); String str = "abcdefg"; List<String> result = new ArrayList<String>(); StringBuilder sb = new StringBuilder(); permutation(result, str,sb); System.out.println(result); } public static void permutation(List<String> result, String str, StringBuilder sb) { if(sb.length() == str.length()) { result.add(sb.toString()); return; } for(int i = 0; i < str.length(); i++) { if(contains(sb, str.charAt(i))) { continue; } sb.append(str.charAt(i)); permutation(result, str, sb); sb.deleteCharAt(sb.length() - 1); } } public static boolean contains(StringBuilder sb, char ch) { for(int i = 0; i < sb.length(); i++) { if(sb.charAt(i) == ch) { return true; } } return false; } }
Permutations without Dups java 走地牙CTCI 8.7
最新推荐文章于 2020-04-18 12:42:57 发布