-
Notifications
You must be signed in to change notification settings - Fork 3
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주차 문제 올립니다. #18
Open
kimeodml
wants to merge
1
commit into
main
Choose a base branch
from
week-02-kimeodml
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−0
Open
Changes from all commits
Commits
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 @@ | ||
# Intuition | ||
|
||
재귀를 활용해 오른쪽 노드와 왼쪽 노드를 교환 | ||
|
||
# Approach | ||
|
||
1. 현재 node가 null일 경우 null을 반환 | ||
2. 현재 노드의 오른쪽/왼쪽을 서로 교환 | ||
3. inverTree를 재귀적으로 호출하여 하위 노드도 동일하게 교환 | ||
4. 루트 노드 반환 | ||
|
||
# Complexity | ||
|
||
- 시간복잡도: O(n) | ||
- 공간복잡도: O(n) | ||
|
||
# Code | ||
```js | ||
var invertTree = function(root) { | ||
if(!root) return null; | ||
|
||
[root.right, root.left] = [root.left, root.right]; | ||
invertTree(root.right); | ||
invertTree(root.left); | ||
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,29 @@ | ||
# Intuition | ||
|
||
set을 이용해 중복 감지 | ||
|
||
# Approach | ||
|
||
1. 빈 해시셋을 생성 | ||
2. 노드를 순회하면서 해시셋에 존재하면 true 아니면 false를 반환 | ||
|
||
# Complexity | ||
|
||
- 시간복잡도: O(n) | ||
- 공간복잡도: O(n) | ||
|
||
# Code | ||
```js | ||
var hasCycle = function(head) { | ||
let cur = head; | ||
let node = new Set(); | ||
while(cur) { | ||
if(node.has(cur)) { | ||
return true; | ||
} | ||
node.add(cur); | ||
cur = cur.next; | ||
} | ||
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,37 @@ | ||
# Intuition | ||
|
||
두 개의 연결 리스트를 서로 비교하면서 이동 | ||
|
||
# Approach | ||
|
||
1. 하나의 dummy 노드를 생성(리스트의 시작점) | ||
2. list1과 list2가 존재할 때까지 반복하면서 서로의 값을 비교 후 연결 | ||
3. 남은 노드가 있을 경우 결과 리스트에 연결 | ||
4. 결과 리스트 반환 | ||
|
||
# Complexity | ||
|
||
- 시간복잡도: O(n+m) | ||
- 공간복잡도: O(1) | ||
|
||
# Code | ||
```js | ||
var mergeTwoLists = function(list1, list2) { | ||
let dummy = new ListNode(); | ||
let tail = dummy; | ||
|
||
while(list1 && list2) { | ||
if(list1.val >= list2.val) { | ||
tail.next = list2; | ||
list2 = list2.next; | ||
} else { | ||
tail.next = list1; | ||
list1 = list1.next; | ||
} | ||
tail = tail.next; | ||
} | ||
|
||
tail.next = list1 || list2; | ||
return dummy.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,34 @@ | ||
# Intuition | ||
|
||
현재의 노드가 이전 노드를 가리키도록 변경 | ||
|
||
# Approach | ||
|
||
1. cur 노드에 head를 prev 노드를 null로 초기화 | ||
2. cur 노드가 mull이 아닐 때까지 반복 | ||
2.1. 다음 노드를 cur에 저장 | ||
2.2. 다음 노드를 prev 노드로 변경(노드 연결 반전) | ||
3.4. prev 노드를 현재 노드로 업데이트 | ||
2.3. 현재 노드를 다음 노드로 업데이트 | ||
3. 반전된 노드 반환 | ||
|
||
|
||
# Complexity | ||
|
||
- 시간복잡도: O(n) | ||
- 공간복잡도: O(1) | ||
|
||
# Code | ||
```js | ||
var reverseList = function(head) { | ||
let cur = head; | ||
let prev = null; | ||
while(cur) { | ||
cur = head.next; | ||
head.next = prev; | ||
prev = head; | ||
head = cur; | ||
} | ||
return prev; | ||
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. 저도 이렇게 했는데 단순한 과정인데 작성하기 힘들더라구여.. |
||
}; | ||
``` |
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,43 @@ | ||
# Intuition | ||
|
||
스택을 활용해 괄호들을 비교 후 pop하거나 push함 | ||
|
||
# Approach | ||
|
||
1. 빈 배열 arr 생성 | ||
2. 여는 괄호일 경우 arr에 push | ||
3. 닫는 괄호일 경우 | ||
3.1. 이때 arr이 비어 있을 경우 false로 반환 | ||
3.2. 가장 맨 위의 prev와 닫는 괄호를 비교하여 올바르면 괄호 제거 그렇지 않으면 false로 반환 | ||
3. arr이 비어있으면 true 그렇지 않을 경우 false로 반환 | ||
|
||
|
||
# Complexity | ||
|
||
- 시간복잡도: O(n) | ||
- 공간복잡도: O(n) | ||
|
||
# Code | ||
```js | ||
var isValid = function(s) { | ||
let arr = []; | ||
for (let char of s) { | ||
if (char === '(' || char === '[' || char === '{') { | ||
arr.push(char); | ||
} else if (char === ')' || char === ']' || char === '}') { | ||
if (arr.length === 0) { | ||
return false; | ||
} | ||
let prev = arr[arr.length - 1]; | ||
if ((char === ']' && prev === '[') || | ||
(char === ')' && prev === '(') || | ||
(char === '}' && prev === '{')) { | ||
arr.pop(); | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
return arr.length === 0; | ||
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. 오 스택이 비었는 지 확인을 단순하게 했네요 굳 |
||
}; | ||
``` |
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.
오 배열을 통한 swap