-
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
[Flynn] week2 #346
Merged
+203
−0
Merged
[Flynn] week2 #346
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2973412
solution: valid anagram
obzva 5e38b0b
Solution: Valid Anagram
obzva 0876fb5
Chore: Valid Anagram
obzva 6f5a4f1
Solution: Counting Bits
obzva 6b3f60e
Solution: Encode and Decode Strings
obzva 8efcb4f
Fix: Encode and Decode
obzva 911ee8e
Solution: Construct Binary Tree from Preorder and Inorder Traversal
obzva 8223d78
Solution: Decode Ways
obzva 6eed7ba
Solution: optimize Decode Ways
obzva 3abfd93
Refactor: Encode and Decode Strings
obzva 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
49 changes: 49 additions & 0 deletions
49
construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp
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,49 @@ | ||
/** | ||
* For the number of given nodes N, | ||
* | ||
* Time complexity: O(N) | ||
* | ||
* Space complexity: O(N) at worst | ||
*/ | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { | ||
unordered_map<int, int> inorder_index_map; | ||
stack<TreeNode*> tree_stack; | ||
|
||
for (int i = 0; i < inorder.size(); i++) inorder_index_map[inorder[i]] = i; | ||
|
||
TreeNode* root = new TreeNode(preorder[0]); | ||
tree_stack.push(root); | ||
|
||
for (int i = 1; i < preorder.size(); i++) { | ||
TreeNode* curr = new TreeNode(preorder[i]); | ||
|
||
if (inorder_index_map[curr->val] < inorder_index_map[tree_stack.top()->val]) { | ||
tree_stack.top()->left = curr; | ||
} else { | ||
TreeNode* parent; | ||
while (!tree_stack.empty() && inorder_index_map[curr->val] > inorder_index_map[tree_stack.top()->val]) { | ||
parent = tree_stack.top(); | ||
tree_stack.pop(); | ||
} | ||
parent->right = curr; | ||
} | ||
tree_stack.push(curr); | ||
} | ||
|
||
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,22 @@ | ||
/** | ||
* Time complexity: O(N) | ||
* | ||
* Space complexity: O(1) | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
vector<int> countBits(int n) { | ||
vector<int> res; | ||
res.push_back(0); | ||
|
||
int i = 1, j = 1; | ||
while (i <= n) { | ||
res.push_back(res[i - j] + 1); | ||
i++; | ||
if (i == j * 2) j *= 2; | ||
} | ||
|
||
return res; | ||
} | ||
}; |
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,53 @@ | ||
/** | ||
* For the length of the given string N, | ||
* | ||
* Time complexity: O(N) | ||
* | ||
* Space complexity: O(N) | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int numDecodings(string s) { | ||
if (s[0] == '0') return 0; | ||
|
||
int memo[s.size() + 1]; | ||
|
||
fill(memo, memo + s.size() + 1, 0); | ||
memo[0] = 1; | ||
memo[1] = 1; | ||
|
||
for (int i = 2; i <= s.size(); i++) { | ||
int s_i = i - 1; | ||
if (s[s_i] != '0') memo[i] = memo[i - 1]; | ||
int two_digits = stoi(s.substr(s_i - 1, 2)); | ||
if (10 <= two_digits && two_digits <= 26) memo[i] += memo[i - 2]; | ||
} | ||
|
||
return memo[s.size()]; | ||
} | ||
}; | ||
|
||
/** | ||
* Space complexity O(1) solution | ||
*/ | ||
|
||
// class Solution { | ||
// public: | ||
// int numDecodings(string s) { | ||
// if (s[0] == '0') return 0; | ||
|
||
// int one_digit_memo = 1, two_digit_memo = 1; | ||
|
||
// for (int i = 1; i < s.size(); i++) { | ||
// int tmp = 0; | ||
// if (s[i] != '0') tmp = one_digit_memo; | ||
// int two_digits = stoi(s.substr(i - 1, 2)); | ||
// if (10 <= two_digits && two_digits <= 26) tmp += two_digit_memo; | ||
// two_digit_memo = one_digit_memo; | ||
// one_digit_memo = tmp; | ||
// } | ||
|
||
// return one_digit_memo; | ||
// } | ||
// }; |
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,57 @@ | ||
/** | ||
* For the number of given strings N, and the length of the longest string M, | ||
* | ||
* Encode | ||
* - Time complexity: O(N) | ||
* - Space complexity: O(1) | ||
* | ||
* Decode | ||
* - Time complexity: O(NM) | ||
* - Space complexity: O(M) | ||
*/ | ||
|
||
class Codec { | ||
public: | ||
|
||
// Encodes a list of strings to a single string. | ||
string encode(vector<string>& strs) { | ||
string res = ""; | ||
for (auto str : strs) { | ||
res += to_string(str.size()); | ||
res.push_back('.'); | ||
res += str; | ||
} | ||
return res; | ||
} | ||
|
||
// Decodes a single string to a list of strings. | ||
vector<string> decode(string s) { | ||
vector<string> res; | ||
|
||
auto it = s.begin(); | ||
DaleSeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while (it != s.end()) { | ||
int str_size = 0; | ||
string tmp = ""; | ||
|
||
while (*it != '.') { | ||
str_size = str_size * 10 + (*it - '0'); | ||
it++; | ||
} | ||
|
||
it++; | ||
|
||
for (int i = 0; i < str_size; i++) { | ||
tmp.push_back(*it); | ||
it++; | ||
} | ||
|
||
res.push_back(tmp); | ||
} | ||
|
||
return res; | ||
} | ||
}; | ||
|
||
// Your Codec object will be instantiated and called as such: | ||
// Codec codec; | ||
// codec.decode(codec.encode(strs)); |
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,22 @@ | ||
/** | ||
* For length of given strings N, | ||
* | ||
* Time complexity: O(N) | ||
* - iteration for given strings | ||
* | ||
* Space complexity: O(1) | ||
* - the size of the container `count` is constant | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
bool isAnagram(string s, string t) { | ||
int count[26] = {0}; | ||
|
||
for (auto c : s) count[c - 'a']++; | ||
for (auto c : t) count[c - 'a']--; | ||
|
||
for (int i = 0; i < 26; i++) if (count[i]) return false; | ||
return true; | ||
} | ||
}; |
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.
이 풀이 너무 아름답습니다! 😍
혹시 다음 모임 때 다른 멤버들한테 설명 좀 해주실 수 있으실까요?
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.
네 ㅎㅎ 준비해보겠습니다
(+ line 여러개에 대해 코멘트를 남길 수도 있군요.. 배워갑니다!)