Skip to content
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

[박종훈] 2주차 답안 제출 #39

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions invert-binary-tree/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- https://leetcode.com/problems/invert-binary-tree/
- time complexity : O(n)
- space complexity : O(h), h = tree height
- https://algorithm.jonghoonpark.com/2024/03/31/leetcode-226

```java
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}

var temp = root.left;
root.left = root.right;
root.right = temp;
Comment on lines +13 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바는 이게 참 귀찮은 거 같아요 ㅋㅋ


if (root.left != null) {
invertTree(root.left);
}
if (root.right != null) {
invertTree(root.right);
}

return root;
}
}
```
27 changes: 27 additions & 0 deletions linked-list-cycle/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- https://leetcode.com/problems/linked-list-cycle
- time complexity : O(n)
- space complexity : O(1)
- https://algorithm.jonghoonpark.com/2024/02/15/leetcode-141

```java
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) {
return false;
}

ListNode p1 = head;
ListNode p2 = head;
while(p2 != null && p2.next != null) {
p1 = p1.next;
p2 = p2.next.next;

if (p1 == p2) {
return true;
}
}

return false;
}
}
```
32 changes: 32 additions & 0 deletions merge-two-sorted-lists/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
- https://leetcode.com/problems/merge-two-sorted-lists/
- time complexity : O(n)
- space complexity : O(n)
- https://algorithm.jonghoonpark.com/2024/05/01/leetcode-21

```java
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode head = new ListNode(0);
ListNode tail = head;

while (list1 != null && list2 != null) {
if (list1.val < list2.val) {
tail.next = list1;
list1 = list1.next;
} else {
tail.next = list2;
list2 = list2.next;
}
tail = tail.next;
}

if (list1 != null) {
tail.next = list1;
} else {
tail.next = list2;
}

return head.next;
}
}
```
27 changes: 27 additions & 0 deletions reverse-linked-list/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- https://leetcode.com/problems/reverse-linked-list/
- time complexity : O(n)
- space complexity : O(1)
- https://algorithm.jonghoonpark.com/2024/02/15/leetcode-206

```java
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null) {
return null;
}

ListNode p1 = head;
ListNode p2 = head.next;
p1.next = null;

while (p2 != null) {
ListNode temp = p2.next;
p2.next = p1;
p1 = p2;
p2 = temp;
}

return p1;
}
}
```
40 changes: 40 additions & 0 deletions valid-parentheses/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
- https://leetcode.com/problems/valid-parentheses/
- time complexity : O(n)
- space complexity : O(n)
- https://algorithm.jonghoonpark.com/2024/04/29/leetcode-20

```java
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();

for (char c : s.toCharArray()) {
if (c == '(' || c == '{' || c == '[') {
stack.push(c);
continue;
}

if (stack.isEmpty()) {
return false;
}

boolean valid = false;
if (c == ')') {
valid = stack.peek() == '(';
} else if (c == '}') {
valid = stack.peek() == '{';
} else if (c == ']') {
valid = stack.peek() == '[';
}

if (valid) {
stack.pop();
} else {
return false;
}
}

return stack.isEmpty();
}
}
```