题目1:300.最长递增子序列
讲解
leetcode
class Solution {
func lengthOfLIS(_ nums: [Int]) -> Int {
var dp = Array(repeating: 1, count: nums.count)
var result = 1
for i in 1..<nums.count {
for j in 0..<i {
if nums[i] > nums[j] {
dp[i] = max(dp[j] + 1, dp[i])
}
}
result = max(result, dp[i])
}
return result
}
}
题目2:674. 最长连续递增序列
讲解
leetcode
class Solution {
func findLengthOfLCIS(_ nums: [Int]) -> Int {
var dp = Array(repeating: 1, count: nums.count)
var result = 1
for i in 1..<nums.count {
if nums[i] > nums[i - 1] {
dp[i] = dp[i - 1] + 1
}
result = max(result, dp[i])
}
return result
}
}
题目3:718. 最长重复子数组
讲解
leetcode
class Solution {
func findLength(_ nums1: [Int], _ nums2: [Int]) -> Int {
var dp = Array(repeating: Array(repeating: 0, count: nums2.count + 1), count: nums1.count + 1)
var res = 0
for i in 1...nums1.count {
for j in 1...nums2.count {
if nums1[i - 1] == nums2[j - 1] {
dp[i][j] = dp[i - 1][j - 1] + 1
}
res = max(res, dp[i][j])
}
}
return res
}
}