本帖最后由 youcyou 于 2014-5-14 17:17 编辑
编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符
例如:
原始字符串是"abc",打印得到下列所有组合情况
"a" "b" "c"
"ab" "bc" "ca" "ba" "cb" "ac"
"abc" "acb" "bac" "bca" "cab" "cba"
还是这个老问题,下面是我写的代码,求大神帮忙看一下- import java.util.ArrayList;
- public class Test5 {
-
- public static void main(String args[])
- {
- String s="abc";
- combinations(s);
- }
- private static void combinations(String s) {
- combinations("",s);
-
- }
- private static void combinations(String st,String s) {
- ArrayList<Character> al =new ArrayList<Character>();
- char[] ch = s.toCharArray();
- for(char c:ch)
- {
- al.add(c);
- }
- getString(al,st);
- }
- private static void getString(ArrayList<Character> al,String st)
- {
- for(int index=0;index<al.size();index++)
- {
- String str=st+al.get(index);
- System.out.println(str);
-
- al.remove(index);
-
- getString(al,str);
- }
- }
- }
复制代码 |