A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© awkflf11 中级黑马   /  2014-5-4 14:47  /  1872 人查看  /  20 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文







编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,

例如:原始字符串是"abc",

打印得到下列所有组合情况:

"a" "b" "c"
"ab" "bc" "ca" "ba" "cb" "ac"
"abc" "acb" "bac" "bca" "cab" "cba"

20 个回复

正序浏览
  1. public class Test8 {

  2.         public static void main(String[] args)
  3.         {
  4.                 //建立需要排列的字符串
  5.         String str = "abc";
  6.         
  7.         //建立一个集合用于存储所排列的字符串,因为不要重合的,所以用Set集合
  8.         TreeSet<String> tsSet=new TreeSet<String>();
  9.         
  10.         //将字符串转换为字符数组传入要进行递归的方法中。
  11.         permutation(tsSet,str.toCharArray(),0);
  12.         
  13.         //在递归完成后,集合中已经有了所有的排列,打印。
  14.         System.out.println(tsSet.toString());
  15.     }
  16.        
  17.     public static void permutation(Set<String> s,char[] str, int x) {
  18.             //定义递归的完结条件
  19.         if (x >= str.length)
  20.             return;
  21.         //用于对递归中取到的排列进行存储
  22.         if (x == str.length - 1) {
  23.             s.add(new String(str));
  24.         } else {
  25.                 //建立一个循环,进行循环读取
  26.             for (int y = x; y < str.length; y++) {
  27.                    
  28.                     //在循环到第二次的时候,开始转换首字母,以便递归
  29.                 char temp = str[y];
  30.                 str[y] = str[x];
  31.                 str[x] = temp;
  32.                
  33.                 //将变换了首字母的字符数组传入,进行递归,取得与与原字符串相同长度的所有排列
  34.                 permutation(s,str, x + 1);
  35.                
  36.                 //去掉首字母进行递归,取得长度减一的字符串的所有排列,
  37.                 //因为不断递归,所以最终会取到单一的字符为止,如此则取到了所有的排列
  38.                 permutation(s,Arrays.copyOfRange(str, 1, str.length),0);
  39.                
  40.                 //将字符数组变回原样,保证每次递归时,都会变换首字母(x和y转换)
  41.                 temp = str[y];
  42.                 str[y] = str[x];
  43.                 str[x] = temp;
  44.             }
  45.         }
  46.     }
  47.                
  48. }
复制代码

编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,例如:
原始字符串是"abc",打印得到下列所有组合情况:
"a" "b" "c"
"ab" "bc" "ca" "ba" "cb" "ac"
"abc" "acb" "bac" "bca" "cab" "cba"

[a, ab, abc, ac, acb, b, ba, bac, bc, bca, c, ca, cab, cb, cba]

思考:可以首先将以a开头的所有排列找出,然后在找出以b开头的排列...如此可以用到递归的方法。


点评

学习了,多谢  发表于 2014-5-5 21:02
回复 使用道具 举报
看看递归 就会明白了 其他的自己领悟去  这样对你才有好处
回复 使用道具 举报
  1. public class Test_02 {
  2.         public static void main(String[] args) {
  3.                 char ch[] = { 'a', 'b', 'c' };
  4.                 for (int i = 0; i < ch.length; i++) {
  5.                         System.out.print("" + ch[i] + '\t');
  6.                         for (int j = 0; j < ch.length; j++) {
  7.                                 if (i != j) {
  8.                                         System.out.print("" + ch[i] + ch[j] + "\t");
  9.                                 }
  10.                                 for (int k = 0; k < ch.length; k++) {
  11.                                         if (i != j && i != k && j != k) {
  12.                                                 System.out.print(ch[i] + "" + ch[j] + ch[k] + "\t");
  13.                                         }
  14.                                 }
  15.                         }
  16.                 }
  17.         }
  18. }
复制代码

点评

学习呀,,厉害  发表于 2014-5-5 21:01
回复 使用道具 举报
李维奥 发表于 2014-5-4 21:17
public class StringDemo {

        /**

貌似不对啊。
回复 使用道具 举报
李维奥 发表于 2014-5-4 21:17
public class StringDemo {

        /**

首先多谢了,
但答案不完全对
如果是abc

结果是
a  ab  abc  
  b  bc  
    c  
少 bca,bac, cab,cab
回复 使用道具 举报
李维奥 发表于 2014-5-4 21:17
public class StringDemo {

        /**

不好意思我看错题了。。。。
回复 使用道具 举报


public class StringDemo {

        /**
         * @param args
         */
        public static void main(String[] args) {
                String s = "abcdefg";
                char[] ch = s.toCharArray();
                turn(ch);

        }

        public static void turn(char[] ch) {
                int len = ch.length;
                for (int k = 0; k < len; k++) {
                        for (int i = 1; i <= len; i++) {
                                for (int j = k; j < i; j++) {
                                        System.out.print(ch[j]);
                                }
                                System.out.print("  ");
                        }
                        System.out.println();
                }

        }
}
做出来了但是思维有些混乱,就没有写注释
回复 使用道具 举报
import java.util.*;
public class Test{
         static int n,count = 0;
         static char[] array = { 'a', 'b', 'c' };
         static LinkedList <char[]> list = new LinkedList<char[]> ();
         static int[] indexs = new int[3];  
         static int len = array.length;

