https://leetcode.com/problems/subsets/description/
문제 파악
중복되지 않는 정수들로 구성된 배열 nums가 주어졌을 때, 가능한 모든 부분집합을 return하기
문제 풀이
📌 완전탐색
- 백트래킹 (재귀)
백트래킹
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> answer = new ArrayList<>();
backtracking(nums, 0, new ArrayList<>(), answer);
return answer;
}
void backtracking(int[] nums, int startIdx, List<Integer> curr, List<List<Integer>> answer) {
answer.add(new ArrayList<>(curr));
for (int i = startIdx; i < nums.length; i++) {
curr.add(nums[i]);
backtracking(nums, i + 1, curr, answer);
curr.remove(curr.size() - 1);
}
}
}
📊 실행 결과
'알고리즘' 카테고리의 다른 글
[Programmers] 87946. 피로도 (java) (1) | 2025.02.04 |
---|---|
[LeetCode] 79. Word Search (java) (1) | 2025.01.27 |
[LeetCode] 77. Combinations (java) (0) | 2025.01.26 |
[LeetCode] 46. Permutations (java) (0) | 2025.01.26 |
[LeetCode] 1. Two Sum (java) (0) | 2025.01.26 |