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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 丑小子799 中级黑马   /  2014-11-22 07:45  /  1021 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

第5题:原始字符串是"abc",打印得到下列所有组合情况:
"a" "b" "c"
"ab" "bc" "ca" "ba" "cb" "ac"
"abc" "acb" "bac" "bca" "cab" "cba"

9 个回复

倒序浏览
这题我去某公司笔试遇到过

static char[] chars="abc".toCharArray();
    public static void main(String[] args) {
        for(int i=0;i<chars.length;i++){
            //取得每一个字符
            List<Integer> list=new ArrayList<Integer>();
            list.add(i);
            play(list);
        }
    }
    //使用递归,每次加上列表中不存在的一个字符
    private static void play(List<Integer> list){
        print(list);
        for(int i=0;i<chars.length;i++){
            if(!list.contains(i)){
                List<Integer> temp=new ArrayList<Integer>(list);
                temp.add(i);
                play(temp);
            }
        }
    }
    //打印列表内容
    private static void print(List<Integer> list){
        for(Integer i:list)
            System.out.print(chars[i]+"");
        System.out.println();
    }
回复 使用道具 举报
一样啊 当时我这道题没做就交了,我得仔细看看楼上的
回复 使用道具 举报
一楼写的不错  研究一下
回复 使用道具 举报
还没到list,回去接着奋斗
回复 使用道具 举报
以前学习C语言做过穷举的题目,比如
abc
a,b,c,
ab,bc,ac,ba,cb,ca,aa,bb,cc,
和这个题目有不同的地方,这个题目没有重复字母。我这个字符串太长,效率就会很低。。。

  1. package myfirsttest;

  2. public class ExhaustionClass {

  3.         private int Num ;
  4.         int count=0;
  5.         public ExhaustionClass(int num) {
  6.                 super();
  7.                 Num = num;
  8.         }

  9.         public static void main(String[] args) {

  10.                 ExhaustionClass test = new ExhaustionClass(3);
  11.                
  12.             long startTime = System.currentTimeMillis();
  13.             
  14.             test.Method();
  15.             
  16.         long endTime = System.currentTimeMillis();
  17.         
  18.         System.out.println("一共有:"+test.count+"个组合");
  19.         System.out.println("运行时间:" + (endTime - startTime) + "毫秒");
  20.                
  21.         }

  22.         public void Method() {
  23.                 for (int i = 0; i < Num;)
  24.                         CreateString("", ++i); //穷举所有的
  25.         }

  26.         private void CreateString(String s, int end) {
  27.                 if (s.length() < end)                                 // 字母长度是否达到要求
  28.                         for (char c = 'a'; c < 'a'+ Num; c++) {
  29.                                
  30.                                 CreateString(s + c, end);         // 递归调用下一位原始字符串增加一个字母
  31.                         }
  32.                 else
  33.                         FilterPrint(s);                                //去掉重复字符后的字符串,如果字符串太长了,效率就不是很高

  34. //                        System.out.println(s);                 //没有去掉重复字母所有的穷举组合。

  35. //                        if(!s.matches("regex")){        //可用正则去掉重复字符,效率应该会高一点,目前还不会写。
  36. //                                System.out.println(s);
  37. //                        }

  38.         }

  39.         private  void FilterPrint(String s) {
  40.                
  41.                 boolean[] flags = new boolean[26];
  42.                
  43.                 char[] buf = s.toCharArray();
  44.                 for (char x : buf) {
  45.                         boolean flag = flags[x - 'a'];
  46.                         if (!flag)
  47.                                 flags[x - 'a'] = !flag;
  48.                         else
  49.                                 return;
  50.                 }
  51.                 count++;
  52.                 System.out.println(s);
  53.                
  54.         }

  55. }
复制代码


回复 使用道具 举报
mark 学了list在研究
回复 使用道具 举报
taany 中级黑马 2014-11-22 22:11:31
8#
感觉我还差好远!
回复 使用道具 举报
cczheng 发表于 2014-11-22 10:07
这题我去某公司笔试遇到过

static char[] chars="abc".toCharArray();

应该是list.contain(chars【i】)把
回复 使用道具 举报
感觉我还差的太远,好好努力。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马