全排列,精简_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 全排列,精简

全排列,精简

 2013/7/10 3:35:45  1140566087  程序员俱乐部  我要评论(0)
  • 摘要:package全排列;importjava.util.ArrayList;publicclassMain{privatestaticArrayList<String>a=newArrayList<String>();publicstaticvoidmain(String[]arg){String[]ss={"1","2","3","4","5","6","7","8","9"};longbegin=System.currentTimeMillis();pailie(0
  • 标签:
class="java" name="code">package 全排列;

import java.util.ArrayList;

public class Main {
	private static ArrayList<String> a = new ArrayList<String>();
	
	public static void main(String[] arg){
		String[] ss = {"1","2","3","4","5","6","7","8","9"};
		long begin = System.currentTimeMillis();
		pailie(0,ss.length-1,ss);
		
		for(int i=0;i<a.size();i++){
			System.out.println(a.get(i)+" ");
		}
		
		long end = System.currentTimeMillis();
		System.out.println((end-begin)/1000f+"秒");
	}
	
	public static void pailie(int start,int end,String[] ss){
		if(start==end){
			String s="";
			for(int i=0;i<ss.length;i++){
				s+=ss[i];
			}
			a.add(s);
		}
		
		for(int i=start;i<=end;i++){
			
			String index=ss[start];
			ss[start]=ss[i];
			ss[i]=index;
			
			pailie(start+1,end,ss);
			
			index=ss[start];
			ss[start]=ss[i];
			ss[i]=index;
		}
	}

}
  • 相关文章
发表评论
用户名: 匿名