-
-
Notifications
You must be signed in to change notification settings - Fork 305
[doh6077] WEEK 03 solutions #2096
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
Changes from all commits
810bbf6
94ee1a8
ce712fc
ca024ee
e6d4e0d
f189d0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| class Solution: | ||
|
|
||
| def solve(self, candidates, remsum, cur, res, idx) : | ||
| if remsum == 0 : | ||
| res.append(list(cur)) | ||
| return | ||
|
|
||
| if remsum < 0 or idx >= len(candidates) : | ||
| return | ||
| cur.append(candidates[idx]) | ||
| self.solve(candidates, remsum-candidates[idx],cur,res,idx) | ||
| cur.pop() | ||
| self.solve(candidates,remsum,cur,res,idx+1) | ||
|
|
||
|
|
||
| def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
| cur = [] | ||
| res = [] | ||
| self.solve(candidates,target,cur,res,0) | ||
| return res |
|
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.
개인적으로 이 코드를 봤을 때, cur값이 제대로 작동하나 싶었는데, 애초에 중간에 두 조건이 충족하지 않으면 그냥 decode가 안되는 것이네요 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| class Solution: | ||
| def numDecodings(self, s: str) -> int: | ||
| if s[0] == '0': | ||
| return 0 | ||
| n = len(s) | ||
| cur = 1 | ||
| pre1 = pre2 = 1 | ||
| for i in range(2, n+1): | ||
| one = int(s[i-1]) | ||
| two = int(s[i-2:i]) | ||
| cur = 0 | ||
| if 1 <= one <= 9: | ||
| cur += pre1 | ||
| if 10 <= two <= 26: | ||
| cur += pre2 | ||
|
|
||
| pre2 = pre1 | ||
| pre1 = cur | ||
|
|
||
| return cur |
|
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. 간결하게 for문으로 나아가며
저는 되게 복잡하게 생각해서 풀이가 이상한데, 잘 작성하셨다 생각합니다 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| res = nums[0] | ||
| total = 0 | ||
|
|
||
| for n in nums: | ||
| if total < 0: | ||
| total = 0 | ||
|
|
||
| total += n | ||
| res = max(res, total) | ||
|
|
||
| return res |
|
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. 비트 연산자인 &를 사용하셨네요! 나중에 코테에서 비트와 관련한 문제가 나오면 사용해봐야겠습니다 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| ans = 0 | ||
| while n != 0: | ||
| ans += 1 | ||
| n = n & ( n-1) | ||
| return ans |
|
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.
Contributor
Author
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. 안녕하세요! 리뷰 남겨주셔서 감사합니다 ㅎㅎ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import re | ||
| # two pointers | ||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| # remove all alphanumeric characters and convert all uppercase letters into lowercase letters | ||
| cleaned_s = re.sub(r'[^a-zA-Z0-9]', '', s).lower() | ||
| n = len(cleaned_s) | ||
| left, right = 0, n -1 | ||
| while left < right: | ||
| if cleaned_s[left] == cleaned_s[right]: | ||
| left += 1 | ||
| right -= 1 | ||
| else: | ||
| return False | ||
|
|
||
| return True |
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.
현재 문제 푸는 함수 바로 위에 따로 함수를 작성한 것이 인상깊었습니다!
이렇게 풀면 내부에서 직접 작성하는 함수보다 분리된다는 느낌이 드네요