-
-
Notifications
You must be signed in to change notification settings - Fork 195
[친환경사과] week 12 #1056
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
Merged
Merged
[친환경사과] week 12 #1056
Changes from all commits
Commits
Show all changes
2 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 hidden or 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 @@ | ||
package leetcode_study | ||
|
||
/* | ||
* 끝에서 n 번째 노드를 제거하는 문제 | ||
* 예외 상황이 발생하는 Case를 나눠 문제 해결 | ||
* 시간 복잡도 : O(n) | ||
* -> node list를 순회하며 배열에 담는 과정 | ||
* 공간 복잡도 : O(n) | ||
* -> 각 node를 담을 list 공간 | ||
* */ | ||
fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? { | ||
val tempNodeList = mutableListOf<ListNode>() | ||
var currentHead = head | ||
while (currentHead != null) { | ||
tempNodeList.add(currentHead) | ||
currentHead = currentHead.next | ||
} | ||
|
||
val preIndex = tempNodeList.size - n - 1 | ||
val postIndex = tempNodeList.size - n + 1 | ||
|
||
if (preIndex < 0) { | ||
if (tempNodeList.size == 1) { | ||
return null | ||
} | ||
return tempNodeList[postIndex] | ||
} else if (postIndex == tempNodeList.size) { | ||
tempNodeList[preIndex].next = null | ||
return head | ||
} else { | ||
tempNodeList[preIndex].next = tempNodeList[postIndex] | ||
} | ||
return head | ||
} |
This file contains hidden or 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,25 @@ | ||
package leetcode_study | ||
|
||
/** | ||
* 같은 이진 트리 확인 문제 | ||
* 재귀를 사용해 문제 해결 | ||
* | ||
* 시간 복잡도: O(n) | ||
* -> 모든 노드를 방문하여 비교 진행 | ||
* 공간 복잡도: O(n) | ||
* -> 재귀 호출 횟수 n 회 | ||
* | ||
* 재귀 문제를 다룰 때 의식적인 연습 | ||
* 1. Base Case(기저 조건)를 명확하게 정의 | ||
* 2. 기저 조건으로 주어진 문제에 따라서 탈출 조건 정의 | ||
* 3. 작은 문제로 나눠 생각하기 | ||
* 4. 작은 문제를 결합하여 정답 도출 | ||
*/ | ||
fun isSameTree(p: TreeNode?, q: TreeNode?): Boolean { | ||
if (p == null && q == null) return true // 둘 다 null이면 같음 | ||
if (p == null || q == null) return false // 한쪽만 null이면 다름 | ||
if (p.`val` != q.`val`) return false // 값이 다르면 다름 | ||
|
||
// 왼쪽 서브트리와 오른쪽 서브트리를 각각 재귀적으로 비교 | ||
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right) | ||
} |
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.
해당 문제는 LinkedList 의 특성을 활용하는 문제로 공간 복잡도를 O(1) 으로 하여 풀이가 가능합니다 :)
어려움이 있으시다면 해답을 확인하시고 이해하시고 넘어가시면 너무 좋을것 같아요!