java求一个数组的所有子序列,连续子序列,所有排列,所有组合

比如:数组a:int[] a={1,2,3};

那么它的所有子序列有:{1},{2},{1,2},{3},{1,3},{2,3},{1,2,3}

它的连续子序列有:{1},{1,2},{1,2,3},{2},{2,3},{3}

它的所有排列:{1,2,3},{1,3,2},{2,1,3},{3,1,2},{2,3,1},{3,2,1}

1,它的所有子序列:

public class Main{
	
	public static void main(String args[]) {
		int[] a= {1,2,3};
		ArrayList<ArrayList<Integer>> list=getSubArray(a,a.length);
		//输出列表:
		for (int i=0;i<list.size();i++) {
			ArrayList<Integer> mList=list.get(i);
			for (int j=0;j<mList.size();j++) {
				System.out.print(mList.get(j)+" ");
			}
			//换行
			System.out.println();
			
		}
	}
	
	private static ArrayList<ArrayList<Integer>> getSubArray(int[] arr,int length) {
		ArrayList<ArrayList<Integer>> bList=new ArrayList<>();
		int mark=0;
		int nEnd=1<<length;
		boolean bNullset=false;
		for (mark=0;mark<nEnd;mark++) {
			ArrayList<Integer> aList=new ArrayList<>();
			bNullset=true;
			for (int i=0;i<length;i++) {
				if (((1<<i)&mark)!=0) {
					bNullset=false;
					aList.add(arr[i]);
				}
			}
			bList.add(aList);
		}
		return bList;
	}
}

输出:

1 
2 
1 2 
3 
1 3 
2 3 
1 2 3 

2,数组所有的连续子序列:


public class Main{
	
	public static void main(String args[]) {
		String str="123";
		allContinuousSubArray(str);
		
	}
	
	private static void allContinuousSubArray(String str) {
		int i,j,k;
		int num=str.length();
		for (i=0;i<num;i++) {
			for (j=i;j<num;j++) {
				for (k=i;k<=j;k++) {
					System.out.print(str.charAt(k)+" ");
				}
				System.out.print(" ");
			}
			System.out.println();
		}
	}
}

输出:

1  1 2  1 2 3  
2  2 3  
3  

对于这个程序,可以输入字符串数字,然后通过Integer.praseInt()变换得到整数,这个程序也可以取得字符串的连续子序列。

3,对数组所有元素进行排序:



public class Main{
	
	public static void main(String args[]) {
		
		String str="123";
		permutation(str);
	}
	
	private static void permutation(String str) {
		if (str==null) {
			return ;
		}
		
		permutation(str.toCharArray(),0);
	}
	

	private static void permutation(char[] chars, int pos) {
		if(pos == chars.length - 1){
			System.out.println(chars);
		}

		for(int i = pos; i < chars.length; i++){

			//首部字符和它后面的字符(包括自己)进行交换

			char temp = chars[i];
			chars[i] = chars[pos];
			chars[pos] = temp;

			//递归求后面的字符的排列

			permutation(chars, pos+1);
			
			//由于前面交换了一下,所以chs的内容改变了,我们要还原回来
			temp = chars[i];
			chars[i] = chars[pos];
			chars[pos] = temp;

		}
	}
}

输出:

123
132
213
231
321
312

组合序列:


/** 1、数组元素的全组合 */

public void combination(char[] chars) {

	char[] subchars = new char[chars.length]; // 存储子组合数据的数组

	// 全组合问题就是所有元素(记为n)中选1个元素的组合, 加上选2个元素的组合...加上选n个元素的组合的和

	for (int i = 0; i < chars.length; ++i) {

		final int m = i + 1;

		combination(chars, chars.length, m, subchars, m);

	}

}


/**

 * n个元素选m个元素的组合问题的实现. 原理如下: 从后往前选取, 选定位置i后, 再在前i-1个里面选取m-1个. 如: 1, 2, 3, 4,

 * 5 中选取3个元素. 1) 选取5后, 再在前4个里面选取2个, 而前4个里面选取2个又是一个子问题, 递归即可; 2) 如果不包含5,

 * 直接选定4, 那么再在前3个里面选取2个, 而前三个里面选取2个又是一个子问题, 递归即可; 3) 如果也不包含4, 直接选取3,

 * 那么再在前2个里面选取2个, 刚好只有两个. 纵向看, 1与2与3刚好是一个for循环, 初值为5, 终值为m. 横向看,

 * 该问题为一个前i-1个中选m-1的递归.

 */

public void combination(char[] chars, int n, int m, char[] subchars,

		int subn) {

	if (m == 0) { // 出口

		for (int i = 0; i < subn; ++i) {

			System.out.print(subchars[i]);

		}

		System.out.println();

	} else {

		for (int i = n; i >= m; --i) { // 从后往前依次选定一个

			subchars[m - 1] = chars[i - 1]; // 选定一个后

			combination(chars, i - 1, m - 1, subchars, subn); // 从前i-1个里面选取m-1个进行递归

		}

	}

}

发布了75 篇原创文章 · 获赞 31 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/yaoyaoyao_123/article/details/100916422
今日推荐