1. 127. Word Ladder

    import Queue
    class Solution(object):
        def ladderLength(self, beginWord, endWord, wordList):
            """
            :type beginWord: str
            :type endWord: str
            :type wordList: List[str]
            :rtype: int
            """
    
            wordSet = set([])
            for item in wordList:
                wordSet.add(item)   # transfer list into set
            queue = Queue.Queue()
            queue.put(beginWord)
            goal = {beginWord:1}
            char = [chr(i) for i ...
    read more
  2. 122. Best Time to Buy and Sell Stock II

    class Solution(object):
        def maxProfit(self, prices):
            """
            :type prices: List[int]
            :rtype: int
            """
            if len(prices) < 2:
                return 0
            current = prices[0]
            prices_len = len(prices)
            profit = 0
            for i in range(1, prices_len):
                if prices[i] > current:
                    profit += (prices[i] - current)
                    print profit
                current = prices[i]
            return profit
    
    if __name__ ...
    read more
  3. 121. Best Time to Buy and Sell Stock

    class Solution(object):
        def maxProfit(self, prices):
            """
            :type prices: List[int]
            :rtype: int
            """
            if prices == []:
                return 0
            result = 0
            low = prices[0]
            for i in range(1, len(prices)):
                if prices[i] - low > result:
                    result = prices[i] - low
                if prices[i] < low:
                    low = prices[i]
            return result
    
    read more
  4. 120. Triangle

    class Solution(object):
        def minimumTotal(self, triangle):
            """
            :type triangle: List[List[int]]
            :rtype: int
            """
            m = len(triangle)
            if m == 0:
                return 0
            n = len(triangle[-1])
            result = [0] * n
            result[0] = triangle[0][0]
            for i in range(1, m):
                for j in range(len(triangle[i])-1,-1,-1 ...
    read more
  5. 119. Pascal's Triangle II

    class Solution(object):
        def getRow(self, rowIndex):
            """
            :type rowIndex: int
            :rtype: List[int]
            """
            if rowIndex == 0:
                return [1]
            if rowIndex == 1:
                return [1, 1]
    
            result = [[1], [1, 1]]
            for i in range(2, rowIndex + 1):
                newRow = [1]
                for j in range(0, len(result[i - 1]) - 1):
                    newRow.append(result ...
    read more
  6. 118. Pascal's Triangle

    class Solution(object):
        def generate(self, numRows):
            """
            :type numRows: int
            :rtype: List[List[int]]
            """
            if numRows == 0:
                return []
            if numRows == 1:
                return [[1]]
            if numRows == 2:
                return [[1], [1, 1]]
    
            result = [[1], [1, 1]]
            for i in range(3, numRows + 1):
                newRow = [1]
                for j in range(0, len(result ...
    read more
  7. 116. Populating Next Right Pointers in Each Node

    # Definition for binary tree with next pointer.
    # class TreeLinkNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    #         self.next = None
    
    class Solution:
        # @param root, a tree link node
        # @return nothing
        def connect(self, root):
            if root and root.left:
                root.left.next = root.right
                if ...
    read more
  8. 114. Flatten Binary Tree to Linked List

    # 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 flatten(self, root):
            """
            :type root: TreeNode
            :rtype: void Do not return anything, modify root in-place instead.
            """
            if root == None:
                return
            result = []
            queue = [root]
            while ...
    read more
  9. 113. Path Sum II

    # 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 pathSum(self, root, sum):
            """
            :type root: TreeNode
            :type sum: int
            :rtype: List[List[int]]
            """
            if root == None:
                return []
            result = []
            self.dfs(root, result, tmp ...
    read more
  10. 112. Path Sum

    # 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 hasPathSum(self, root, sum):
            """
            :type root: TreeNode
            :type sum: int
            :rtype: bool
            """
            if root == None:
                return False
            result = []
            self.dfs(root, result, tmp=[])
            return ...
    read more

« Page 6 / 21 »

blogroll

social