ํ์ด์ฌ ์๊ณ ๋ฆฌ์ฆ ์ธํฐ๋ทฐ 6์ฅ - ๋ฌธ์์ด ์กฐ์
๋ฌธ์์ด์ ๋ค๋ฃจ๋ ์ ํ์ ๋ฌธ์ ๋ฅผ ๋ ํ์ด๋ด์ผ๊ฒ ๋ค. ๋ฑ ๋ฌธ์ ๋ง ๋ด์๋ ์ด๋ป๊ฒ ํด๊ฒฐํด์ผํ ์ง ๊ฐ์ด ์ ์กํ๋ค. ๐ข
์ ํจํ ํฐ๋ฆฐ๋๋กฌ
๋ด๊ฐ ์์ฑํ ์ฝ๋
def isPalindrome(self, s: str) -> bool:
filter_string = ""
for a in s.lower():
if a.isalnum():
filter_string += a
return filter_string == filter_string[::-1]
ํ์ด1. ๋ฆฌ์คํธ๋ก ๋ณํ
def isPalindrome(self, s: str) -> bool:
strs = []
for char in s:
if char.isalnum():
strs.append(char.lower())
# ํฐ๋ฆฐ๋๋กฌ ํ๋ณ ์ฌ๋ถ
while len(strs) > 1:
if strs.pop(0) != strs.pop():
return False
return True
ํ์ด2. Deque ์๋ฃํ์ ์ด์ฉํ ์ต์ ํ
def isPalindrome(self, s: str) -> bool:
strs: Deque = collections.deque()
for char in s:
if char.isalnum():
strs.append(char.lower())
while len(strs) > 1:
if strs.popleft() !== strs.pop():
return False
return True
ํ์ด3. ์ฌ๋ผ์ด์ฑ ์ฌ์ฉ
def isPalindrome(self, s: str) -> bool:
s = s.lower()
# ์ ๊ท์์ผ๋ก ๋ถํ์ํ ๋ฌธ์ ํํฐ๋ง
s = re.sub('[^a-z0-9]', '', s)
return s == s[::-1] # ์ฌ๋ผ์ด์ฑ
๋ฌธ์์ด ๋ค์ง๊ธฐ
๋ด๊ฐ ์์ฑํ ์ฝ๋
def reverseString(self, s: List[str]) -> None:
s.reverse()
ํ์ด1. ํฌ ํฌ์ธํฐ๋ฅผ ์ด์ฉํ ์ค์
def reverseString(self, s: List[str]) -> None:
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
ํ์ด2. ํ์ด์ฌ๋ค์ด ๋ฐฉ์
๋ด๊ฐ ์์ฑํ ์ฝ๋์ ๋์ผ
๋ก๊ทธ ํ์ผ ์ฌ์ ๋ ฌ
๐ก 2๊ฐ ์ด์์ ์กฐ๊ฑด์ผ๋ก ์ ๋ ฌํ ๋ ๋๋ค๋ฅผ ์ฌ์ฉํด์ ์ ๋ ฌํ๋ ๊ฒ์ ์๊ฒ๋์๋ค.
ํ์ด1. ๋๋ค์ + ์ฐ์ฐ์๋ฅผ ์ด์ฉ
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๊ฐ์ ํค๋ฅผ ๋๋ค ํํ์์ผ๋ก ์ ๋ ฌ
letters.sort(key=lambda x: (x.split()[1:], x.split()[0]))
return letters + digits
๊ฐ์ฅ ํํ ๋จ์ด
๐ก Counter ๊ฐ์ฒด ์ฌ์ฉ ๋ฐฉ๋ฒ ์์งํ๊ธฐ
๋ด๊ฐ ์์ฑํ ์ฝ๋
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
words = re.sub('[^\w]', ' ', paragraph.lower()).split()
dic = dict()
for word in words:
if not word in banned:
dic[word] = dic.get(word, 0) + 1
return max(dic.keys(), key=lambda x: dic[x])
ํ์ด1. ๋ฆฌ์คํธ ์ปดํ๋ฆฌํจ์ , Counter ๊ฐ์ฒด ์ฌ์ฉ
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
words = [word for word in re.sub(r'[^\w]', ' ', paragraph).lower().split() if word not in banned]
counts = collections.Counter(words)
return counts.most_common(1)[0][0]
๊ทธ๋ฃน ์ ๋๊ทธ๋จ
ํ์ด1. ์ ๋ ฌํ์ฌ ๋์ ๋๋ฆฌ์ ์ถ๊ฐ
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = collections.defaultdict(list)
for word in strs:
# ์ ๋ ฌํ์ฌ ๋์
๋๋ฆฌ์ ์ถ๊ฐ
anagrams[''.join(sorted(word))].append(word)
return anagrams.values()
๊ฐ์ฅ ๊ธด ํฐ๋ฆฐ๋๋กฌ ๋ถ๋ถ ๋ฌธ์์ด
๐ก ํฌ ํฌ์ธํฐ ๊ฐ๋
์์งํ๊ณ ๋น์ทํ ์ ํ์ ๋ฌธ์ ํ์ด๋ณด๊ธฐ
ํ์ด1. ์ค์์ ์ค์ฌ์ผ๋ก ํ์ฅํ๋ ํ์ด
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
return s[left + 1:right - 1]
# ํด๋น ์ฌํญ์ด ์์ ๋ ๋น ๋ฅด๊ฒ ๋ฆฌํด
if len(s) < 2 orf s == s[::-1]:
return s
result = ''
# ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ์ฐ์ธก์ผ๋ก ์ด๋
for i in range(len(s) - 1):
result = max(result,
expand(i, i+1),
expand(i, i+2),
key=len)
return result
๋ฐ์ํ
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote