forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fbffa7e
commit 88fa0ef
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
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,30 @@ | ||
# 2Sum์ ํ์ฉํ ํ์ด | ||
# Note : ์ ๋ ฌ ํ ํธ๋ ๊ฒ์ด ๋ ์ง๊ด์ ์ด๊ณ , ๋น ๋ฆ | ||
|
||
""" | ||
๋ณต์ก๋ : ์์ -> ์์ํ ์ด์ | ||
์๊ฐ ๋ณต์ก๋ : O(n^2) -> nums ๋ฐฐ์ด 2์ค for๋ฌธ | ||
๊ณต๊ฐ ๋ณต์ก๋ : O(n) -> ์ต์ ์ ๊ฒฝ์ฐ nums ๋ฐฐ์ด ๊ธธ์ด๋งํผ์ ๋์ ๋๋ฆฌ ์์ฑ | ||
""" | ||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
ans_list = set() | ||
added_pair = {} | ||
for target_i, target_number in enumerate(nums): | ||
cur_ans_list = [] | ||
num_dict = {} | ||
if target_number in added_pair: | ||
continue | ||
|
||
for i in range(target_i + 1, len(nums)): | ||
num_A = nums[i] | ||
num_B = - target_number - num_A | ||
if num_B in num_dict: | ||
cur_ans_list.append(sorted([target_number, num_A, num_B])) | ||
added_pair[target_number] = num_A | ||
num_dict[num_A] = 1 | ||
for item in cur_ans_list: | ||
ans_list.add(tuple(item)) | ||
return list(ans_list) | ||
|
59 changes: 59 additions & 0 deletions
59
construct-binary-tree-from-preorder-and-inorder-traversal/heypaprika.py
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,59 @@ | ||
# 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 | ||
|
||
# preorder -> root, left, right | ||
# inorder -> left, root, right | ||
|
||
# Ex1 [3,9,20,null,null,15,7] | ||
# preorder -> root-1, left, (root-2 - root, left, right) | ||
# inorder -> left, root-1, (root-2 - left, root, right) | ||
|
||
# Ex2 [-1] | ||
# preorder -> root, left(x), right(x) | ||
# inorder -> left(x), root, right(x) | ||
|
||
# Ex3 [3,9,20,11,8,15,7] | ||
# preorder -> root-1, (root-2(left) - root, left, right), (root-3(right) - root, left, right) | ||
# [3,9,11,8,20,15,7] | ||
# inorder -> (root-1(left) - left, root, right), root-2, (root-3(right) - left, root, right) | ||
# [11, 9, 8, 3, 15, 20, 7] | ||
|
||
# Ex4 [3,9,20,11,8] | ||
# preorder -> root-1, (root-2(left) - root, left, right), (root-3(right) - root) | ||
# [3,9,11,8,20] | ||
# inorder -> (root-1(left) - left, root, right), root-2, (root-3(right) - left(X), root) | ||
# [11, 9, 8, 3, 20] | ||
|
||
# ๋ฌธ์ ํ์ด : divide and conquer | ||
# preorder์ ์ฒซ๋ฒ์งธ ์์๋ฅผ ๊ฐ์ง๊ณ inorder split | ||
# split๋ left์ right์ ๊ฐ์์ ๋ง๊ฒ preorder์ ๋ ๋ฒ ์งธ ์์๋ถํฐ ์์๋๋ก ํ ๋น | ||
# ์) left ์์ ๊ฐ์ : 3 -> preorder[1:1+3], right ์์ ๊ฐ์ : 2 -> preorder[1+3:] | ||
# left, right ๊ฐ๊ฐ buildTree๋ก ๋ฃ๊ธฐ | ||
|
||
""" | ||
๋ณต์ก๋ : ์์ -> ์์ํ ์ด์ | ||
์๊ฐ ๋ณต์ก๋ : O(n) -> ๋ถํ ํด์ ์ฒ๋ฆฌํ์ง๋ง, ๋ชจ๋ ๋ ธ๋๋ฅผ ๋ฐฉ๋ฌธํ๋ฏ๋ก | ||
๊ณต๊ฐ ๋ณต์ก๋ : O(n) -> ์ ๋ ฅ ๋ฐฐ์ด๋งํผ์ ๊ธธ์ด์ ๊ฑฐ์ ๋์ผํ ๋ฐฐ์ด์ด ์์ฑ๋๋ฏ๋ก | ||
""" | ||
class Solution: | ||
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
if len(preorder) == 0: | ||
return None | ||
if len(preorder) == 1: | ||
return TreeNode(preorder[0]) | ||
root = TreeNode(preorder[0]) | ||
|
||
split_idx = inorder.index(preorder[0]) | ||
left_inorder = inorder[:split_idx] | ||
right_inorder = inorder[split_idx+1:] | ||
left_preorder = preorder[1:len(left_inorder)+1] | ||
right_preorder = preorder[len(left_inorder)+1:] | ||
root.left = self.buildTree(left_preorder, left_inorder) | ||
root.right = self.buildTree(right_preorder, right_inorder) | ||
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,20 @@ | ||
""" | ||
๋ณต์ก๋ : ์์ -> ์์ํ ์ด์ | ||
์๊ฐ ๋ณต์ก๋ : O(n) -> ๋ฐฐ์ด์ ๊ธธ์ด ๋งํผ ๋ฐ๋ณตํ๋ฏ๋ก | ||
๊ณต๊ฐ ๋ณต์ก๋ : O(n) -> n+1 ๊ธธ์ด์ ๋ฐฐ์ด ํ๋๋ฅผ ์์ฑํ๋ฏ๋ก | ||
""" | ||
class Solution: | ||
def numDecodings(self, s: str) -> int: | ||
if s[0] == '0': | ||
return 0 | ||
a = [0] * (len(s)+1) | ||
a[0] = 1 | ||
a[1] = 1 | ||
for i in range(2, len(s)+1): | ||
if 1 <= int(s[i-1]) <= 9: | ||
a[i] += a[i-1] | ||
if 10 <= int(s[i-2:i]) <= 26: | ||
a[i] += a[i-2] | ||
return a[-1] | ||
|