class Solution(object):
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
if not board:
return
for i in range(1, len(board) - 1): # begin from top left 1, bottom right -1
for j in range(1, len(board[0 ...- read more
129. Sum Root to Leaf Numbers
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 sumNumbers(self, root): """ :type root: TreeNode :rtype: int """ if root == None: return 0 result = [] self.dfs(root, result, tmp=0) return sum(result) def ...
127. Word Ladder
read moreimport 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 ...
122. Best Time to Buy and Sell Stock II
read moreclass 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__ ...
121. Best Time to Buy and Sell Stock
read moreclass 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
120. Triangle
read moreclass 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 ...
119. Pascal's Triangle II
read moreclass 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 ...
118. Pascal's Triangle
read moreclass 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 ...
116. Populating Next Right Pointers in Each Node
read more# 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 ...
114. Flatten Binary Tree to Linked List
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 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 ...