-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combination.java
42 lines (35 loc) · 1.03 KB
/
Combination.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package hehaitao;
import java.util.Vector;
/**
* 题目:输出字符串的所有组合,本题的例子是abcd。
* @author hongbin.gao
*
*/
public class Combination {
public static void main(String[] args){
String str = "abcd";
int length = str.length();
Vector<Character> vec = new Vector<Character>();
for(int i=1;i<=length;i++){
combination(str.toCharArray(),0,length-1,i,vec);
}
}
public static void combination(char[] c, int low, int high, int length,Vector<Character> vec){
if(c==null) //如果字符数组为空
return ;
if(length==0){
for(int i=0;i<vec.size();i++)
System.out.print(vec.elementAt(i));
System.out.println();
return ;
}
if(low>high || (high-low+1)<length) //当出现不可能的情况时及时停止
return ;
else{
combination(c,low+1,high,length,vec); //low位置的字符不算
vec.add(c[low]);//将low位置的字符加入
combination(c,low+1,high,length-1,vec);
vec.remove(vec.size()-1);
}
}
}