본문 바로가기

LeetCode30

[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.
[Algorithm][Array] 문제 풀이 #16 - Ransom Note 문제 링크 https://leetcode.com/problems/ransom-note/ 내 풀이 public boolean canConstruct(String ransomNote, String magazine) { Map map = new HashMap(); for(char c : ransomNote.toCharArray()) { map.put(c, map.getOrDefault(c, 0) + 1); } for(char c : magazine.toCharArray()) { if (map.containsKey(c)) { int value = map.get(c); if (value == 1) map.remove(c); else map.put(c, value - 1); } if(map.isEmpty()) re.. 2022. 8. 1.
[Algorithm][Array] 문제 풀이 #15 - Group Anagrams 문제 링크 https://leetcode.com/problems/group-anagrams/ 문제 설명 배열에 있는 문자들끼리의 아나그램을 구하고 그룹핑 시켜라 내 코드 public List groupAnagrams(String[] strs) { List results = new ArrayList(); List stringList = new ArrayList(); stringList.add(strs[0]); results.add(stringList); for(int i = 1; i < strs.length; i++) { boolean add = true; for(List result : results) { String r = result.get(0); if(isAnagram(r, strs[i])) { r.. 2022. 7. 28.