-
-
Notifications
You must be signed in to change notification settings - Fork 247
[chordpli] Week3 Solutions #1808
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
5 commits
Select commit
Hold shift + click to select a range
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,29 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
result = [] | ||
candidates.sort() | ||
self.__backtrack__(candidates, 0, target, result, []) | ||
return result | ||
|
||
def __backtrack__(self, candidates: List[int], current_idx: int, target: int, result: List[List[int]], | ||
current_arr: List[int]): | ||
if current_idx >= len(candidates): | ||
return | ||
|
||
add_value = sum(current_arr) + candidates[current_idx] | ||
|
||
if add_value == target: | ||
current_arr.append(candidates[current_idx]) | ||
result.append(current_arr.copy()) | ||
current_arr.pop() | ||
|
||
if add_value > target: | ||
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. 이 조건 이후 바로 return해서 다음 인덱스로 넘어가는 아이디어는 좋으나 너무 깊은 재귀가 발생할 수 있어 주의가 필요합니다 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. 오.. 감사합니다! 비슷한 상황이 있을 때, 참고해서 회피하도록 하겠습니다! |
||
return self.__backtrack__(candidates, current_idx + 1, target, result, current_arr) | ||
|
||
current_arr.append(candidates[current_idx]) | ||
self.__backtrack__(candidates, current_idx, target, result, current_arr) | ||
current_arr.pop() | ||
self.__backtrack__(candidates, current_idx + 1, target, result, current_arr) |
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,3 @@ | ||
class Solution: | ||
def hammingWeight(self, n: int) -> int: | ||
return bin(n).count('1') |
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 @@ | ||
import re | ||
|
||
|
||
class Solution: | ||
def isPalindrome(self, s: str) -> bool: | ||
lower_s = s.lower() | ||
lower_eng_str = re.sub(r"[^a-z0-9]", "", lower_s) | ||
reverse_str = lower_eng_str[::-1] | ||
|
||
if lower_eng_str == reverse_str: | ||
return True | ||
|
||
return False |
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.
매번 sum(current_arr)을 계산하면 비효율적이기에 현재 합계를 변수로 관리하는 방법 추천드립니다