欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java递归全排列

程序员文章站 2024-01-13 12:28:40
...
	
    public static Stream<Integer> range(final Integer start, Integer length, Integer step, List<Integer> except) {  
        Supplier<Integer> seed = new Supplier<Integer>() {  
  
            private Integer next = start;  
  
            @Override  
            public Integer get() {  
                Integer _next = next;  
                next += step;  
                return _next;  
            }  
        };  
        return Stream.generate(seed).limit(length).filter(new Predicate<Integer>()     {
            @Override  
            public boolean test(Integer t) {  
                if (except == null)  
                    return true;  
                if (except.contains(t))  
                    return false;  
                return true;  
            }  
        });  
    }  
 
    List<Integer> list = new ArrayList<Integer>();
	AtomicInteger count = new AtomicInteger(0);
	public void WholeArrangement(int N) {
		Stream<Integer> range = range(0, N, 1, list);
		range.forEach(new Consumer<Integer>() {
			@Override
			public void accept(Integer t) {
				list.add(t);
				WholeArrangement(N);
				list.remove(list.size() - 1);
			}
		});
		if (list.size() == N) {
			count.incrementAndGet();
			System.out.println(Arrays.toString(list.toArray()));
		}
	}
相关标签: 全排列