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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

这是一个算法题:比如输出abc就输出abc acb bca bac等不同组合,给整晕了,大神速来
  1. public static void main(String args[]){
  2.                 System.out.println(list("abc",""));
  3.         }
  4.          public static List<String> list(String base,String buff){   
  5.                 List<String> result = new ArrayList<String>();//存放结果信息。   
  6.                 System.out.println(base);//刚进来是abc没错
  7.                 if(base.length()<=0){
  8.                         System.out.println(base.length());//真的是0 这里一个搞不懂
  9.                     result.add(buff);
  10.                     System.out.println(result);
  11.                 }   
  12.                 for(int i=0;i<base.length();i++){   //还有这里for循环的base.length()和上面感觉又冲突了
  13.                     List<String> temp = list(new StringBuilder(base).deleteCharAt(i).toString(),buff+base.charAt(i));
  14.                     System.out.println(new StringBuilder(base).deleteCharAt(i).toString()+":"+buff+":"+base.charAt(i));
  15.                     //将整个temp放入result
  16.                     result.addAll(temp);   
  17.                       
  18.                 }   
  19.                 return result;   
  20.             }   
复制代码



13 个回复

倒序浏览
楼主,请先了解下面三个方法再看程序:
String类中:
int length()
          返回此字符串的长度。
StringBuffer类中:
StringBuffer deleteCharAt(int index)
          移除这个序列中最后一个元素。
String toString()
          返回表示这个序列的字符串。
回复 使用道具 举报
感觉也不是很难啊
回复 使用道具 举报
pvbj0314 发表于 2015-5-29 09:42
楼主,请先了解下面三个方法再看程序:
String类中:
int length()

肯定知道啊。
回复 使用道具 举报
mmakun 发表于 2015-5-29 10:04
感觉也不是很难啊

也许是给绕晕了把,昨天一哥们给我的,说着说着我也晕了,来个细致解释?
回复 使用道具 举报
是求全排列问题吗?用递归试试。
  1. public class Test {

  2.         /**
  3.          * @param args
  4.          */
  5.         static String str="abc";
  6.         public static void main(String[] args) {
  7.                 // TODO Auto-generated method stub
  8.                 sortmethod(0,new String());

  9.         }
  10.         public static void sortmethod(int len, String temp) {
  11.                 // TODO Auto-generated method stub
  12.                 if(len<str.length()){
  13.                         for(int i=0;i<str.length();i++){
  14.                                 if(!temp.contains(str.substring(i,i+1))){
  15.                                         System.out.print(temp+str.substring(i, i+1)+" ");
  16.                                         sortmethod(len+1, new String(temp+str.substring(i,i+1)));
  17.                                 }
  18.                         }
  19.                 }
  20.         }
  21. }
复制代码

打印结果:a ab abc ac acb b ba bac bc bca c ca cab cb cba
所有组合都有了。
回复 使用道具 举报
KK要有光 发表于 2015-5-29 10:43
是求全排列问题吗?用递归试试。
打印结果:a ab abc ac acb b ba bac bc bca c ca cab cb cba
所有组合都 ...

大神!!赞啊
回复 使用道具 举报
同样的问题得到解决    谢谢大神
回复 使用道具 举报
灯火通明 来自手机 中级黑马 2015-5-29 12:27:12
9#
不错的题目
回复 使用道具 举报
夜愿Relax 来自手机 中级黑马 2015-5-29 12:31:31
10#
学习了,赞
回复 使用道具 举报
膜拜{:3_46:}{:3_53:{:3_53:}
回复 使用道具 举报
学习了~~~                                                        
回复 使用道具 举报
膜拜大神!谢谢各位!
回复 使用道具 举报
学习了。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马