ffinn92 / Keep-at-solve-it

꾸준히 알고리즘 풀기 위한 스터디 저장소입니다.
2 stars 3 forks source link

[220802][BC][인프런](7-6) 부분집합 구하기(DFS) #113

Closed honeySleepr closed 2 years ago

honeySleepr commented 2 years ago

📌 문제

⭐️ 아이디어

🤔 고민한 내용

💪 새롭게 배운 내용

🆘 이해가 어려운 내용

❌ 해결하지 못한 이유

✅ 본인 풀이

// 부분집합 구하기(DFS)
public class P0706 {

    private static int[] arr;
    private static int n = 3;
    private static StringBuilder sb = new StringBuilder();

    public static void main(String[] args) {
        arr = new int[n + 1];

        DFS(1);
        System.out.println(sb);
    }

    private static void DFS(int L) {
        if (L > n) {
            for (int i = 1; i < arr.length; i++) {
                if (arr[i] == 1) {
                    sb.append(i).append(" ");
                }
            }
            sb.append("\n");
            return;
        }

        arr[L] = 1;
        DFS(L + 1);
        arr[L] = 0;
        DFS(L + 1);
    }
}

참고한 자료