알고리즘
[Algorithms] 파이썬 알고리즘 인터뷰 #6, Leetcode 5번: Longest Palindromic Substring
gomduribo
2021. 3. 30. 19:52
나를 정말 힘들게 한 문제다... 이해를 정말 못하겠어서 5-6번은 읽고 또 읽은 거 같다...
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]:
left-=1
right+=1
return s[left+1:right]
if len(s)<2 and s==s[::-1]
return s
result=''
for i for range(len(s)-1):
result=max(result,expand(i,i+1),expand(i,i+2),key=len)
return result
딱히 어려운 문법은 없다. 메서드 안에 또다른 메서드를 선언해서 사용할 수 도 있다!라는 것만 기억해 두면 좋을듯