diff --git a/problems/0718.最长重复子数组.md b/problems/0718.最长重复子数组.md index 18cc0240..272cf2b2 100644 --- a/problems/0718.最长重复子数组.md +++ b/problems/0718.最长重复子数组.md @@ -536,6 +536,29 @@ function findLength(nums1: number[], nums2: number[]): number { }; ``` +Rust: + +> 滚动数组 + +```rust +impl Solution { + pub fn find_length(nums1: Vec, nums2: Vec) -> i32 { + let (mut res, mut dp) = (0, vec![0; nums2.len()]); + + for n1 in nums1 { + for j in (0..nums2.len()).rev() { + if n1 == nums2[j] { + dp[j] = if j == 0 { 1 } else { dp[j - 1] + 1 }; + res = res.max(dp[j]); + } else { + dp[j] = 0; + } + } + } + res + } +} +```