-
-
Notifications
You must be signed in to change notification settings - Fork 248
[anniemon78] Week6 #915
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
[anniemon78] Week6 #915
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,66 @@ | ||
var WordDictionary = function() { | ||
this.children = {}; | ||
this.isEnd = false; | ||
}; | ||
/** | ||
* 시간 복잡도: word의 길이만큼 순회하므로, O(n) | ||
* 공간 복잡도: 최대 word의 길이만큼 추가 노드를 생성하므로, O(n) | ||
*/ | ||
/** | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
WordDictionary.prototype.addWord = function(word) { | ||
let children = this.children; | ||
for(let w of word) { | ||
if(!children[w]) { | ||
children[w] = {}; | ||
children[w].isEnd = false; | ||
} | ||
children = children[w]; | ||
} | ||
children.isEnd = true; | ||
}; | ||
/** | ||
* 시간 복잡도: | ||
* 최악의 경우 각 노드의 자식 노드는 모든 알파벳의 갯수인 26이 되고, | ||
* 재귀 호출 스택의 깊이는 word.length에 비례하므로, | ||
* word의 길이가 n이면, 시간 복잡도는 O(26^n) | ||
* 공간 복잡도: | ||
* 호출 스택 깊이는 n이므로, 공간 복잡도는 O(n) | ||
*/ | ||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
WordDictionary.prototype.search = function(word) { | ||
let children = this.children; | ||
return this.dfs(0, word, children) | ||
}; | ||
|
||
WordDictionary.prototype.dfs = function(i, word, children) { | ||
if(i === word.length) { | ||
return children.isEnd; | ||
} | ||
if(word[i] === '.') { | ||
for(const c in children) { | ||
if(c === 'isEnd') continue; | ||
if(this.dfs(i+1, word, children[c])) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} else { | ||
if(!children[word[i]]) { | ||
return false; | ||
} | ||
return this.dfs(i+1, word, children[word[i]]); | ||
} | ||
} | ||
|
||
/** | ||
* Your WordDictionary object will be instantiated and called as such: | ||
* var obj = new WordDictionary() | ||
* obj.addWord(word) | ||
* var param_2 = obj.search(word) | ||
*/ |
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,27 @@ | ||
/** | ||
* 시간 복잡도: s의 길이만큼 순회하므로, O(n) | ||
* 공간 복잡도: 스택은 최대 s의 길이만큼 문자를 저장하므로, O(n) | ||
*/ | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isValid = function(s) { | ||
const stack = []; | ||
const brackets = { | ||
'(': ')', | ||
'{': '}', | ||
'[': ']' | ||
} | ||
for(const b of s) { | ||
if(b in brackets) { | ||
stack.push(b); | ||
} else { | ||
const cur = stack.pop(); | ||
if(brackets[cur] !== b) { | ||
return false; | ||
} | ||
} | ||
} | ||
return stack.length === 0; | ||
}; |
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.
Uh oh!
There was an error while loading. Please reload this page.