본문 바로가기

Leetcode98

[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.
[LeetCode] Palindrom Number 파이썬 (reverse str) class Solution: def isPalindrome(self, x: int) -> bool: if str(x) == str(x)[::-1]: return True else: return False Revese Integer에 있던 str reverse 활용함 메모리에서는 50%인데 시간 상위 솔루션은 class Solution: def isPalindrome(self, x: int) -> bool: if x bool: x = str(x) if x == x[::-1]: return True return False str전환을 한번만해서 빠르게 계산되도록 메모리 측면에서의 상위 솔루션 class Solution: def isPalindrome(self, x: int) -> bool: if x < 0:.. 2021. 3. 25.
LeetCode dark mode 설정하기 (extension) addons.mozilla.org/en-US/firefox/addon/darkreader/ Dark Reader – Get this Extension for 🦊 Firefox (en-US) Download Dark Reader for Firefox. Dark mode for every website. Take care of your eyes, use dark theme for night and daily browsing. addons.mozilla.org 2021. 3. 25.
[LeetCode] Revese Integer 파이썬 (list comprehension, reverse str) 36ms 걸린 내 솔루션 class Solution: def reverse(self, x: int) -> int: sign = "" if x (2**31)-1 or int(answer) int: if x > 0: result = int(str(x)[::-1]) else: result = -int(str(abs(x))[::-1.. 2021. 3. 25.
[LeetCode] Two Sum 파이썬 (dict, if ~ in) 풀이: class Solution: def twoSum(self, nums, target): seen = {} for i, v in enumerate(nums): remaining = target - v if remaining in seen: return [seen[remaining], i] seen[v] = i return [] enumerate를 사용해서 nums 리스트를 순회한다. target과 nums의 element를 뺀 나머지(remaining)가 seen의 key값으로 존재하는지 확인 존재한다면, seen의 remaininig 키값의 value와 현재 index(i)를 리스트의 원소로 각각 입력후 return seen 이라는 dictionary를 만들어서 value를 key값으로, index.. 2021. 3. 25.
198. House Robber Example 1: Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. Example 2: Input: nums = [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12. class Solution(object): def rob(self, nums): """ :type nums: L.. 2020. 8. 16.
303. Range Sum Query - Immutable class NumArray(object): def __init__(self, nums): """ initialize your data structure here. :type nums: List[int] """ self.sums = [0] * (len(nums) + 1) for i in xrange(len(nums)): self.sums[i+1] = self.sums[i] + nums[i] def sumRange(self, i, j): """ sum of elements nums[i..j], inclusive. :type i: int :type j: int :rtype: int """ return self.sums[j+1] - self.sums[i] 출처: leetcode.com/problems/range.. 2020. 8. 16.
53. Maximum Subarray class Solution: # @param A, a list of integers # @return an integer # 6:57 def maxSubArray(self, A): if not A: return 0 curSum = maxSum = A[0] for num in A[1:]: curSum = max(num, curSum + num) # max(current index value, current max sub array w/ current idx value) maxSum = max(maxSum, curSum) # get each sub array's max sub array return maxSum www.youtube.com/watch?time_continue=487&v=2MmGzdiKR9Y&.. 2020. 8. 16.