比如我有一个数“abc”,
列出为3为数的组合
abc
acb
bac
bca
cab
cba四位数就是"abcd"就列出4*3*2*1=24个数,
如果是“abcde”就是列出5*4*3*2*1=120个数,我现在已经做出来了,但是效率很低,到6位数时就出现Exception in thread "main" java.lang.OutOfMemoryError: Java heap space的异常,大家有什么好方法么?
列出为3为数的组合
abc
acb
bac
bca
cab
cba四位数就是"abcd"就列出4*3*2*1=24个数,
如果是“abcde”就是列出5*4*3*2*1=120个数,我现在已经做出来了,但是效率很低,到6位数时就出现Exception in thread "main" java.lang.OutOfMemoryError: Java heap space的异常,大家有什么好方法么?
解决方案 »
- 今天突然想到了一个问题,关于java如何实现对文件分配表的操作
- 使用插件!
- 请问这是什么问题?
- javax.servlet.ServletException: oracle.jdbc.driver.ScrollableResultSet.getBlob(Ljava/lang/String;)Ljava/sql/Blob;这是什么错误?
- 关于String类的基本问题
- 得到系统目录!!!
- 对象引用的类型转换??
- 有关于Swing中文本域(JTextArea)控件显示问题
- 回答问题的人太少了,如果是散分呢?
- 【救命】java.io.NotSerializableException: wfpattern.LineStore
- 如何实现JTable的多列排序?就是点多个列表头能实现多列排序的
- 各位前辈好,请问如何实现Graphics2D图形的平移拖动,缩放及裁剪?
/**只支持到9位
*/
public class Main { public static void main(String[] args) throws Exception {
String str = "123456789";
java.util.Vector<char[]> vc = getAll(str.toCharArray());
System.out.println("");
for(char[] c:vc){
System.out.println(new String(c));
}
}
static public java.util.Vector<char[]> getAll(char []tem) {
java.util.Vector<char[]> re = new java.util.Vector();
/////////////////
re.add(tem);
char bu;
for(int i=1,im=tem.length;i<im;i++){
for(int k=0,km=re.size();k<km;k++){
char[]bt = re.get(k);
for(int j=0;j<i;j++){
char[]t = new char[bt.length];
System.arraycopy(bt, 0, t, 0, bt.length);
bu=t[j];
t[j]=t[i];
t[i]=bu;
re.add(t);
}
}
}
return re;
}
}
我说LZ,别人回答你的问题,错了就错了,指出来就可以了吗。干嘛如此没素质呀!人家回答错了,就要被骂,你这种LZ我不伺候,谁爱回答谁回答去。真让人生气: 别出来搞IT了,回去吧,连宽容的态度都没有,难怪有人说IT人的素质差。看看LZ这种就人就行了。
# import java.util.ArrayList;
# import java.util.Arrays;
#
# public class Permutation {
#
# /**
# * @param args
# */
# public static void main(String[] args) {
# String[] strs=permutation("1234");
# System.out.println(strs.length);
#
# }
#
# public static String[] permutation(String str) {
# ArrayList<String> myList = new ArrayList<String>();
# char[] strChars=str.toCharArray();
# char temp;
# long times=1;
# int pos=strChars.length-2;
# int increment=-1;
# for(int i=1;i<strChars.length+1;i++){
# times*=i;
# }
# for(int i=1;i<times;i++){
# temp=strChars[pos];
# strChars[pos]=strChars[pos+1];
# strChars[pos+1]=temp;
# myList.add(new String(strChars));
# pos+=increment;
# if(pos==-1){
# increment=1;
# pos=0;
# temp=strChars[strChars.length-2];
# strChars[strChars.length-2]=strChars[strChars.length-1];
# strChars[strChars.length-1]=temp;
# myList.add(new String(strChars));
# i++;
# }else if(pos==strChars.length-1){
# increment=-1;
# pos=strChars.length-2;
# temp=strChars[0];
# strChars[0]=strChars[1];
# strChars[1]=temp;
# myList.add(new String(strChars));
# i++;
# }
# }
# return myList.toArray(new String[0]);
# }
#
# }
public class Main {
public static void main(String[] args) {
String s = "abc";
char[] c = s.toCharArray();
new Main().zuhe(c, c.length, 0);
}
private void zuhe(char[] array, int n, int k) {
if (n == k) {
String str = new String(array);
System.out.println(str);
} else {
for (int i = k; i < n; i++) {
swap(array, k, i);
zuhe(array, n, k + 1);
swap(array, i, k);
}
}
} private void swap(char[] a, int x, int y) {
char temp = a[x];
a[x] = a[y];
a[y] = temp;
}}abc
acb
bac
bca
cba
cab