Skip to content

Commit 7728a62

Browse files
committed
Add week2
1 parent 4e6204f commit 7728a62

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

invert-binary-tree/Seunghyun-Lim.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 달레님 강의 참고
3+
* 아직 완벽하게 이해하지 못한 풀이,, 추가로 다시 확인 필요
4+
* 시간복잡도: O(n)
5+
* 공간복잡도: O(n)
6+
*/
7+
class Solution {
8+
public TreeNode invertTree(TreeNode root) {
9+
if (root == null) return null;
10+
11+
TreeNode temp = root.left;
12+
root.left = root.right;
13+
root.right = temp;
14+
15+
invertTree(root.right);
16+
invertTree(root.left);
17+
18+
return root;
19+
}
20+
}

linked-list-cycle/Seunghyun-Lim.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 순환 리스트
3+
* 달레님 강의 참고
4+
* 기존에 이해했던 Linked List 처럼 null이 되는 경우가 아니기 때문에 loop에 빠지지 않게 주의해야 한다.
5+
* 시간복잡도: O(n)
6+
* 공간복잡도: O(n)
7+
*/
8+
class Solution {
9+
public boolean hasCycle(ListNode head) {
10+
Set<ListNode> visited = new HashSet<>();
11+
while (head != null) {
12+
if (visited.contains(head)) {
13+
return true;
14+
} else {
15+
visited.add(head);
16+
head = head.next;
17+
}
18+
}
19+
20+
return false;
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
3+
if (list1 == null) return list2;
4+
if (list2 == null) return list1;
5+
6+
if (list1.val < list2.val) {
7+
list1.next = mergeTwoLists(list1.next, list2);
8+
return list1;
9+
} else {
10+
list2.next = mergeTwoLists(list1, list2.next);
11+
return list2;
12+
}
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 시간복잡도: O(n)
3+
* -> 노드가 존재하지 않을때 까지 반복,,
4+
* 공간복잡도: O(1)
5+
* -> ListNode
6+
*/
7+
class Solution {
8+
public ListNode reverseList(ListNode head) {
9+
if (head == null || head.next == null) {
10+
return head;
11+
}
12+
13+
ListNode node1 = head;
14+
ListNode node2 = node1.next;
15+
head.next = null;
16+
17+
while (node1 != null && node2 != null) {
18+
ListNode t = node2.next;
19+
node2.next = node1;
20+
node1 = node2;
21+
node2 = t;
22+
}
23+
24+
return node1;
25+
}
26+
}

valid-parentheses/Seunghyun-Lim.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 시간복잡도: O(n)
3+
* -> 문자열 s의 길이에 따라 증가
4+
* 공간복잡도: O(n)
5+
* -> 문자열 s의 길이에 따라 stack의 공간 증가
6+
*/
7+
class Solution {
8+
public boolean isValid(String s) {
9+
char first = s.charAt(0);
10+
if (s.length() == 1 || first == ')' || first == '}' || first == ']') return false;
11+
12+
Stack<Character> stack = new Stack<>();
13+
14+
for (int i = 0; i < s.length(); i++) {
15+
char c = s.charAt(i);
16+
17+
if (c == '(' || c == '{' || c == '[') {
18+
stack.push(c);
19+
continue;
20+
}
21+
22+
if (stack.isEmpty()) return false;
23+
24+
if (c == ')') {
25+
Character pop = stack.pop();
26+
if (pop != '(') return false;
27+
} else if (c == '}') {
28+
Character pop = stack.pop();
29+
if (pop != '{') return false;
30+
} else if (c == ']') {
31+
Character pop = stack.pop();
32+
if (pop != '[') return false;
33+
}
34+
}
35+
36+
return stack.size() == 0;
37+
}
38+
}

0 commit comments

Comments
 (0)