-
-
Notifications
You must be signed in to change notification settings - Fork 195
[bky373] Solutions for week 2 problems #50
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
17b7837
7e22b46
fdc0c0a
24af36f
089b114
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,16 @@ | ||
/** | ||
* - 문제: https://leetcode.com/problems/linked-list-cycle/ | ||
* - TC: O(N) | ||
* - SC: O(N) | ||
*/ | ||
public class Solution_226 { | ||
public TreeNode invertTree(TreeNode root) { | ||
if (root == null) { | ||
return null; | ||
} | ||
TreeNode tmp = root.left; | ||
root.left = invertTree(root.right); | ||
root.right = invertTree(tmp); | ||
return root; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* - 문제: https://leetcode.com/problems/linked-list-cycle/ | ||
* - TC: O(N) | ||
* - SC: O(1) | ||
*/ | ||
public class Solution { | ||
public boolean hasCycle(ListNode head) { | ||
if (head == null || head.next == null) { | ||
return false; | ||
} | ||
int min = -10001; | ||
|
||
while (head.next != null) { | ||
if (head.next.val == min) { | ||
return true; | ||
} | ||
head.val = min; | ||
head = head.next; | ||
} | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* TC: O(N) | ||
* SC: O(N) | ||
*/ | ||
class Solution_21 { | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
if (list1 == null && list2 == null) { | ||
return null; | ||
} | ||
|
||
List<Integer> nums = new ArrayList<>(); | ||
|
||
while (list1 != null) { | ||
nums.add(list1.val); | ||
list1 = list1.next; | ||
} | ||
while (list2 != null) { | ||
nums.add(list2.val); | ||
list2 = list2.next; | ||
} | ||
|
||
Object[] arr = nums.toArray(); | ||
Arrays.sort(arr); | ||
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. 여기서 정렬을 하는데... 시간 복잡도가 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. 아 제 풀이에서는 처음에 모든 node 를 돌아야 하기 때문에 |
||
|
||
ListNode head = new ListNode((int) arr[0]); | ||
ListNode curr = head; | ||
for (int i=1; i<arr.length; i++) { | ||
curr.next = new ListNode((int) arr[i]); | ||
curr = curr.next; | ||
} | ||
return head; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* https://leetcode.com/problems/reverse-linked-list/ | ||
* TC: O(N) | ||
* SC: O(N) | ||
*/ | ||
class Solution_206 { | ||
|
||
public ListNode reverseList(ListNode head) { | ||
if (head == null) { | ||
return head; | ||
} | ||
ListNode a = new ListNode(head.val); | ||
ListNode b; | ||
while (head.next != null) { | ||
b = new ListNode(head.next.val, a); | ||
a = b; | ||
head = head.next; | ||
} | ||
return a; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* - 문제: https://leetcode.com/problems/valid-parentheses/ | ||
* - TC: O(N) | ||
* - SC: O(1) | ||
*/ | ||
class Solution_20 { | ||
|
||
public boolean isValid(String s) { | ||
Stack<Character> st = new Stack<>(); | ||
Map<Character, Character> pairs = Map.of('(', ')', '{', '}', '[', ']'); | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
// 열린 괄호이면 스택에 push | ||
if (pairs.containsKey(s.charAt(i))) { | ||
st.push(s.charAt(i)); | ||
} else { // 닫힌 괄호인데 | ||
if (st.isEmpty()) { // 스택이 비어있으면(열린 괄호가 없으면) false 반환 | ||
return false; | ||
} | ||
// 스택의 최근 요소(열린 괄호)와 짝을 이루지 않으면 false 반환 | ||
if (s.charAt(i) != pairs.get(st.pop())) { | ||
return false; | ||
} | ||
} | ||
} | ||
// 열린 괄호가 남아 있을 수 있으므로 목록이 비어있는지 체크 | ||
return st.isEmpty(); | ||
} | ||
} |
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.
상당히 흥미로운 접근이네요. 노드를 방문했음으로 표시해놓고, 표시된 노드가 다시 나오는지를 찾는군요!
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.
네네 저걸로 방문 기록을 대신 했습니다 ㅋㅋ 다른 분들 풀이 보니 어떻게 저렇게 생각하셨을까.. 감탄했습니다