-
Notifications
You must be signed in to change notification settings - Fork 126
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
[박종훈] 2주차 답안 제출 #39
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
if (root.left != null) { | ||
invertTree(root.left); | ||
} | ||
if (root.right != null) { | ||
invertTree(root.right); | ||
} | ||
|
||
return root; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
자바는 이게 참 귀찮은 거 같아요 ㅋㅋ