-
-
Notifications
You must be signed in to change notification settings - Fork 245
[yyyyyyyyyKim] WEEK 03 solutions #1287
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
6 commits
Select commit
Hold shift + click to select a range
a9df701
valid-palindrome solution
yyyyyyyyyKim 0fb50a8
valid-palindrome solution
yyyyyyyyyKim c20a0af
number-of-1-bits solution
yyyyyyyyyKim ffaa0f7
combination-sum solution
yyyyyyyyyKim f3d756f
decode-ways solution
yyyyyyyyyKim e63ffbe
maximum-subarray solution
yyyyyyyyyKim 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,25 @@ | ||
class Solution: | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
|
||
answer = [] | ||
|
||
# 재귀 | ||
def backtrack(start, path, total): | ||
# total이 target과 같아지면 path복사해서 answer에 추가하고 종료 | ||
if total == target: | ||
answer.append(path[:]) | ||
return | ||
|
||
# total이 target값 넘어가면 종료 | ||
if total > target: | ||
return | ||
|
||
for i in range(start, len(candidates)): | ||
path.append(candidates[i]) # 일단 path에 추가하고 | ||
backtrack(i, path, total + candidates[i]) # 검증하기 | ||
path.pop() # 마지막 값 꺼내고 다음으로 | ||
|
||
# backtrack 함수호출 | ||
backtrack(0, [], 0) | ||
|
||
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,24 @@ | ||
class Solution: | ||
def numDecodings(self, s: str) -> int: | ||
|
||
# DP | ||
dp = [0]*(len(s)+1) | ||
|
||
# s가 0으로 시작하면 0 return | ||
if s[0] == '0': | ||
return 0 | ||
|
||
dp[0] = 1 # 빈문자열은 해석가능한 1가지 경우로 취급 (초기기준점 역할, dp[i-2]계산시필요) | ||
dp[1] = 1 # 첫번째자리의 처리방법은 1가지 | ||
|
||
# len(s)가 2 이상일때 | ||
for i in range(2,len(s)+1): | ||
one = int(s[i-1]) # 한자리(현재자리) | ||
two = int(s[i-2:i]) # 한자리 + 앞자리 = 두자리 | ||
|
||
if 1 <= one <= 9: | ||
dp[i] += dp[i-1] | ||
if 10 <= two <= 26: | ||
dp[i] += dp[i-2] | ||
|
||
return dp[len(s)] |
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,14 @@ | ||
class Solution: | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
# 시간복잡도: O(n) - nums 배열을 한 번만 순회함 | ||
# 공간복잡도: O(n) - dp 배열을 nums 길이만큼 생성 | ||
|
||
# DP | ||
dp = [0]*len(nums) | ||
dp[0] = nums[0] # 초기화 | ||
|
||
for i in range(1,len(nums)): | ||
# 현재값과 (이전까지의 합 + 현재값) 중 더 큰 값을 dp[i]에 저장 | ||
dp[i] = max(nums[i], nums[i]+dp[i-1]) | ||
|
||
return max(dp) |
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,9 @@ | ||
class Solution: | ||
def hammingWeight(self, n: int) -> int: | ||
answer = 0 | ||
|
||
while n > 0: | ||
answer += n%2 #나머지(현재 비트가 1이면 ++) | ||
n //= 2 #몫(다음 비트로 이동) | ||
|
||
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,14 @@ | ||
class Solution: | ||
def isPalindrome(self, s: str) -> bool: | ||
|
||
# 소문자로 변경 | ||
s = s.lower() | ||
p = "" | ||
|
||
# 문자,숫자만 뽑기 | ||
for i in range(len(s)): | ||
if (ord(s[i]) > 96 and ord(s[i]) < 123) or (ord(s[i]) >= 48 and ord(s[i]) <= 57): | ||
p += s[i] | ||
|
||
# 문자열 뒤집기 | ||
return p == p[::-1] | ||
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. p[::-1]을 활용하여 문자열을 효율적으로 뒤집는 방법을 사용한 점 👍 |
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.
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.
path로 리스트를 복사 후 추가하는 부분이 좋았습니다 😀
전체적으로 코드가 간결하고 각 부분에 주석을 달아 로직을 이해하기 쉬웠어요