Skip to content

Commit 4e6204f

Browse files
authored
Merge pull request DaleStudy#50 from bky373/main
[WIP][bky373] Solutions for week 2 problems
2 parents 8676555 + 089b114 commit 4e6204f

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

โ€Žinvert-binary-tree/bky373.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* - ๋ฌธ์ œ: https://leetcode.com/problems/linked-list-cycle/
3+
* - TC: O(N)
4+
* - SC: O(N)
5+
*/
6+
public class Solution_226 {
7+
public TreeNode invertTree(TreeNode root) {
8+
if (root == null) {
9+
return null;
10+
}
11+
TreeNode tmp = root.left;
12+
root.left = invertTree(root.right);
13+
root.right = invertTree(tmp);
14+
return root;
15+
}
16+
}

โ€Žlinked-list-cycle/bky373.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* - ๋ฌธ์ œ: https://leetcode.com/problems/linked-list-cycle/
3+
* - TC: O(N)
4+
* - SC: O(1)
5+
*/
6+
public class Solution {
7+
public boolean hasCycle(ListNode head) {
8+
if (head == null || head.next == null) {
9+
return false;
10+
}
11+
int min = -10001;
12+
13+
while (head.next != null) {
14+
if (head.next.val == min) {
15+
return true;
16+
}
17+
head.val = min;
18+
head = head.next;
19+
}
20+
return false;
21+
}
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* TC: O(N)
3+
* SC: O(N)
4+
*/
5+
class Solution_21 {
6+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
7+
if (list1 == null && list2 == null) {
8+
return null;
9+
}
10+
11+
List<Integer> nums = new ArrayList<>();
12+
13+
while (list1 != null) {
14+
nums.add(list1.val);
15+
list1 = list1.next;
16+
}
17+
while (list2 != null) {
18+
nums.add(list2.val);
19+
list2 = list2.next;
20+
}
21+
22+
Object[] arr = nums.toArray();
23+
Arrays.sort(arr);
24+
25+
ListNode head = new ListNode((int) arr[0]);
26+
ListNode curr = head;
27+
for (int i=1; i<arr.length; i++) {
28+
curr.next = new ListNode((int) arr[i]);
29+
curr = curr.next;
30+
}
31+
return head;
32+
}
33+
}

โ€Žreverse-linked-list/bky373.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* https://leetcode.com/problems/reverse-linked-list/
3+
* TC: O(N)
4+
* SC: O(N)
5+
*/
6+
class Solution_206 {
7+
8+
public ListNode reverseList(ListNode head) {
9+
if (head == null) {
10+
return head;
11+
}
12+
ListNode a = new ListNode(head.val);
13+
ListNode b;
14+
while (head.next != null) {
15+
b = new ListNode(head.next.val, a);
16+
a = b;
17+
head = head.next;
18+
}
19+
return a;
20+
}
21+
}

โ€Žvalid-parentheses/bky373.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* - ๋ฌธ์ œ: https://leetcode.com/problems/valid-parentheses/
3+
* - TC: O(N)
4+
* - SC: O(1)
5+
*/
6+
class Solution_20 {
7+
8+
public boolean isValid(String s) {
9+
Stack<Character> st = new Stack<>();
10+
Map<Character, Character> pairs = Map.of('(', ')', '{', '}', '[', ']');
11+
12+
for (int i = 0; i < s.length(); i++) {
13+
// ์—ด๋ฆฐ ๊ด„ํ˜ธ์ด๋ฉด ์Šคํƒ์— push
14+
if (pairs.containsKey(s.charAt(i))) {
15+
st.push(s.charAt(i));
16+
} else { // ๋‹ซํžŒ ๊ด„ํ˜ธ์ธ๋ฐ
17+
if (st.isEmpty()) { // ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด(์—ด๋ฆฐ ๊ด„ํ˜ธ๊ฐ€ ์—†์œผ๋ฉด) false ๋ฐ˜ํ™˜
18+
return false;
19+
}
20+
// ์Šคํƒ์˜ ์ตœ๊ทผ ์š”์†Œ(์—ด๋ฆฐ ๊ด„ํ˜ธ)์™€ ์ง์„ ์ด๋ฃจ์ง€ ์•Š์œผ๋ฉด false ๋ฐ˜ํ™˜
21+
if (s.charAt(i) != pairs.get(st.pop())) {
22+
return false;
23+
}
24+
}
25+
}
26+
// ์—ด๋ฆฐ ๊ด„ํ˜ธ๊ฐ€ ๋‚จ์•„ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ชฉ๋ก์ด ๋น„์–ด์žˆ๋Š”์ง€ ์ฒดํฌ
27+
return st.isEmpty();
28+
}
29+
}

0 commit comments

Comments
ย (0)