public class Main {
public static void main(String[] args) {
combination("abc");
permutation("abc".toCharArray(), 0, "abc".length()-1);
}
public static void combination(String str) {
if(str==null || str.length()<=0) return;
int len = str.length();
int n = 1<<len;
for (int i = 1; i < n; i++) {
for (int j = 0; j < len; j++) {
if(((1<<j) & i)!=0) {
System.out.print(str.charAt(j));
}
}
System.out.println();
}
}
public static void permutation(char[] arr, int first, int end) {
if(first == end) {
System.out.println(arr);
}
else {
for (int i = first; i <= end; i++) {
swap(arr, first, i);
permutation(arr, first + 1, end);
swap(arr, first, i);
}
}
}
private static void swap(char[]arr, int i, int j) {
char tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
字符串全组合与全排列
最新推荐文章于 2019-06-02 16:57:00 发布