LeetCode – Power of Two (Easy)
𝑂(log 𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
if n <= 0:
return False
while n>1:
if n%2 == 0:
n = n/2
else:
return False
return True
LeetCode – Valid Parentheses (Easy)
𝑂(𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛
class Solution:
def isValid(self, s: str) -> bool:
new_s = ' '
for text in s:
if new_s[-1] == '{' and text == '}':
new_s = new_s[:-1]
continue
elif new_s[-1] == '(' and text == ')':
new_s = new_s[:-1]
continue
elif new_s[-1] == '[' and text == ']':
new_s = new_s[:-1]
continue
elif new_s[-1] == '{' and (text == ')' or text == ']'):
return False
elif new_s[-1] == '(' and (text == ']' or text == '}'):
return False
elif new_s[-1] == '[' and (text == ')' or text == '}'):
return False
new_s += text
if new_s[1:] == '':
return True
else:
return False
LeetCode – Find Minimum in Rotated Sorted Array (Medium)
𝑂(𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛
class Solution:
def findMin(self, nums: List[int]) -> int:
prev = nums[0] - 1
for num in nums:
if prev > num:
return num
prev = num
return nums[0]
𝑂 log 𝑛 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛
class Solution:
def findMin(self, nums: List[int]) -> int:
cur = nums
while len(cur)>0:
leng = len(cur)
if leng == 1:
return cur[0]
mid = int(leng/2)
if len(cur[:mid]) > 1:
if cur[:mid][0] > cur[:mid][-1] and cur[:mid][-1] < cur[mid:][0]:
cur = cur[:mid]
elif cur[:mid][0] < cur[:mid][-1] and cur[:mid][0] < cur[mid:][-1]:
return cur[:mid][0]
else:
cur = cur[mid:]
elif len(cur[:mid]) == 0:
cur = cur[mid:]
elif len(cur[:mid]) == 1:
if cur[:mid][0] > cur[mid:][-1]:
cur = cur[mid:]
else:
return cur[:mid][0]
Thank You

Coding Test Review 3

  • 2.
    LeetCode – Powerof Two (Easy)
  • 3.
    𝑂(log 𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 classSolution: def isPowerOfTwo(self, n: int) -> bool: if n <= 0: return False while n>1: if n%2 == 0: n = n/2 else: return False return True
  • 4.
    LeetCode – ValidParentheses (Easy)
  • 5.
    𝑂(𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 class Solution: defisValid(self, s: str) -> bool: new_s = ' ' for text in s: if new_s[-1] == '{' and text == '}': new_s = new_s[:-1] continue elif new_s[-1] == '(' and text == ')': new_s = new_s[:-1] continue elif new_s[-1] == '[' and text == ']': new_s = new_s[:-1] continue elif new_s[-1] == '{' and (text == ')' or text == ']'): return False elif new_s[-1] == '(' and (text == ']' or text == '}'): return False elif new_s[-1] == '[' and (text == ')' or text == '}'): return False new_s += text if new_s[1:] == '': return True else: return False
  • 6.
    LeetCode – FindMinimum in Rotated Sorted Array (Medium)
  • 7.
    𝑂(𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 class Solution: deffindMin(self, nums: List[int]) -> int: prev = nums[0] - 1 for num in nums: if prev > num: return num prev = num return nums[0]
  • 8.
    𝑂 log 𝑛𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 class Solution: def findMin(self, nums: List[int]) -> int: cur = nums while len(cur)>0: leng = len(cur) if leng == 1: return cur[0] mid = int(leng/2) if len(cur[:mid]) > 1: if cur[:mid][0] > cur[:mid][-1] and cur[:mid][-1] < cur[mid:][0]: cur = cur[:mid] elif cur[:mid][0] < cur[:mid][-1] and cur[:mid][0] < cur[mid:][-1]: return cur[:mid][0] else: cur = cur[mid:] elif len(cur[:mid]) == 0: cur = cur[mid:] elif len(cur[:mid]) == 1: if cur[:mid][0] > cur[mid:][-1]: cur = cur[mid:] else: return cur[:mid][0]
  • 9.