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;i0 && 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 list = new LinkedList (); 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; }}