medium17 [Algorithm][Recursion] 문제 풀이 #20 - Subsets II 문제 링크 https://leetcode.com/problems/subsets-ii/ 내 코드 private Set result = new HashSet(); private List tmp = new ArrayList(); public List subsetsWithDup(int[] nums) { Arrays.sort(nums); recursion(nums, 0); return new ArrayList(result); } private void recursion(int[] nums, int index) { if(index >= nums.length) { result.add(new ArrayList(tmp)); return; } tmp.add(nums[index]); recursion(nums, index+.. 2022. 8. 6. [Algorithm][Recursion] 문제 풀이 #19 - Subsets 문제 링크 https://leetcode.com/problems/subsets/ 풀이 private List result = new ArrayList(); private List tmp = new ArrayList(); public List subsets(int[] nums) { recursion(nums, 0); return result; } private void recursion(int[] nums, int index){ if(index >= nums.length) { result.add(new ArrayList(tmp)); return; } tmp.add(nums[index]); recursion(nums, index+1); tmp.remove(tmp.size() - 1); recursion(nu.. 2022. 8. 5. [Algorithm][Recursion] 문제 풀이 #18 - Combinations 문제 링크 https://leetcode.com/problems/combinations/ 내 코드 List result; LinkedList current; public List combine(int n, int k) { result = new ArrayList(); current = new LinkedList(); recursion(1, n, k); return result; } private void recursion(int start, int n, int k) { if(current.size()==k){ result.add(new ArrayList(current)); return; } for(int i=start;i 2022. 8. 4. [Algorithm][Recursion] 문제 풀이 #17 - Generate Parentheses 문제 링크 https://leetcode.com/problems/generate-parentheses/ 내 풀이 List list; public List generateParenthesis(int n) { list = new ArrayList(); recursion("", 0, 0, n); return list; } private void recursion(String ans, int left, int right, int n) { if(ans.length() == n * 2) { list.add(ans); return; } if(left right) recursion(ans + ")", left, rig.. 2022. 8. 4. 이전 1 2 3 4 5 다음