728x90
LeetCode-5
Longest Palindrome Substring : ๊ฐ์ฅ ๊ธด Palindrome
note :
Myanswer
class Solution:
def longestPalindrome(self, s: str) -> str:
if len(s)==1:
return s[0]
for i in range(0,len(s)-1):
for j in range(0,i+1):
org = s[j:len(s)-(-j+i)]
if org[::-1]==org:
return org
return org[0]
Result : 6515ms Memory: 13.9mb
Solution
class Solution:
def longestPalindrome(self, s: str) -> str:
def expend(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 or s == s[::-1]:
return s
result = ''
for i in range(len(s)-1):
result = max(
result,
expend(i, i+1),
expend(i, i+2),
key=len
)
return result
Result : 103ms Memory: 13.9mb
๋ฐ์ํ
'๐ข One step' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetcode-42] Trapping Rain Water (0) | 2023.03.05 |
---|---|
[leetcode-1] Two Sum (0) | 2023.03.04 |
[leetcode-49] Group Anagrams (0) | 2023.03.02 |
LeetCode - Time complexity ์๊ฐ ๋ณต์ก๋ (0) | 2023.03.02 |
[leetcode-819] Most Common Word (0) | 2023.03.02 |