java组合算法(非递归)

package net.kitbox.util;

import java.util.Iterator;
import java.util.LinkedList;

@SuppressWarnings("rawtypes")
public class CombineIterator implements Iterator {
	//源数据
	private int[] source;
	//结果数组大小
	private int resultSize;
	//结果数组个数
	private int size;
	//当前元素索引
	private int[] index;
	//当前序列索引
	private int offset = 0;

	public CombineIterator(int[] source , int resultSize){
		if(source == null) throw new NullPointerException();
		int n = source.length;
		if(n < resultSize || resultSize <= 0) throw new IllegalArgumentException("size : " + n + ", m : " + resultSize);
		this.source = source;
		this.size = clacSize(n, resultSize);
		this.resultSize = resultSize;
		this.index = new int[resultSize];
		for(int i=0;i<resultSize;i++){
			this.index[i] = i;
		}
		this.index[resultSize-1] -= 1;
	}

	/**
	 * n中选m
	 * @param n
	 * @param m
	 * @return
	 */
	private int clacSize(int n ,int m){
		return Factorial.factorial(n-m+1,n).divide(Factorial.factorial(m)).intValue();
	}

	/**
	 * 获取迭代器内元素总数
	 * @return
	 */
	public int size(){
		return size;
	}

	public boolean hasNext() {
		return offset < size;
	}

	@Override
	public int[] next() {
		int idx = resultSize - 1;
		int n = source.length;
		if(index[idx] < n - 1){
			index[idx] += 1;
		}else{
			idx -= 1;
			while(idx > 0 && index[idx] == index[idx + 1] -1){
				idx -= 1;
			}
			index[idx] += 1;
			for(int i = idx + 1;i<= resultSize -1;i++){
				index[i] = index[idx] + (i - idx);
			}
		}
		int[] result = new int[resultSize];
		for(int i=0;i<=resultSize-1;i++){
			result[i] = source[index[i]];
		}
		offset++;
		return result;
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}

	public static void main(String[] args) {
		long t1 = System.currentTimeMillis();
		int[] source = new int[33];
		for(int i= 1;i<=33;i++){
			source[i-1] = i;
		}
		int resultSize = 6;
		CombineIterator itr = new CombineIterator(source, resultSize);
		//LinkedList<int[]> list = new LinkedList<int[]>();
 		while(itr.hasNext()){
			int [] a = itr.next();
			//list.add(a);
		}
		long t2 = System.currentTimeMillis();
		System.out.println("耗时:" + (t2 - t1));//44~48ms
		//System.out.println("总计:" + list.size());
	}
}

package net.kitbox.util;

import java.math.BigInteger;
/**
 * 阶乘计算
 * @author kitbox.net
 *
 */
public class Factorial {

	/**
	 * 计算1到n的阶乘,0! = 1
	 * @param n
	 * @return	1 * 2 *3 * ... * (n - 1) * n
	 */
	public static BigInteger factorial(int n){
		if(n == 0) return new BigInteger("1");
		return factorial(1,n);
	}

	/**
	 * 计算start到end的阶乘,不支持0参数
	 * @param start		起始数(包含)
	 * @param end		终止数(包含)
	 * @return	start * (start + 1) * ... *(end - 1) * end
	 */
	public static BigInteger factorial(int start,int end){
		if(start <= 0 || end < start) throw new IllegalArgumentException("start : " + start + ",end : " + end);
		BigInteger result = new BigInteger("1");
		for(int i = start;i <= end; i++){
			result = result.multiply(new BigInteger(i + ""));
		}
		return result;
	}
}

java组合算法(非递归)

时间: 2024-08-05 05:45:02

java组合算法(非递归)的相关文章

8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,循环控制及其优化

上两篇博客 8皇后以及N皇后算法探究,回溯算法的JAVA实现,递归方案 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,数据结构“栈”实现 研究了递归方法实现回溯,解决N皇后问题,下面我们来探讨一下非递归方案 实验结果令人还是有些失望,原来非递归方案的性能并不比递归方案性能高 代码如下: package com.newflypig.eightqueen; import java.util.Date; /** * 使用循环控制来实现回溯,解决N皇后 * @author [email pr

组合问题非递归

组合问题非递归形式: 例如有5个数,选其中3个数 ,将其模拟成 1 1 1 0 0 1 1 0 1 0 1 1 0 0 1 1 0 1 1 0 1 0 0 1 1 0 1 1 1 0 .......... 需要将每次交换后的1结合在一起. 1 #include<cstdio> 2 int b[25]={ 3 0 4 } ; 5 void Print(int len,int r) 6 { 7 int temp=0; 8 for(int i=0;i<len;i++) 9 { 10 if(b[

【Java】 归并排序的非递归实现

归并排序可以采用递归方法(见:归并排序),但递归方法会消耗深度位O(longn)的栈空间,使用归并排序时,应该尽量使用非递归方法.本文实现了java版的非递归归并排序. 更多:数据结构与算法合集 思路分析 递归排序的核心是merge(int[] arr, int start, int mid, int end)函数,讲[start~mid-1]和[mid~end]部分的数据合并,递归代码是使用递归得到mid,一步步分解数组. 非递归时,我们直接定义要合并的小数组长度从1开始,在较小的长度数组都合

【LeetCode-面试算法经典-Java实现】【144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)】

[144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution

【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】

[145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive soluti

自己写算法---java的堆的非递归遍历

import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { Scanner cin = new Scanner(System.in); //ArrayList<String> list = new ArrayList<String>(); //Scanner scan = new Scanner(System.in); //获取键盘输入的另一种格

算法笔记_013:汉诺塔问题(Java递归法和非递归法)

目录 1 问题描述 2 解决方案  2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus is possible for using animation. e.g. if n = 2 ; A→B ; A→C ; B→C; if n = 3; A→C ; A→B ; C→B ; A→C ; B→A ; B→C ; A→C; 翻译:模拟汉诺塔问题的移动规则:获得奖励的移动方法还是有可能的.

Java数据结构和算法之递归

四.递归 递归是函数调用自身的一种特殊的编程技术,其应用主要在以下几个方面:   阶乘 在java当中的基本形式是: Public  void  mothed(int n){//当满足某条件时: Mothed(n‐1): } 递归二分查找 Java二分查找实现,欢迎大家提出交流意见.  /** *名称:BinarySearch *功能:实现了折半查找(二分查找)的递归和非递归算法. *说明: *     1.要求所查找的数组已有序,并且其中元素已实现Comparable<T>接口,如Integ

算法:归并算法的递归与非递归形式

归并算法是将两个或两个以上的有序表组合成一个新的有序表,它的原理是:假设初始序列含有n个记录,则可以看成是n个有序子序列,两两归并,得到[n/2]个有序子序列,再次归并--不断重复直至归并到长度为n的有序序列,这样的排序方法称为2路归并排序. 实例一:递归形式的2路归并算法 #define MAXSIZE 4 int data[MAXSIZE] = {2,1,0,3}; /* * 功能:将from数组min到max-1下标数据排好序,最后的结果是to[min]...to[max-1] * 输入: