-
-
Notifications
You must be signed in to change notification settings - Fork 339
[sadie100] WEEK7 Solutions #2538
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
Open
sadie100
wants to merge
2
commits into
DaleStudy:main
Choose a base branch
from
sadie100:main
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.
+106
−0
Open
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,72 @@ | ||
| /* | ||
| words를 맵형태로 저장, 각 스펠링과 다음에 나오는 문자열을 key-value로 하는 맵으로 저장 | ||
| .의 경우 모든 경우를 탐색한다 | ||
|
|
||
| 시간복잡도 : | ||
| addWord : O(L) (L은 words의 길이) | ||
| search : 일반적으로 O(L), .이 많으면 O(26^k) (k는 .의 개수) | ||
|
|
||
| */ | ||
| class TrieNode { | ||
| children: Map<string, TrieNode> | ||
| isEnd: boolean | ||
|
|
||
| constructor() { | ||
| this.children = new Map() | ||
| this.isEnd = false | ||
| } | ||
| } | ||
|
|
||
| class WordDictionary { | ||
| words = new TrieNode() | ||
| constructor() {} | ||
|
|
||
| addWord(word: string): void { | ||
| let curWords = this.words | ||
|
|
||
| for (let i = 0; i < word.length; i++) { | ||
| const char = word[i] | ||
|
|
||
| if (!curWords.children.has(char)) { | ||
| curWords.children.set(char, new TrieNode()) | ||
| } | ||
|
|
||
| curWords = curWords.children.get(char) | ||
| } | ||
| curWords.isEnd = true | ||
| } | ||
|
|
||
| search(word: string): boolean { | ||
| return this.searchRecursively(word, this.words) | ||
| } | ||
|
|
||
| searchRecursively(word: string, area: TrieNode) { | ||
| for (let i = 0; i < word.length; i++) { | ||
| if (!area) return false | ||
| const char = word[i] | ||
|
|
||
| if (char === '.') { | ||
| const values = [...area.children.values()] | ||
|
|
||
| for (let newArea of values) { | ||
| const result = this.searchRecursively(word.slice(i + 1), newArea) | ||
| if (result) return true | ||
| } | ||
| return false | ||
| } else { | ||
| if (!area.children.has(char)) return false | ||
| area = area.children.get(char) | ||
| } | ||
| } | ||
|
|
||
| if (area.isEnd) return true | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Your WordDictionary object will be instantiated and called as such: | ||
| * var obj = new WordDictionary() | ||
| * obj.addWord(word) | ||
| * var param_2 = obj.search(word) | ||
| */ |
|
Contributor
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 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 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * class ListNode { | ||
| * val: number | ||
| * next: ListNode | null | ||
| * constructor(val?: number, next?: ListNode | null) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.next = (next===undefined ? null : next) | ||
| * } | ||
| * } | ||
| */ | ||
|
|
||
| /* | ||
| 직전 노드를 담는 변수 before과 다음 노드를 담는 변수 next, 순회중인 노드 head 변수와 함께 list를 순회 | ||
| head의 next에 before을 넣고, head를 next로 변환하며 head가 null이 아닐 때까지 반복한다 | ||
|
|
||
| 시간복잡도 : O(N) - 1번 순회 | ||
| 공간복잡도 : O(1) - before, next 변수 | ||
|
|
||
| */ | ||
| function reverseList(head: ListNode | null): ListNode | null { | ||
| if (!head) return head | ||
| let originNext = head.next | ||
| let before = null | ||
|
|
||
| while (head !== null) { | ||
| originNext = head.next | ||
| head.next = before | ||
| before = head | ||
| head = originNext | ||
| } | ||
|
|
||
| return before | ||
| } |
Oops, something went wrong.
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.
🏷️ 알고리즘 패턴 분석