개발일기
하루에 한가지라도 배울점을 찾기
-
(LeetCode)125. 유효한 팰린드롬
(LeetCode)125. 유효한 팰린드롬 문제 Valid Palindrome - LeetCode 아이디어 정규식으로 불필요한 문자를 필터링한 후 슬라이싱으로 문자열을 뒤집어 기존 문자열과 비교 풀이 class Solution: def isPalindrome(self, s: str) -> bool: # 소문자로 치환 s = s.lower() #정규식으로 불칠요한 문자 필터링 s = re.sub('[^0-9a-z]', '', s) return s == s[::-1] # 슬라이싱...
-
(LeetCode)49. 그룹 애너그램
(LeetCode)49. 그룹 애너그램 문제 Group Anagrams - LeetCode 아이디어 정렬하여 딕셔너리에 추가한후에 일괄로 출력함 정렬된 문자열을 key로, 원본 문자열을 value로 저장 코드 class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: # 기존에 등록된 key없는경우 빈 리스트를 추가 anagrams = collections.defaultdict(list) for word in strs: # 정렬하여 딕셔너리에 추가 anagrams[''.join(sorted(word))].append(word) return...
-
(LeetCode)5. 가장 긴 팰린드롬 부분 문자열
(LeetCode)5. 가장 긴 팰린드롬 부분 문자열 문제 Longest Palindromic Substring - LeetCode 아이디어 투 포인터가 중앙을 중심으로 확장하는 형태로 풀이 코드 class Solution: def longestPalindrome(self, s: str) -> str: # 팰린드롬 판별 및 투포인터 확장 def expand(left: int, right: int) -> str: while left>=0 and right<=len(s) and s[left]==s[right-1]: left-=1 right+=1...
-
(LeetCode)819. 가장 흔한 단어
(LeetCode)819. 가장 흔한 단어 문제 Most Common Word - LeetCode 아이디어 리스트 컴프리헨션과 딕셔너리의 Count객체를 활용한 풀이 코드 class Solution: def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: # \w는 단어 문자를 뜻하며, ^는 not을 뜻함 [^\w]는 문자가 아닌 모든항목을 공백으로 치환 words = [word for word in re.sub('[^\w]', '...
-
(LeetCode)937. 로그 파일 재정렬
(LeetCode)937. 로그 파일 재정렬 문제 Reorder Data in Log Files - LeetCode 아이디어 람다표현식을 이용한 정렬과 + 연산자를 사용 코드 def reorderLogFiles(self, logs: List[str]) -> List[str]: letters, digits = [], [] # 문자로 이루어진 로그와 숫자로 이루어진 로그를 분리 for log in logs: if log.split()[1].isdigit(): digits.append(log) else: letters.append(log) # 2개의...