class Solution {
public void solve(List<List<Integer>> answer, int[] nums, List<Integer> cur) {
if (cur.size() == nums.length) {
answer.add(new ArrayList<>(cur));
return;
}
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
if (cur.contains(num)) {
continue;
}
cur.add(num);
solve(answer, nums, cur);
cur.remove((Integer) num);
}
}
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> answer = new ArrayList<>();
solve(answer, nums, new ArrayList<>());
return answer;
}
}
반응형
'Algorithm > LeetCode' 카테고리의 다른 글
[c++] LeetCode 79번 Word Search (0) | 2022.11.16 |
---|---|
[c++] 41. First Missing Positive (0) | 2022.10.19 |
[Java] LeetCode 39번 Combination Sum (0) | 2021.10.04 |
[Java] LeetCode 34번 Find First and Last Position of Element in Sorted Array (0) | 2021.10.03 |
[Java] LeetCode 33번 Search in Rotated Sorted Array (0) | 2021.10.02 |