黑马程序员技术交流社区
标题:
看看下面的代码,能不能优化
[打印本页]
作者:
pegasus
时间:
2015-11-25 09:12
标题:
看看下面的代码,能不能优化
/**
* 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,例如: 原始字符串是"abc",打印得到下列所有组合情况: "a" "b" "c" "ab"
* "bc" "ca" "ba" "cb" "ac" "abc" "acb" "bac" "bca" "cab" "cba"
*/
public class CharacterAssemble {
public static void main(String[] args) throws IOException {
System.out.println("请输入一个字符串:");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
String str = bufferedReader.readLine().trim();
String[] strs = str.split("");
List<String> temp = new ArrayList<String>();
List<String> list = new ArrayList<String>();
List<String> listAll = new ArrayList<String>();
int len = strs.length;
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if (!temp.isEmpty()) {
for (int k = 0; k < temp.size(); k++) {
// 判断是否有重复
if (!temp.get(k).contains(strs[j]))
list.add(temp.get(k) + strs[j]);
}
} else {
list.add(strs[j]);
}
}
listAll.addAll(list);
// 将list的元素复制给temp
temp.addAll(list);
// list清空
list.clear();
}
System.out.println(listAll);
}
}
复制代码
作者:
Aaron_wang
时间:
2015-11-25 22:10
获取字符串数组的方法太麻烦,另外,你确定你的程序是对的?
看下这个:
http://bbs.itheima.com/thread-263226-1-1.html
作者:
小布丁当
时间:
2015-11-25 22:24
看着 有点蒙圈圈
作者:
小布丁当
时间:
2015-11-25 22:25
!!!!!!!!!!!!!!!!!!!!!!!!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2