public static List<int[]> combin(final int TOTAL, final int SELETED) { List<int[]> list = new ArrayList<int[]>(400000); int[] i = new int[SELETED]; for (int x = 1; x <= SELETED; x++) i[x - 1] = x; final int LAST = SELETED - 1; while (true) { list.add(Arrays.copyOf(i, SELETED)); i[LAST]++; for (int n = LAST; n > 0; n--) { //i[n]达到顶点 if (i[n] > TOTAL - SELETED + 1 + n) { i[n - 1]++; for (int x = n; x < SELETED; x++) { i[x] = i[x - 1] + 1; } } } if (i[0] == TOTAL - LAST + 1) { break; } } return list; // return null; } public static void main(String[] args) { long i = System.currentTimeMillis(); List<int[]> list = combin(36, 5); System.out.println(System.currentTimeMillis() - i); System.out.println(list.size()); }
如果有兴趣使用的同仁,请自行修改int[]下标记录数组为更合适的数据结构。
时间: 2024-10-18 16:07:01