-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hyogshin] WEEK 03 solutions #1803
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
7 commits
Select commit
Hold shift + click to select a range
e9a35e3
feat: add 0220 Valid Palindrome solution
hyogshin dfab2ef
feat: add 0232 Number of 1 Bits solution
hyogshin f5cbc1c
docs: add time and space complexity
hyogshin df24d9f
chore: insert a line break at the end of file
hyogshin ea2e3d2
docs: add time and space complexity
hyogshin f5fddf4
chore: merge upstream/main
hyogshin fbf5c6f
docs: insert a line break at the end of file
hyogshin 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,13 @@ | ||
''' | ||
풀이 | ||
- 파이썬3 내장 함수인 bit_count() 함수 이용 | ||
시간 복잡도: O(1) | ||
공간 복잡도: O(1) | ||
''' | ||
|
||
class Solution: | ||
def hammingWeight(self, n: int) -> int: | ||
return n.bit_count() | ||
|
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,26 @@ | ||
''' | ||
풀이 | ||
- alphanumeric만 저장하는 alnum 배열 생성 | ||
- alnum 배열의 절반 (소수점 버림) 길이를 순회하며 Palindrome 인지 확인 | ||
시간 복잡도: O(n) | ||
- for loop * 2 -> O(2n) => O(n) | ||
공간 복잡도: O(n) | ||
- alphanumeric만 저장하는 alnum 배열이 최악의 경우 O(n) 일 수 있음 | ||
''' | ||
|
||
class Solution: | ||
def isPalindrome(self, s: str) -> bool: | ||
alnum = [] | ||
is_pal = True | ||
for c in s: | ||
if c.isalnum(): | ||
alnum.append(c.lower()) | ||
|
||
for c in range(len(alnum) // 2): | ||
if alnum[c] != alnum[-1 - c]: | ||
is_pal = False | ||
break | ||
|
||
return is_pal | ||
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. is_pal 변수를 쓰지 않고 불일치 시 바로 return False를 하고 마지막에 return True만 남기면 더 간단해집니다 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. 더 간단한 방법이 있었네요! |
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.
-> alnum = [c.lower() for c in s if c.isalnum()]처럼 한 줄로 간결하게 작성 가능