41. First Missing Positive | Hard
Runtime: 36 ms, faster than 99.96% of Python3 online submissions for First Missing Positive.
class Solution: def firstMissingPositive(self, nums): """ :type nums: List[int] :rtype: int """ numm = {} for num in nums: if num > 0: numm[num] = 1 counter = 1 for _ in range(len(numm)): if counter in nums and numm[counter]: counter += 1 return counter
4. Median of Two Sorted Arrays | Hard
# there must be a better way, below is O(n)
# also for it to work, I would need to check bounds
class Solution: def findMedianSortedArrays(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: float """ l1, r1 = 0, len(nums1) - 1 l2, r2 = 0, len(nums2) - 1 odd = (len(nums1) + len(nums2)) % 2 cur = 0 medIndex1 = ((len(nums1) + len(nums2))/2 + 0.
20. Valid Parentheses | Easy
Runtime: 48 ms, faster than 38.56% of Python3 online submissions for Valid Parentheses.
class Solution: def isValid(self, s): """ :type s: str :rtype: bool """ if len(s) == 0: return True stack = [] for elem in s: if elem == '{': stack += '}' elif elem == '[': stack += ']' elif elem == '(': stack += ')' elif len(stack) >= 1 and elem == stack[-1]: stack = stack[:-1] elif elem == '}': return False elif elem == ']': return False elif elem == ')': return False else: continue return True if len(stack) == 0 else False
Reverse Integer
Currently only works for postive numbers since // operation floors in negative case, changing the numbers.
class Solution: def reverse(self, x): """ :type x: int :rtype: int """ if x == 0: return 0 elif x < 10 and x > -10: return x stack = [] negative = True if x < 0 else False if negative: stack += '-' while x >= 10 or x <= -10: stack += str(x % 10) x = x // 10 if x !
I always wondered if it was actually legal or it is something people do customarily or is something other states who have it legal but not in California. It always itched me as I make my left turns in a one-way to one-way intersection on a red light.
I finally got around to search the legislature; here is the definitive answer as of January 2019: Yes, it is legal, as per California Vehicle Code.