1. 154. Find Minimum in Rotated Sorted Array II

    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
  2. 153. Find Minimum in Rotated Sorted Array

    class 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 ...
    read more
  3. 152. Maximum Product Subarray

    class 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 ...
    read more
  4. 151. Reverse Words in a String

    class 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 ...
    read more
  5. 150. Evaluate Reverse Polish Notation

    class 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 ...
    read more
  6. 148. Sort List

    # 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 ...
    read more
  7. 147. Insertion Sort List

    # 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 ...
    read more
  8. 145. Binary Tree Postorder Traversal

    # 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 ...
    read more
  9. 144. Binary Tree Preorder Traversal

    # 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 ...
    read more
  10. 143. Reorder List

    # 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 ...
    read more

« Page 4 / 21 »

blogroll

social