[LeetCode] Longest Common Prefix 파이썬 (zip, list comprehension, set)
class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: strs = [list(x) for x in strs] strs = zip(*strs) pre = "" for a in strs: if len(set(a)) == 1: pre += a[0] else: break return pre # output # [['f', 'l', 'o', 'w', 'e', 'r'], ['f', 'l', 'o', 'w'], ['f', 'l', 'i', 'g', 'h', 't']] # ('f', 'f', 'f') # a output # {'f'} # set(a) # 1 # len(set(a)) # ('l', 'l', 'l') # {'l'} # 1 # ('o'..
2021. 3. 28.
[LeetCode] Roman to Integer 파이썬 (dict, stack, deque)
from collections import deque class Solution: def romanToInt(self, s: str) -> int: symbol = {"IV":4, "IX":9, "XL":40, "XC":90, "CD":400, "CM":900} roman = {"I":1,"V":5,"X":10,"L":50,"C":100,"D":500, "M":1000, "IV":4, "IX":9, "XL":40, "XC":90, "CD":400, "CM":900} stack = deque() stack.append((s[0],0)) for i in range(1, len(s)): pattern = stack[-1][0]+s[i] if pattern in symbol: stack.pop() stack.a..
2021. 3. 26.