leetcode_medium动态规划_python
leetcode_medium动态规划_python
Leobing001
这个作者很懒,什么都没留下…
展开
-
5. Longest Palindromic Substring
class Solution: def longestPalindrome(self, s): """ :type s: str :rtype: str """ # dp ans = '' max_len = 0 n = len(s) DP = ...原创 2018-08-27 16:24:13 · 116 阅读 · 0 评论 -
62. Unique Paths
class Solution: def uniquePaths(self, m, n): """ :type m: int :type n: int :rtype: int """ # 让初始第一行和第一列为1 dp = [[1] * n for _ in range(m)] ...原创 2018-08-27 16:35:33 · 90 阅读 · 0 评论 -
63. Unique Paths II
class Solution: def uniquePathsWithObstacles(self, obstacleGrid): """ :type obstacleGrid: List[List[int]] :rtype: int """ # base cases:第一行第一列为1,但遇到obstacl...原创 2018-08-27 17:06:33 · 105 阅读 · 0 评论 -
64. Minimum Path Sum
class Solution: def minPathSum(self, grid): """ :type grid: List[List[int]] :rtype: int """ R = len(grid) C = len(grid[0]) dp = [[0] * C fo...原创 2018-08-28 11:58:23 · 119 阅读 · 0 评论 -
91. Decode Ways
class Solution: def numDecodings(self, s): """ :type s: str :rtype: int """ # 动态规划dp[i]表示s第i个数时decode方式 if s == "": return 0 d...原创 2018-08-28 13:06:33 · 116 阅读 · 0 评论 -
120. Triangle
class Solution: def minimumTotal(self, triangle): """ :type triangle: List[List[int]] :rtype: int """ # dp if not triangle: return 0 ...原创 2018-08-28 14:14:55 · 171 阅读 · 0 评论 -
139. Word Break
class Solution: def wordBreak(self, s, wordDict): """ :type s: str :type wordDict: List[str] :rtype: bool """ n = len(s) dp = [False] * (n ...原创 2018-08-29 14:35:45 · 112 阅读 · 0 评论 -
152. Maximum Product Subarray
class Solution: def maxProduct(self, nums): """ :type nums: List[int] :rtype: int """ n = len(nums) maxi = nums[0] dp_min = [0] * n ...原创 2018-08-29 20:11:52 · 117 阅读 · 0 评论 -
213. House Robber II
class Solution: def rob(self, nums): """ :type nums: List[int] :rtype: int """ # 将第一家和最后一家分别去掉,用动态规划求出两个最大值,然后较大的那个就是 n = len(nums) if n ==...原创 2018-08-29 21:20:27 · 241 阅读 · 0 评论 -
221. Maximal Square
class Solution: def maximalSquare(self, A): """ :type A: List[List[str]] :rtype: int """ for i in range(len(A)): for j in range(len(A[i])): ...原创 2018-08-31 23:46:27 · 124 阅读 · 0 评论 -
264. Ugly Number II
class Solution: def nthUglyNumber(self, n): """ :type n: int :rtype: int """ ugly = [1] i2, i3, i5 = 0, 0, 0 for _ in range(2, n + 1): ...原创 2018-09-01 18:08:53 · 124 阅读 · 0 评论 -
279. Perfect Squares
class Solution: def numSquares(self, n): """ :type n: int :rtype: int """ # # BFS # # 每一层都减去所有可以减去的平方数,下一层判断所有(x-平方数) # if n < 2: # ...原创 2018-09-01 20:39:11 · 220 阅读 · 0 评论 -
300. Longest Increasing Subsequence
class Solution: def lengthOfLIS(self, nums): """ :type nums: List[int] :rtype: int """ # dp[i] if not nums: return 0 n...原创 2018-09-02 13:09:02 · 110 阅读 · 0 评论 -
304. Range Sum Query 2D - Immutable
class NumMatrix: def __init__(self, matrix): """ :type matrix: List[List[int]] """ if not matrix: return None r = len(matrix) c = len(...原创 2018-09-02 13:50:58 · 275 阅读 · 0 评论 -
309. Best Time to Buy and Sell Stock with Cooldown
class Solution: def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ if not prices or len(prices) == 1: return 0 ...原创 2018-09-02 14:51:13 · 93 阅读 · 0 评论 -
322. Coin Change
class Solution: def coinChange(self, coins, amount): """ :type coins: List[int] :type amount: int :rtype: int """ MAX = float('inf') # 初始条件...原创 2018-09-06 10:55:24 · 115 阅读 · 0 评论 -
338. Counting Bits
class Solution: def countBits(self, num): """ :type num: int :rtype: List[int] """ # 右移一位(之前计算的记录下来)+ 末尾是否是1 num += 1 # 初始条件dp[0] = 0 ...原创 2018-09-06 13:57:28 · 102 阅读 · 0 评论 -
343. Integer Break
class Solution: def integerBreak(self, n): """ :type n: int :rtype: int """ dp = [0 for i in range(n+1)] dp[1] = 1 for i in range(2, n+1): ...原创 2018-09-06 18:05:20 · 95 阅读 · 0 评论 -
357. Count Numbers with Unique Digits
class Solution: def countNumbersWithUniqueDigits(self, n): """ :type n: int :rtype: int """ # 解法来源https://leetcode.com/problems/count-numbers-with-unique-d...原创 2018-09-21 10:08:57 · 119 阅读 · 0 评论