-
-
Notifications
You must be signed in to change notification settings - Fork 335
[liza0525] WEEK 15 Solutions #2637
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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f21e536
[7th batch] week 15 - subtree of another tree
liza0525 b197155
[7th batch] week 15 - constract binary tree from preorder and inorder…
liza0525 90c654c
[7th batch] week 15 - longest palindromic substring
liza0525 e3ade7e
[7th batch] week 15 - rotate image
liza0525 399a0b9
[7th batch] week 14 - house robber ii
liza0525 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
29 changes: 29 additions & 0 deletions
29
construct-binary-tree-from-preorder-and-inorder-traversal/liza0525.py
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,29 @@ | ||
| class Solution: | ||
| def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
| inorder_idx_map = { | ||
| val: idx | ||
| for idx, val in enumerate(inorder) | ||
| } | ||
|
|
||
| def dfs(pre_start, pre_end, in_start, in_end): | ||
| if pre_start > pre_end or in_start > in_end: | ||
| return None | ||
|
|
||
| node = TreeNode() | ||
| node.val = preorder[pre_start] | ||
|
|
||
| in_idx = inorder_idx_map[node.val] | ||
| left_size = in_idx - in_start | ||
|
|
||
| node.left = dfs( | ||
| pre_start + 1, pre_start + left_size, | ||
| in_start, in_idx - 1, | ||
| ) | ||
| node.right = dfs( | ||
| pre_start + left_size + 1, pre_end, | ||
| in_idx + 1, in_end, | ||
| ) | ||
|
|
||
| return node | ||
|
|
||
| return dfs(0, len(preorder) - 1, 0, len(inorder) - 1) |
|
liza0525 marked this conversation as resolved.
|
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,31 @@ | ||
| class Solution: | ||
| def longestPalindrome(self, s: str) -> str: | ||
| res = "" | ||
| len_res = 0 | ||
| len_s = len(s) | ||
|
|
||
| for i in range(len_s): | ||
| left, right = i, i | ||
| while 0 <= left and right < len_s: | ||
| if s[left] == s[right]: | ||
| if right - left + 1 > len_res: | ||
| res = s[left:right + 1] | ||
| len_res = right - left + 1 | ||
| left -= 1 | ||
| right += 1 | ||
| else: | ||
| break | ||
|
|
||
| for i in range(len_s - 1): | ||
| left, right = i, i + 1 | ||
| while 0 <= left and right < len_s: | ||
| if s[left] == s[right]: | ||
| if right - left + 1 > len_res: | ||
| res = s[left:right + 1] | ||
| len_res = right - left + 1 | ||
| left -= 1 | ||
| right += 1 | ||
| else: | ||
| break | ||
|
|
||
| return res |
|
liza0525 marked this conversation as resolved.
|
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,21 @@ | ||
| class Solution: | ||
| def rotate(self, matrix: List[List[int]]) -> None: | ||
| """ | ||
| Do not return anything, modify matrix in-place instead. | ||
| """ | ||
| len_matrix = len(matrix) | ||
|
|
||
|
|
||
| for i in range(len_matrix // 2): | ||
| for j in range(i, len_matrix - i - 1): | ||
| ( | ||
| matrix[i][j], | ||
| matrix[j][len_matrix - i - 1], | ||
| matrix[len_matrix - i - 1][len_matrix - j - 1], | ||
| matrix[len_matrix - j - 1][i], | ||
| ) = ( | ||
| matrix[len_matrix - j - 1][i], | ||
| matrix[i][j], | ||
| matrix[j][len_matrix - i - 1], | ||
| matrix[len_matrix - i - 1][len_matrix - j - 1], | ||
| ) | ||
|
Comment on lines
+9
to
+21
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. in-place 4-way swap을 튜플 할당으로 한 번에 처리해 주셔서, 임시 변수를 여러 개 두는 방식보다 훨씬 읽기 쉽고 깔끔한 코드가 된 것 같아요! |
||
|
liza0525 marked this conversation as resolved.
|
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,32 @@ | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: | ||
| def check_same(node, subnode): | ||
| if not node and not subnode: | ||
| return True | ||
| elif node and subnode: | ||
|
|
||
| if node.val != subnode.val: | ||
| return False | ||
|
|
||
| return ( | ||
| check_same(node.left, subnode.left) | ||
| and check_same(node.right, subnode.right) | ||
| ) | ||
| return False | ||
|
|
||
| def dfs(node): | ||
| if not node: | ||
| return False | ||
|
|
||
| if node.val == subRoot.val and check_same(node, subRoot): | ||
| return True | ||
|
|
||
| return dfs(node.left) or dfs(node.right) | ||
|
|
||
| return dfs(root) |
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.