黑马程序员技术交流社区

标题: 【字符串的全字符组合】,大家来讨论,谁的方法吊炸天 [打印本页]

作者: 黑马晓志    时间: 2014-2-22 21:59
标题: 【字符串的全字符组合】,大家来讨论,谁的方法吊炸天
编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符例如:原始字符串是"abc",打印得到下列所有组合情况"a" "b" "c" "ab" "bc" "ca" "ba" "cb" "ac""abc" "acb" "bac" "bca" "cab" "cba"
一开始没思路,苦思冥想一天,脑袋顿开,可以把它想象成数学中的(a+b+c)(a+b+c),下面是我的代码,你有不同的方法吗?

  1. package com.itheima;

  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.List;

  6. public class Test7 {

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

  8.                 String string="abcd";
  9.                 //将字符串转换成字符数组,再将字符数组,转化成只有一个字符组成的字符串的字符串数组,转化结果是这种形式{"a","b","c","d"}
  10.                 char[]chars=string.toCharArray();
  11.                 String[] strings =new String[chars.length];
  12.                 for(int i=0;i<chars.length;i++){
  13.                          strings[i]=chars[i]+"";
  14.                 }

  15.                 // 空list
  16.                 List<String> oldList = new ArrayList<String>();
  17.                 List<String> newList = new ArrayList<String>();

  18.                 for (int i = 0; i < strings.length; i++) {
  19.                         // 装入newList,清空oldList
  20.                         for (int j = 0; j < strings.length; j++) {
  21.                                 if (oldList.size() != 0) {
  22.                                         for (String str : oldList) {
  23.                                                 //判断字符串是否含有相同字符
  24.                                                 if (method(strings, strings[j] + str)) {
  25.                                                         newList.add(strings[j] + str);
  26.                                                         System.out.print(strings[j] + str + " ");
  27.                                                 }
  28.                                         }
  29.                                 } else {
  30.                                         newList.add(strings[j]);
  31.                                         System.out.print(strings[j] + " ");
  32.                                 }
  33.                         }
  34.                         System.out.println();
  35.                         oldList.clear();

  36.                         // 装入oldList,清空newList
  37.                         for (String str : newList) {
  38.                                 oldList.add(str);
  39.                         }
  40.                         newList.clear();
  41.                 }

  42.         }

  43.         //定义一个判断字符串中是否有重复的制定的字符的方法
  44.         private static boolean method(String[] strings, String str) {
  45.                 for (int i = 0; i < strings.length; i++) {
  46.                         int num = 0;
  47.                         char c[] = str.toCharArray();
  48.                         for (int j = 0; j < c.length; j++) {
  49.                                 if ((c[j] + "") .equals(strings[i]))
  50.                                         num++;
  51.                         }
  52.                         if (num > 1)
  53.                                 return false;

  54.                 }
  55.                 return true;
  56.         }
  57. }
复制代码

