코테 자바 next permutation 해결책

Sorbet·2021년 2월 26일
0
  • 넥스트 퍼뮤테이션 문제는 비트마스크로 해결
public List<int[]> getCombination(int n, int r) {
        List<int[]> list = new ArrayList<>();
        for (int i = 0; i < (1 << n); i++) {
            if (Integer.bitCount(i) == r) {
                int[] add = new int[n];
                for (int j = 0; j < n; j++) {
                    if ((i & (1 << j)) > 0) {
                        add[j] = 1;
                    }
                }
                list.add(add);
            }
        }
        return list;
    }
profile
Sorbet is good...!

0개의 댓글