전체 글15 [LeetCode with Python] 264. Ugly Number II Difficulty : Medium 1st Approach : search all postive numbers and check if they are ugly -> Time Limit Exceeded! class Solution: def nthUglyNumber(self, n: int) -> int: uglies = [0] * (n + 1) i = 0 num = 1 while i < n + 1: origin_num = num while not num % 5: num //= 5 while not num % 3: num //= 3 while not num % 2: num //= 2 if num == 1: uglies[i] = origin_num i += 1 # print(uglies) num = origin.. 2020. 7. 5. [LeetCode with Python] 307. Range Sum Query Difficulty : Medium Approach 1 : use array and update, sum with index class NumArray: def __init__(self, nums: List[int]): self.nums = nums def update(self, i: int, val: int) -> None: self.nums[i] = val def sumRange(self, i: int, j: int) -> int: return sum(self.nums[i:j+1]) # Your NumArray object will be instantiated and called as such: # obj = NumArray(nums) # obj.update(i,val) # param_2 = obj... 2020. 7. 4. [LeetCode with Python] 263. Ugly Number Difficulty : easy Approach : 1) number range check 2) divide with [2, 3, 5] as much as possible class Solution: def isUgly(self, num: int) -> bool: if num == 1: return True elif num 2020. 7. 4. [LeetCode with Python] 58. Length of Last Word Difficulty : Easy Approach 1 : use rstrip to treat with case with rightside spaces. class Solution: def lengthOfLastWord(self, s: str) -> int: count = 0 s = s.rstrip() for i in range(len(s) - 1, -1, -1): if s[i] == " ": break count += 1 return count Approach 2 : no rstrip, put another if when the letter we encountered is space to check it's rightside space or not. too slow :( class Solution: def.. 2020. 7. 2. [LeetCode with Python] 20. Valid Parenthesis First Approach: use 'stack' and 'dictionary' to check correct parenthesis pair from collections import deque par_map = {')': '(', '}': '{', ']': '['} class Solution: def isValid(self, s: str) -> bool: stack = deque() for letter in s: if letter not in par_map: stack.append(letter) else: if not stack: return False elif stack[-1] == par_map[letter]: stack.pop() else: return False if stack: return F.. 2020. 7. 1. [LeetCode with Python] 14. Longest Common Prefix Difficulty: Easy First Approach: For efficiency, sort strings by its length and suppose the first string of the sorted array as a 'key' with which we compare others. And find longest partial string( of the 'key' word) that is the longest common prefix too. from typing import List class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: strs.sort(key=lambda x: len(x)) answer = "" if.. 2020. 7. 1. [LeetCode with Python] 13. Roman to Integer Difficulty: Easy First Approach: to deal with exception cases(e.g. "IV"), use before_map to know there is next letter to check. -> inefficent in memory usage. Second Approach(reference from https://medium.com/@angelahiking/13-roman-to-integer-python-easy-cabcb1d6b8a3): The idea is, in exception cases there always comes "smaller" number before "larger" number. So we don't need before_map, and jus.. 2020. 6. 29. 이전 1 2 다음