class Solution(object):
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left = 0
right = len(nums) - 1
while left + 1 < right:
if nums[left] == nums[right]: # must be a horizontal line on the left
value = nums[left]
while nums[left] == value:
if left == right:
return value # all the ...- read more
153. Find Minimum in Rotated Sorted Array
read moreclass Solution(object): def findMin(self, nums): """ :type nums: List[int] :rtype: int """ left = 0 right = len(nums) - 1 while left + 1 < right: mid = left + (right - left)/2 if nums[mid] > nums[left] and nums[mid] < nums[right]: return nums[left] elif nums[mid] > nums[left]: left = mid + 1 else ...
152. Maximum Product Subarray
read moreclass Solution(object): def maxProduct(self, nums): """ :type nums: List[int] :rtype: int """ minproduct = nums[0] maxproduct = nums[0] totproduct = nums[0] for i in range(1, len(nums)): copymin = minproduct minproduct = min(minproduct * nums[i], maxproduct * nums[i], nums[i]) maxproduct = max(copymin * nums[i], maxproduct * nums[i], nums ...
151. Reverse Words in a String
read moreclass Solution(object): def reverseWords(self, s): """ :type s: str :rtype: str """ # s = " ".join(s.split()) # replace interval with only one space. Create a new array! can't use it! s = s + ' ' # if not add ' ', the last word can not be extracted for i in range(len(s)): if s ...
150. Evaluate Reverse Polish Notation
read moreclass Solution(object): def evalRPN(self, tokens): """ :type tokens: List[str] :rtype: int """ queue = [] for item in tokens: if item == "*" or item == "/" or item == "+" or item == "-": operand2 = queue.pop() operand1 = queue.pop() result = self.calc(item, operand1, operand2) queue.append(result) else: queue.append(int(item)) return queue.pop() def ...
148. Sort List
read more# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def sortList(self, head): """ :type head: ListNode :rtype: ListNode """ if head == None or head.next == None: return head slow = head fast = head while fast.next and fast.next.next: slow ...
147. Insertion Sort List
read more# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def insertionSortList(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return None if not head.next: return head cur = head.next beforecur = head while cur: tmp = head beforetmp ...
145. Binary Tree Postorder Traversal
read more# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def postorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ if not root: return [] queue = [root] result = [] while queue: node = queue.pop() result.insert(0 ...
144. Binary Tree Preorder Traversal
read more# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def preorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ if not root: return [] queue = [root] result = [] while queue: node = queue.pop() result.append(node ...
143. Reorder List
read more# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def reorderList(self, head): """ :type head: ListNode :rtype: void Do not return anything, modify head in-place instead. """ if not head: return None slow = head fast = head while fast and fast ...