-
-
Notifications
You must be signed in to change notification settings - Fork 195
[printjin-gmailcom] WEEK 02 Solutions #1219
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 all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d19576f
Solve : Valid Anagram
printjin-gmailcom f01effa
Refactor : Valid Anagram
printjin-gmailcom a1e3845
Merge branch 'DaleStudy:main' into main
printjin-gmailcom 26d68fc
Solve : climbing-Stairs
printjin-gmailcom a1707ad
Solve : Product of Array Except Self
printjin-gmailcom 914d94b
Refactor : Product of Array Except Self
printjin-gmailcom cefa7c9
Solve : 3sum
printjin-gmailcom af1d46c
Refactor : 3sum
printjin-gmailcom edabe77
Solve : Validate Binary Search Tree
printjin-gmailcom b4d2e82
Fix : Lint Error
printjin-gmailcom eeb7c5f
Fix : Lint Error
printjin-gmailcom 376c8c4
Fix : Lint Error
printjin-gmailcom 6177297
Refactor : 3sum
printjin-gmailcom 84b3725
Refactor : 3sum
printjin-gmailcom 09faf30
Refactor : 3sum
printjin-gmailcom 0823a9f
Refactor : Validate Binary Search Tree
printjin-gmailcom 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,22 @@ | ||
class Solution: | ||
def threeSum(self, nums): | ||
triplets = [] | ||
nums.sort() | ||
for i in range(len(nums) - 2): | ||
if i > 0 and nums[i - 1] == nums[i]: | ||
continue | ||
low, high = i + 1, len(nums) - 1 | ||
while low < high: | ||
three_sum = nums[i] + nums[low] + nums[high] | ||
if three_sum < 0: | ||
low += 1 | ||
elif three_sum > 0: | ||
high -= 1 | ||
else: | ||
triplets.append([nums[i], nums[low], nums[high]]) | ||
while low < high and nums[low] == nums[low + 1]: | ||
low += 1 | ||
while low < high and nums[high] == nums[high - 1]: | ||
high -= 1 | ||
low, high = low + 1, high - 1 | ||
return triplets |
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,8 @@ | ||
class Solution: | ||
def climbStairs(self, n: int): | ||
if n <= 2: | ||
return n | ||
a, b = 1, 2 | ||
for _ in range(3, n+1): | ||
a, b = b, a + b | ||
return b |
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,13 @@ | ||
class Solution: | ||
def productExceptSelf(self, nums): | ||
n = len(nums) | ||
answer = [1] * n | ||
left = 1 | ||
for i in range(n): | ||
answer[i] = left | ||
left *= nums[i] | ||
right = 1 | ||
for i in range(n-1, -1, -1): | ||
answer[i] *= right | ||
right *= nums[i] | ||
return answer |
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,5 @@ | ||
from collections import Counter | ||
|
||
class Solution: | ||
def isAnagram(self, s: str, t: str): | ||
return Counter(s) == Counter(t) | ||
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,16 @@ | ||
class Solution: | ||
def isValidBST(self, root): | ||
max_val = float("-inf") | ||
def dfs(node): | ||
if not node: | ||
return True | ||
nonlocal max_val | ||
if not dfs(node.left): | ||
return False | ||
if max_val >= node.val: | ||
return False | ||
max_val = node.val | ||
if not dfs(node.right): | ||
return False | ||
return True | ||
return dfs(root) | ||
Comment on lines
+1
to
+16
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. 저는 트리 탐색의 결과를 List 로 저장을 하여 최종적으로 오름차순 검증을 하였는데, |
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.
python에서 제공하는 Counter 자료구조를 사용하면 코드가 정말 간단해지네요!