-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GangBean] Week 2 #707
[GangBean] Week 2 #707
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2주차 문제풀이 고생하셨습니다~!
- space: O(1) -> no extra space is needed | ||
*/ | ||
// 0. assign return variable Set | ||
Set<List<Integer>> answer = new HashSet<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
중복을 피하려고 Set을 사용한게 인상깊네요..!
if (preorder.length == 0) return null; | ||
if (preorder.length == 1) return new TreeNode(preorder[0]); | ||
int i = 0; | ||
List<Integer> leftPreorder = new ArrayList<>(); // O(N) | ||
List<Integer> leftInorder = new ArrayList<>(); // O(N) | ||
List<Integer> rightPreorder = new ArrayList<>(); // O(N) | ||
List<Integer> rightInorder = new ArrayList<>(); // O(N) | ||
for (; i < inorder.length; i++) { // O(N) | ||
if (inorder[i] == preorder[0]) break; | ||
leftPreorder.add(preorder[i+1]); | ||
leftInorder.add(inorder[i]); | ||
} | ||
for (int idx = i+1; idx < inorder.length; idx++) { // O(N) | ||
rightPreorder.add(preorder[idx]); | ||
rightInorder.add(inorder[idx]); | ||
} | ||
|
||
return new TreeNode(preorder[0], buildTree(leftPreorder.stream().mapToInt(Integer::intValue).toArray(), leftInorder.stream().mapToInt(Integer::intValue).toArray()), buildTree(rightPreorder.stream().mapToInt(Integer::intValue).toArray(), rightInorder.stream().mapToInt(Integer::intValue).toArray())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
와; 이렇게 푸시는건 상상을 못했네요. dfs로 풀어보시는 것도 좋을것 같습니다!
Map<Character, Integer> sMap = new HashMap<>(); | ||
Map<Character, Integer> tMap = new HashMap<>(); | ||
|
||
for (char c: s.toCharArray()) { | ||
sMap.put(c, sMap.getOrDefault(c, 0) + 1); | ||
} | ||
for (char c: t.toCharArray()) { | ||
tMap.put(c, tMap.getOrDefault(c, 0) + 1); | ||
} | ||
|
||
return Objects.equals(sMap, tMap); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
전 s와 t를 .toCharArray()
로 바꾼 후 Arrays.sort
-> Arrays.equals()
로 비교했는데 Objects.equals로 HashMap 자체를 비교하는 방법도 있군요. 배워갑니다!
답안 제출 문제
체크 리스트
In Review
로 설정해주세요.