日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode Hot100 ---- 最长相关专题(动态规划)

發布時間:2023/12/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode Hot100 ---- 最长相关专题(动态规划) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

動態規劃解題步驟

核心思想是遞推,難點在于想清楚 狀態 dp[i] 代表什么,然后構造狀態轉移矩陣,利用初始條件遞推出最終結果

  • 將原問題拆分成子問題
  • 確認狀態
  • 確認邊界狀態(初始條件)
  • 狀態轉移方程
  • 最長問題

    647. 回文子串

    給定一個字符串,你的任務是計算這個字符串中有多少個回文子串。

    class Solution(object):def countSubstrings(self, s):""":type s: str:rtype: int"""dp = [[False] * len(s) for _ in range(len(s))]result = 0for i in range(len(s) -1, -1, -1):for j in range(i, len(s)):if s[i] == s[j]:if j - i <= 1:result = result + 1dp[i][j] = Trueelif dp[i+1][j-1]:result = result + 1dp[i][j] = Truereturn result

    5. 最長回文子串

    class Solution:def longestPalindrome(self, s: str) -> str:""":type s: str:rtype: int"""dp = [[False] * len(s) for _ in range(len(s))]maxLength = 0left, right = 0, 0for i in range(len(s) -1, -1, -1):for j in range(i, len(s)):if s[i] == s[j]:if j - i <= 1:dp[i][j] = Trueelif dp[i+1][j-1]:dp[i][j] = Trueif dp[i][j] and j - i + 1 > maxLength:left = iright = jmaxLength = j - i + 1return s[left:right+1]

    516. 最長回文子序列?

    回文子串是要連續的,回文子序列可不是連續的!

    class Solution(object):def longestPalindromeSubseq(self, s):""":type s: str:rtype: int"""dp = [[0] * len(s) for _ in range(len(s))]for i in range(len(s)):dp[i][i] = 1for i in range(len(s)-1, -1, -1):for j in range(i+1, len(s)):if s[i] == s[j]:dp[i][j] = dp[i+1][j-1] + 2else:dp[i][j] = max(dp[i+1][j], dp[i][j-1])return dp[0][-1]

    300. 最長遞增子序列

    ?

    class Solution(object):def lengthOfLIS(self, nums):""":type nums: List[int]:rtype: int"""if not nums:return 0dp = [1] * len(nums)maxLength = 0for i in range(len(nums)):for j in range(i):if nums[i] > nums[j]:dp[i] = max(dp[i], dp[j] + 1)if dp[i] > maxLength:maxLength = dp[i]return maxLength

    674. 最長連續遞增序列

    class Solution(object):def findLengthOfLCIS(self, nums):""":type nums: List[int]:rtype: int"""dp = [1] * len(nums)for i in range(len(nums)-1):if nums[i+1] > nums[i]:dp[i+1] = dp[i] + 1return max(dp)

    718. 最長重復子數組

    給兩個整數數組?A?和?B?,返回兩個數組中公共的、長度最長的子數組的長度。

    class Solution(object):def findLength(self, nums1, nums2):""":type nums1: List[int]:type nums2: List[int]:rtype: int"""n, m = len(nums1), len(nums2)dp = [[0]* (m + 1) for _ in range(n + 1)]result = 0for i in range(1, n+1):for j in range(1, m+1):if nums1[i-1] == nums2[j -1]:dp[i][j] = dp[i-1][j-1] + 1if dp[i][j] > result:result = dp[i][j]return result

    ?1143. 最長公共子序列

    本題和718. 最長重復子數組區別在于這里不要求是連續的了,但要有相對順序,即:"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。

    class Solution(object):def longestCommonSubsequence(self, text1, text2):""":type text1: str:type text2: str:rtype: int"""n, m = len(text1), len(text2)dp = [[0]* (m + 1) for _ in range(n + 1)]result = 0for i in range(1, n+1):for j in range(1, m+1):if text1[i-1] == text2[j -1]:dp[i][j] = dp[i-1][j-1] + 1else:dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])if dp[i][j] > result:result = dp[i][j]return result

    53. 最大子序和

    class Solution(object):def maxSubArray(self, nums):""":type nums: List[int]:rtype: int"""if len(nums) == 0:return 0dp = [0] * len(nums)dp[0] = nums[0]result = dp[0]for i in range(1, len(nums)):dp[i] = max(dp[i-1] + nums[i], nums[i])if dp[i] > result:result = dp[i]return result

    128. 最長連續序列

    給定一個未排序的整數數組 nums ,找出數字連續(不是升序是連續)的最長序列(不要求序列元素在原數組中連續)的長度。

    class Solution:def longestConsecutive(self, nums: List[int]) -> int:length=len(nums)if length<2:return lengthnums.sort()dp=[0]*lengthdp[0]=1 for i in range(1, length):if nums[i]==nums[i-1]+1:dp[i]=dp[i-1]+1elif nums[i]==nums[i-1]:dp[i]=dp[i-1]else:dp[i]=1return max(dp)

    最長連續公共字串

    最長連續為1的字串

    最長有效括號

    最長無重復字符的連續字串

    最長等差數列

    最長上升連續序列

    最長上升子序列

    最長和諧子序列

    總結

    以上是生活随笔為你收集整理的LeetCode Hot100 ---- 最长相关专题(动态规划)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。