-
-
Notifications
You must be signed in to change notification settings - Fork 245
[SunaDu] Week 1 #628
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
[SunaDu] Week 1 #628
Changes from 4 commits
f4ac433
227415b
75e6d44
be09081
9a395e9
86658cb
395f249
e6c66d5
89739e6
24792fe
3dff920
055969d
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,23 @@ | ||
''' | ||
# Leetcode 217. Contains Duplicate | ||
|
||
use set to store distinct elements 🗂️ | ||
|
||
## Time and Space Complexity | ||
|
||
``` | ||
TC: O(n) | ||
SC: O(n) | ||
``` | ||
|
||
### TC is O(n): | ||
- iterating through the list just once to convert it to a set. | ||
|
||
### SC is O(n): | ||
- creating a set to store the distinct elements of the list. | ||
''' | ||
|
||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
return len(nums) != len(set(nums)) | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,28 @@ | ||||||
''' | ||||||
# Leetcode 125. Valid Palindrome | ||||||
|
||||||
use regex to filter out non-alphanumeric characters 🔍 | ||||||
|
||||||
## Time and Space Complexity | ||||||
|
||||||
``` | ||||||
TC: O(n) | ||||||
SC: O(n) | ||||||
``` | ||||||
|
||||||
### TC is O(n): | ||||||
- iterating through the string just once to filter out non-alphanumeric characters. | ||||||
|
||||||
### SC is O(n): | ||||||
- creating a new string to store the filtered characters. | ||||||
''' | ||||||
|
||||||
class Solution: | ||||||
def isPalindrome(self, s: str) -> bool: | ||||||
if s is " ": | ||||||
|
if s == " ": |
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.
지금도 사실 짧은 문자열이라 사실 결과값은 같으니까 문제 없는 코드라고 생각됩니다!
Outdated
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.
regex가 간편해서 저도 사용하긴 했는데 사실 regex보단 내장함수들이 성능이 좋을거라 gpt한테 물어봤더니 이런 코드를 제안해주네요! (코드가 그렇게 가독성이 좋진 않은 것 같아서 좋은진 잘 모르겠습니다... ㅎ..)
class Solution:
def isPalindrome(self, s: str) -> bool:
# 문자열을 소문자로 변환하고 알파벳과 숫자만 남김
converted_s = ''.join(c for c in s.lower() if c.isalnum())
# 회문 여부 판단
return converted_s == converted_s[::-1]
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.
코멘트 잘 봤어요!
말씀하신 대로 내장함수 기반 접근이 regex보다 성능상 유리할 것 같습니다😀
그리고 제너레이터 표현식을 사용하는 것도 효율성 면에서 좋은 것 같습니다.
약간의 가독성을 향상을 위해서 s.lower만 분리해서 적용해봤어요
leetcode-study/valid-palindrome/dusunax.py
Lines 25 to 26 in e6c66d5
s = s.lower() | |
converted_s = ''.join(c for c in s if c.isalnum()) |
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.
파이썬 라이브러리와 문법은 정말.. 알고리즘 풀기에 행복한 수준이네요 ㅎㅎ
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.
ㅎㅎㅎㅎ 공감합니다