classSolution: # @return a boolean defisMatch(self, s, p): dp=[[Falsefor i in range(len(p)+1)] for j in range(len(s)+1)] dp[0][0]=True for i in range(1,len(p)+1): if p[i-1]=='*': if i>=2: dp[0][i]=dp[0][i-2] for i in range(1,len(s)+1): for j in range(1,len(p)+1): if p[j-1]=='.': dp[i][j]=dp[i-1][j-1] elif p[j-1]=='*': dp[i][j]=dp[i][j-1] or dp[i][j-2] or (dp[i-1][j] and (s[i-1]==p[j-2] or p[j-2]=='.')) else: dp[i][j]=dp[i-1][j-1] and s[i-1]==p[j-1] return dp[len(s)][len(p)]
classSolution: defminPathSum(self, grid): """ :type grid: List[List[int]] :rtype: int """ ifnot grid ornot grid[0]: return0 m, n = len(grid), len(grid[0]) for i in range(m): for j in range(n): if i == 0and j == 0: before = 0 elif i == 0: before = grid[i][j-1] elif j == 0: before = grid[i-1][j] else: before = min(grid[i-1][j], grid[i][j-1]) grid[i][j] = before + grid[i][j] return grid[m-1][n-1]