-
-
Notifications
You must be signed in to change notification settings - Fork 195
[seunghyun] Week 2 Solutions #65
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* 달레님 강의 참고 | ||
* 아직 완벽하게 이해하지 못한 풀이,, 추가로 다시 확인 필요 | ||
* 시간복잡도: O(n) | ||
* 공간복잡도: O(n) | ||
*/ | ||
class Solution { | ||
public TreeNode invertTree(TreeNode root) { | ||
if (root == null) return null; | ||
|
||
TreeNode temp = root.left; | ||
root.left = root.right; | ||
root.right = temp; | ||
|
||
invertTree(root.right); | ||
invertTree(root.left); | ||
|
||
return root; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* 순환 리스트 | ||
* 달레님 강의 참고 | ||
* 기존에 이해했던 Linked List 처럼 null이 되는 경우가 아니기 때문에 loop에 빠지지 않게 주의해야 한다. | ||
* 시간복잡도: O(n) | ||
* 공간복잡도: O(n) | ||
*/ | ||
class Solution { | ||
public boolean hasCycle(ListNode head) { | ||
Set<ListNode> visited = new HashSet<>(); | ||
while (head != null) { | ||
if (visited.contains(head)) { | ||
return true; | ||
} else { | ||
visited.add(head); | ||
head = head.next; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 재귀로 접근하신게 신선한 것 같네요...! : ) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
if (list1 == null) return list2; | ||
if (list2 == null) return list1; | ||
|
||
if (list1.val < list2.val) { | ||
list1.next = mergeTwoLists(list1.next, list2); | ||
return list1; | ||
} else { | ||
list2.next = mergeTwoLists(list1, list2.next); | ||
return list2; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* 시간복잡도: O(n) | ||
* -> 노드가 존재하지 않을때 까지 반복,, | ||
* 공간복잡도: O(1) | ||
* -> ListNode | ||
*/ | ||
class Solution { | ||
public ListNode reverseList(ListNode head) { | ||
if (head == null || head.next == null) { | ||
return head; | ||
} | ||
|
||
ListNode node1 = head; | ||
ListNode node2 = node1.next; | ||
head.next = null; | ||
|
||
while (node1 != null && node2 != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요 부분에서 굳이 node1 까지 확인한 이유가 혹시 있을까요? 위에 이미 head로 지정을 해놓으셔서 이미 while 루프에 들어간 리스트는 list1이 null 이 아님을 확인한 상태인거 같아서요! |
||
ListNode t = node2.next; | ||
node2.next = node1; | ||
node1 = node2; | ||
node2 = t; | ||
} | ||
|
||
return node1; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* 시간복잡도: O(n) | ||
* -> 문자열 s의 길이에 따라 증가 | ||
* 공간복잡도: O(n) | ||
* -> 문자열 s의 길이에 따라 stack의 공간 증가 | ||
*/ | ||
class Solution { | ||
public boolean isValid(String s) { | ||
char first = s.charAt(0); | ||
if (s.length() == 1 || first == ')' || first == '}' || first == ']') return false; | ||
|
||
Stack<Character> stack = new Stack<>(); | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
char c = s.charAt(i); | ||
|
||
if (c == '(' || c == '{' || c == '[') { | ||
stack.push(c); | ||
continue; | ||
} | ||
|
||
if (stack.isEmpty()) return false; | ||
|
||
if (c == ')') { | ||
Character pop = stack.pop(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stack pop 부분이 각 조건문 안에서 반복되어서 |
||
if (pop != '(') return false; | ||
} else if (c == '}') { | ||
Character pop = stack.pop(); | ||
if (pop != '{') return false; | ||
} else if (c == ']') { | ||
Character pop = stack.pop(); | ||
if (pop != '[') return false; | ||
} | ||
} | ||
|
||
return stack.size() == 0; | ||
} | ||
} |
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.
해당 솔루션만 시/공간 복잡도 누락된 것 같습니다 😄