返回介绍

solution / 0700-0799 / 0718.Maximum Length of Repeated Subarray / README_EN

发布于 2024-06-17 01:03:35 字数 4312 浏览 0 评论 0 收藏 0

718. Maximum Length of Repeated Subarray

中文文档

Description

Given two integer arrays nums1 and nums2, return _the maximum length of a subarray that appears in both arrays_.

 

Example 1:

Input: nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
Output: 3
Explanation: The repeated subarray with maximum length is [3,2,1].

Example 2:

Input: nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
Output: 5
Explanation: The repeated subarray with maximum length is [0,0,0,0,0].

 

Constraints:

  • 1 <= nums1.length, nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 100

Solutions

Solution 1

class Solution:
  def findLength(self, nums1: List[int], nums2: List[int]) -> int:
    m, n = len(nums1), len(nums2)
    f = [[0] * (n + 1) for _ in range(m + 1)]
    ans = 0
    for i in range(1, m + 1):
      for j in range(1, n + 1):
        if nums1[i - 1] == nums2[j - 1]:
          f[i][j] = f[i - 1][j - 1] + 1
          ans = max(ans, f[i][j])
    return ans
class Solution {
  public int findLength(int[] nums1, int[] nums2) {
    int m = nums1.length;
    int n = nums2.length;
    int[][] f = new int[m + 1][n + 1];
    int ans = 0;
    for (int i = 1; i <= m; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (nums1[i - 1] == nums2[j - 1]) {
          f[i][j] = f[i - 1][j - 1] + 1;
          ans = Math.max(ans, f[i][j]);
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int findLength(vector<int>& nums1, vector<int>& nums2) {
    int m = nums1.size(), n = nums2.size();
    vector<vector<int>> f(m + 1, vector<int>(n + 1));
    int ans = 0;
    for (int i = 1; i <= m; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (nums1[i - 1] == nums2[j - 1]) {
          f[i][j] = f[i - 1][j - 1] + 1;
          ans = max(ans, f[i][j]);
        }
      }
    }
    return ans;
  }
};
func findLength(nums1 []int, nums2 []int) (ans int) {
  m, n := len(nums1), len(nums2)
  f := make([][]int, m+1)
  for i := range f {
    f[i] = make([]int, n+1)
  }
  for i := 1; i <= m; i++ {
    for j := 1; j <= n; j++ {
      if nums1[i-1] == nums2[j-1] {
        f[i][j] = f[i-1][j-1] + 1
        if ans < f[i][j] {
          ans = f[i][j]
        }
      }
    }
  }
  return ans
}
function findLength(nums1: number[], nums2: number[]): number {
  const m = nums1.length;
  const n = nums2.length;
  const f = Array.from({ length: m + 1 }, _ => new Array(n + 1).fill(0));
  let ans = 0;
  for (let i = 1; i <= m; ++i) {
    for (let j = 1; j <= n; ++j) {
      if (nums1[i - 1] == nums2[j - 1]) {
        f[i][j] = f[i - 1][j - 1] + 1;
        ans = Math.max(ans, f[i][j]);
      }
    }
  }
  return ans;
}
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var findLength = function (nums1, nums2) {
  const m = nums1.length;
  const n = nums2.length;
  const f = Array.from({ length: m + 1 }, _ => new Array(n + 1).fill(0));
  let ans = 0;
  for (let i = 1; i <= m; ++i) {
    for (let j = 1; j <= n; ++j) {
      if (nums1[i - 1] == nums2[j - 1]) {
        f[i][j] = f[i - 1][j - 1] + 1;
        ans = Math.max(ans, f[i][j]);
      }
    }
  }
  return ans;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文