作者: flying    时间: 2014-2-23 02:08
  1. public class A {
  2.         public static void main(String[] args) throws Exception{
  3.                 //读入字符
  4.                 BufferedReader bufr=
  5.                                 new BufferedReader(new InputStreamReader(System.in));
  6.                 String line =null;
  7.                 while((line=bufr.readLine())!=null){
  8.                         List<String> list = new ArrayList<String>();
  9.                         addToList(line,list);
  10.                         List<String>list1 =fun(list);//得到字符串的所有组合
  11.                         for(int i=1;i<=list.size();i++){
  12.                                 show(list1,i);
  13.                                 }
  14.                 }
  15.         }
  16.         public static void addToList(String line, List<String> list) {
  17.                 for(int i=0;i<line.length();i++){
  18.                         list.add(line.substring(i,i+1));
  19.                 }
  20.         }
  21.         public static void show(List<String>list,int num){
  22.                 HashSet<String> hs = new HashSet<String>();
  23.                 for(String str:list){
  24.                         hs.add(str.substring(0,num));
  25.                 }
  26.                 for(String str:hs){
  27.                         System.out.print(str+" ");
  28.                 }
  29.                 System.out.println();
  30.         }
  31.         public static List<String> fun(List<String> list){
  32.                 List<String> list1 = new ArrayList<String>();
  33.                 if(list.size()==1)
  34.                         return list;
  35.                 else{
  36.                 for (int i = 0; i < list.size(); i++) {
  37.                         List<String> listbuf=copyList(list);
  38.                         StringBuilder s =null;
  39.                         listbuf.remove(i);
  40.                         List<String> list2 =fun(listbuf);
  41.                         for(int j=0;j<list2.size();j++){
  42.                                 s=new StringBuilder();
  43.                                 s.append(list.get(i)+list2.get(j));
  44.                                 list1.add(s.toString());
  45.                         }

  46.                 }
  47.                 return list1;
  48.                 }
  49.         }
  50.         public static List<String>copyList(List<String> list){
  51.                 List<String> list1 =new ArrayList<String>();
  52.                 for(String s:list){
  53.                         list1.add(s);
  54.                 }
  55.                 return list1;
  56.         }
  57. }
复制代码

作者: lachening    时间: 2014-3-4 01:44
本帖最后由 lachening 于 2014-3-4 01:52 编辑

纠结了好久,才想好在嵌套for循环和递归两者中到底要用哪种方法。感觉这道题在基础测试题中应该算是有点难度的题,搞了好久才写出来。。。


  1. package com.itheima;
  2. import java.util.*;

  3. public class Test5 {
  4.         static List<String> all = new ArrayList<String>();        //用于存放全部组合字符串。
  5.         static Iterator<String> iter;                             //操作List的迭代器。
  6.         
  7.         private static void per(StringBuffer strb, StringBuffer s) {        
  8.                                                           //核心函数,递归方式实现字符排列组合并注入List
  9.                 StringBuffer tmp = new StringBuffer();
  10.                 StringBuffer tmp_s = new StringBuffer();     //用于保护实参的临时变量
  11.                 if (strb.length() != 0) {                      //若已有串strb不是空串,直接注入List
  12.                         tmp.append(strb);
  13.                         all.add(new String(tmp.substring(0)));
  14.                 }
  15.                 if (s.length() == 1) {                        //若剩余串s中只包含一个字符
  16.                         tmp.append(s.charAt(0));         //则将s中唯一的字符append入tmp后注入List
  17.                         all.add(new String(tmp.substring(0)));
  18.                 } else {                               //若剩余串s中字符数大于1,则取出其中之一加入已有串,连同
  19.                         for(int i=0; i<s.length(); i++ ) {        //取后剩下的串作为参数传入该函数,递归调用。
  20.                                 tmp.replace(0, tmp.length(), strb.substring(0));                 //保护实参
  21.                                 tmp_s.replace(0, tmp_s.length(), s.substring(0));               //保护实参
  22.                                 Test5.per(tmp.append(s.charAt(i)), tmp_s.deleteCharAt(i));        
  23.                         }
  24.                 }
  25.         }
  26.         
  27.         private static void fun(String string) {             //字符串预处理及结果打印函数
  28.                 StringBuffer test = new StringBuffer(string);     //将输入的字符串转换成StingBuffer类型
  29.                 Test5.per(new StringBuffer(), test);
  30.                 for(int i=0; i<string.length(); i++) {           //每次循环,都只输出包含i个字符的串
  31.                         iter = all.iterator();
  32.                         while (iter.hasNext()) {
  33.                                 String tmp = iter.next();
  34.                                 if (tmp.length() == i + 1) {
  35.                                         System.out.print("\"" + tmp + "\"" + "  ");
  36.                                 }
  37.                         }
  38.                         System.out.print("\n");                                        //输出一组后进行换行
  39.                 }
  40.         }

  41.         public static void main(String[] args) {
  42.                 String testString = "abc";
  43.                 Test5.fun(testString);
  44.         }
  45. }
复制代码








欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2