Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions combination-sum/doh6077.py
Copy link
Contributor

Choose a reason for hiding this comment

The 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,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
20 changes: 20 additions & 0 deletions decode-ways/doh6077.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onetwo로 각각 나눠서 확인한 후, 값들을 누적시키는군요

개인적으로 이 코드를 봤을 때, 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
13 changes: 13 additions & 0 deletions maximum-subarray/doh6077.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

간결하게 for문으로 나아가며

  • 현재 값이 0보다 작으면 초기화하고
  • 아니면 계속 더하는 방식이네요

저는 되게 복잡하게 생각해서 풀이가 이상한데, 잘 작성하셨다 생각합니다

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
7 changes: 7 additions & 0 deletions number-of-1-bits/doh6077.py
Copy link
Contributor

Choose a reason for hiding this comment

The 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,7 @@
class Solution:
def hammingWeight(self, n: int) -> int:
ans = 0
while n != 0:
ans += 1
n = n & ( n-1)
return ans
16 changes: 16 additions & 0 deletions valid-palindrome/doh6077.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re를 import하면 정규식으로 작성하여 주어진 문자열을 수정할 수 있군요

re도 배워갑니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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