classSolution: defuniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: #新建矩阵版 height, width = len(obstacleGrid),len(obstacleGrid[0]) store = [[0]*width for i inrange(height)]
#从上到下,从左到右 for m inrange(height):#每一行 for n inrange(width):#每一列 ifnot obstacleGrid[m][n]: #如果这一格没有障碍物 if m == n == 0: #或if not(m or n) store[m][n] = 1 else: a = store[m-1][n] if m!=0else0#上方格子 b = store[m][n-1] if n!=0else0#左方格子 store[m][n] = a+b return store[-1][-1]
classSolution: deflongestPalindrome(self, s: str) -> str: n = len(s) dp = [[False] * n for _ inrange(n)] ans = "" # 枚举子串的长度 l+1 for l inrange(n): sub_len = l+1 # 枚举子串的起始位置 i,这样可以通过 j=i+l 得到子串的结束位置 for i inrange(n): j = i + l if j >= len(s): break if sub_len == 1: dp[i][j] = True elif sub_len == 2: dp[i][j] = (s[i] == s[j]) else: dp[i][j] = (dp[i + 1][j - 1] and s[i] == s[j]) if dp[i][j] and sub_len > len(ans): ans = s[i:j+1] # "abcd"[1:2] = b return ans
classSolution: defisInterleave(self, s1: str, s2: str, s3: str) -> bool: len1=len(s1) len2=len(s2) len3=len(s3) if(len1+len2!=len3): returnFalse dp=[[False]*(len2+1) for i inrange(len1+1)] dp[0][0]=True for i inrange(1,len1+1): dp[i][0]=(dp[i-1][0] and s1[i-1]==s3[i-1]) for i inrange(1,len2+1): dp[0][i]=(dp[0][i-1] and s2[i-1]==s3[i-1]) for i inrange(1,len1+1): for j inrange(1,len2+1): dp[i][j]=(dp[i][j-1] and s2[j-1]==s3[i+j-1]) or (dp[i-1][j] and s1[i-1]==s3[i+j-1]) return dp[-1][-1]
classSolution: deftranslateNum(self, num: int) -> int: s = str(num) a = b = 1 for i inrange(2, len(s) + 1): tmp = s[i - 2:i] c = a + b if"10" <= tmp <= "25"else a b = a a = c return a
Checking if Disqus is accessible...