티스토리 뷰
Problem
Given an integer array nums of unique elements, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:
Input: nums = [0]
Output: [[],[0]]
Constraints:
- 1 <= nums.length <= 10
- -10 <= nums[i] <= 10
- All the numbers of nums are unique.
Solution
고유한 원소로 이루어진 정수 배열이 주어질 때 모든 가능한 부분 집합을 반환하는 문제입니다.
dfs를 이용해 풀이할 수 있습니다.
package io.lcalmsky.leetcode.subsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
if (nums == null || nums.length == 0) {
return Collections.emptyList();
}
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
List<Integer> subset = new ArrayList<>();
dfs(nums, result, subset, 0);
return result;
}
private void dfs(int[] nums, List<List<Integer>> result, List<Integer> subset, int start) {
result.add(new ArrayList<>(subset));
for (int i = start; i < nums.length; i++) {
subset.add(nums[i]);
dfs(nums, result, subset, i + 1);
subset.remove(subset.size() - 1);
}
}
}
Test
package io.lcalmsky.leetcode.subsets;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SolutionTest {
@Test
void testAll() {
assertAll(
() -> test(new int[]{1, 2, 3}, List.of(List.of(), List.of(1), List.of(2), List.of(1, 2), List.of(3), List.of(1, 3), List.of(2, 3), List.of(1, 2, 3))),
() -> test(new int[]{0}, List.of(List.of(), List.of(0)))
);
}
private void test(int[] nums, List<List<Integer>> expected) {
Solution solution = new Solution();
List<List<Integer>> actual = solution.subsets(nums);
Map<List<Integer>, Integer> map = new HashMap<>();
for (List<Integer> subset : expected) {
map.put(subset, map.getOrDefault(subset, 0) + 1);
}
for (List<Integer> subset : actual) {
map.put(subset, map.getOrDefault(subset, 0) - 1);
}
map.values().forEach(v -> assertEquals(0, v));
}
}
'Algorithm > LeetCode' 카테고리의 다른 글
[LeetCode] 72. Edit Distance (0) | 2023.07.04 |
---|---|
[LeetCode] 146. LRU Cache (0) | 2023.07.03 |
[LeetCode] 240. Search a 2D Matrix II (0) | 2023.07.01 |
[LeetCode] 46. Permutations (0) | 2023.06.30 |
[LeetCode] 230. Kth Smallest Element in a BST (0) | 2023.06.29 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- JPA
- QueryDSL
- Linux
- 스프링부트
- 클린 아키텍처
- Spring Boot
- gRPC
- r
- Spring Data JPA
- @ManyToOne
- Java
- 스프링 데이터 jpa
- Spring Boot Tutorial
- proto3
- 함께 자라기
- JSON
- 함께 자라기 후기
- leetcode
- spring boot application
- Jackson
- Spring Boot JPA
- spring boot app
- 스프링 부트 애플리케이션
- 스프링 부트
- 헥사고날 아키텍처
- 알고리즘
- intellij
- 스프링 부트 회원 가입
- spring boot jwt
- 스프링 부트 튜토리얼
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함