1 <= nums.length <= 10-10 <= nums[i] <= 10 All the numbers of nums are *unique.
题目大意:
求所有子集
解题思路:
组合知识点
解题步骤:
N/A
注意事项:
题目要求结果含空集
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
defsubsets(self, nums: List[int]) -> List[List[int]]: ifnot nums: return [] res = [[]] self.dfs(nums, 0, [], res) return res
defdfs(self, nums, st, path, res): if st == len(nums): return for i in range(st, len(nums)): path.append(nums[i]) res.append(list(path)) self.dfs(nums, i + 1, path, res) path.pop()
A valid number can be split up into these components (in order):
1. A decimal number or an integer. 2. (Optional) An 'e' or 'E', followed by an integer.
A decimal number can be split up into these components (in order):
1. (Optional) A sign character (either '+' or '-'). 2. One of the following formats: 1. One or more digits, followed by a dot '.'. 2. One or more digits, followed by a dot '.', followed by one or more digits. 3. A dot '.', followed by one or more digits.
An integer can be split up into these components (in order):
1. (Optional) A sign character (either '+' or '-'). 2. One or more digits.
For example, all the following are valid numbers: ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"], while the following are not valid numbers: ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"].
Given a string s, return trueifsis a valid number.
Example 1:
Input: s = “0” Output: true
Example 2:
Input: s = “e” Output: false
Example 3:
Input: s = “.” Output: false
Constraints:
1 <= s.length <= 20s consists of only English letters (both uppercase and lowercase), digits (0-9), plus '+', minus '-', or dot '.'.
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and space is marked as 1 and 0 respectively in the grid.
Example 1:
Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]] Output: 2 Explanation: There is one obstacle in the middle of the 3x3 grid above. There are two ways to reach the bottom-right corner: 1. Right -> Right -> Down -> Down 2. Down -> Down -> Right -> Right
Example 2:
Input: obstacleGrid = [[0,1],[0,0]] Output: 1
Constraints:
m == obstacleGrid.lengthn == obstacleGrid[i].length 1 <= m, n <= 100obstacleGrid[i][j] is 0 or 1.
题目大意:
求矩阵路径总数。有障碍
解题思路:
求个数用DP,递归式:
1 2
dp[i][j] = dp[i-1][j] + dp[i][j-1] if obstacle[i][j-1] == 0 = 0 if obstacle[i][j-1] == 1
解题步骤:
N/A
注意事项:
obstacleGrid[i][j-1], j-1因为dp从1开始, 但i不是,因为dp不含i。
Python代码:
1 2 3 4 5 6 7 8 9 10
defuniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int: dp = [0] * (len(obstacleGrid[0]) + 1) dp[1] = 1 for i in range(len(obstacleGrid)): for j in range(1, len(dp)): if obstacleGrid[i][j - 1] == 0: dp[j] += dp[j - 1] else: dp[j] = 0 return dp[-1]
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
The test cases are generated so that the answer will be less than or equal to 2 * 10<sup>9</sup>.
Example 1:
Input: m = 3, n = 7 Output: 28
Example 2:
Input: m = 3, n = 2 Output: 3 Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner: 1. Right -> Down -> Down 2. Down -> Down -> Right 3. Down -> Right -> Down
Constraints:
* 1 <= m, n <= 100
题目大意:
求矩阵路径总数
解题思路:
求个数用DP,递归式:
1
dp[i][j] = dp[i-1][j] + dp[i][j-1]
解题步骤:
N/A
注意事项:
初始值dp[1] = 1而不是dp[0] = 1因为第二行的第一格不能加左边的虚拟格=1
range(m)不是range(len(m))
优化空间用一维
Python代码:
1 2 3 4 5 6 7
defuniquePaths(self, m: int, n: int) -> int: dp = [0] * (n + 1) dp[1] = 1# remember not dp[0] = 1 for i in range(m): # remember no len(m) for j in range(1, len(dp)): dp[j] += dp[j - 1] return dp[-1]