         public static void main(String[] args){
                                 getSub ();

                 for ( char[] cs : list )     //遍历list
                 {        
                         //n++;
                        // if(n%3!=0)

                    System.out.println(cs);
                        // else System.out.println(cs);
               }
             }

             private static LinkedList<char[]> getSub ()      //获取
             {
                 while (count <= len){
                     recursionSub (-1);
                     count++;
                 }
                 return list;
             }

             private static LinkedList <char[]> recursionSub ( int start )
             {
                             start++;

                             if (start > count - 1)
                                     {
                                     return null;
                                     }
                             for ( indexs[start] = 0; indexs[start] < len; indexs[start]++ )
                                     {
                                             recursionSub (start);
                                             if (start == count - 1)
                                                     {
                                                             char[] temp = new char[count];
                                                             for ( int i = count - 1; i >= 0; i-- )
                                                                     {
                                                                             temp[start - i] = array[indexs[start - i]];
                                                                     }
                                                             boolean flag = true;
                                                             for ( int i = 0; i < temp.length; i++ )
                                                                     {
                                                                             for ( int j = i+1; j < temp.length; j++ )
                                                                                     {
                                                                                             if (temp[i] == temp[j])
                                                                                                     {
                                                                                                             flag = false;
                                                                                                             break;
                                                                                                     }
                                                                                     }
                                                                     }
                                                             if (flag)
                                                             {
                                                                     list.add (temp);
                                                             }
                                                     }        
                                     }
                             return list;
         }

}

可以共同探讨一下,这个也不是很完美

回复 使用道具 举报
Lin0411 发表于 2014-5-4 17:13
它是要输出所有排列。小子愚钝,能够给个完整代码。

:L看错了,以为要子串,我错了。
回复 使用道具 举报
skill20 发表于 2014-5-4 16:50
咋不对了?你先 给 S打印下嘛,看看能不能取出子串,可与的话弄个返回值就哦了。 ...

它是要输出所有排列。小子愚钝,能够给个完整代码。
回复 使用道具 举报
  1. 这下应该完整了。
  2. public class test
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 showPermString("abcd");
  7.         }
  8.        
  9.        
  10.        
  11.         public static void showPermString(String str)
  12.         {
  13.                 for(int i=1; i<=str.length(); i++)
  14.                 {
  15.                         List<String> list = new ArrayList<String>();
  16.                         perm(list, str.toCharArray(), 0, i);
  17.                         System.out.println(list);
  18.                 }
  19.         }

  20.        
  21.        
  22.        
  23.         public static void perm(List<String> list,char[] chs,int k,int len)
  24.         {
  25.                 if(k == chs.length)
  26.                 {
  27.                         String string = String.valueOf(chs, 0, len);
  28.                         if(!list.contains(string))
  29.                                 list.add(string);
  30.                 }
  31.                 else
  32.                 {
  33.                         for(int i=k; i<chs.length; i++)
  34.                         {
  35.                                 swap(chs, i, k);
  36.                                 perm(list,chs, k+1,len);
  37.                                 swap(chs, i, k);
  38.                         }
  39.                 }
  40.                
  41.         }
  42.        
  43.         public static void swap(char[] chs,int i,int j)
  44.         {
  45.                 char tem = chs[i];
  46.                 chs[i] = chs[j];
  47.                 chs[j] = tem;
  48.         }

  49. }
复制代码

点评

厉害,,,学习了  发表于 2014-5-5 20:50
回复 使用道具 举报
Lin0411 发表于 2014-5-4 16:42
如果是输出s的话貌似不对啊

咋不对了?你先 给 S打印下嘛,看看能不能取出子串,可与的话弄个返回值就哦了。
回复 使用道具 举报

如果是输出s的话貌似不对啊
回复 使用道具 举报

试试嘛。
回复 使用道具 举报
  1. 这个函数只给出了打出固定字符串的所有排列。

  2. public class test
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 perm("abc".toCharArray(), 0);
  7.         }
  8.        
  9.         public static void perm(char[] chs,int k)
  10.         {
  11.                 if(k == chs.length)
  12.                 {
  13.                         for(int i=0; i<chs.length; i++)
  14.                         {
  15.                                 System.out.print(chs[i]);
  16.                         }
  17.                         System.out.print("  ");
  18.                        
  19.                 }
  20.                 else
  21.                 {
  22.                         for(int i=k; i<chs.length; i++)
  23.                         {
  24.                                 swap(chs, i, k);
  25.                                 perm(chs, k+1);
  26.                                 swap(chs, i, k);
  27.                         }
  28.                 }
  29.                
  30.         }
  31.        
  32.         public static void swap(char[] chs,int i,int j)
  33.         {
  34.                 char tem = chs[i];
  35.                 chs[i] = chs[j];
  36.                 chs[j] = tem;
  37.         }
  38.        
  39.        

  40. }
复制代码
回复 使用道具 举报
skill20 发表于 2014-5-4 15:16
//取子串就可以了。

这也行?
回复 使用道具 举报
本帖最后由 skill20 于 2014-5-4 16:37 编辑

//取子串就可以了。
  1. <div class="blockcode"><blockquote>String st = "asdsfds";
  2.                 for (int a = 0; a < st.length() ; a++)
  3.                 {
  4.                         for (int x = 0 ,y = st.length()-a; y != st.length() + 1 ; x++ ,y++ )
  5.                         {
  6.                                 String s = st.substring(x,y);
  7.                         }
  8.                 }
复制代码


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马