博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 718. Maximum Length of Repeated Subarray
阅读量:4622 次
发布时间:2019-06-09

本文共 1245 字,大约阅读时间需要 4 分钟。

原题链接在这里:

题目:

Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.

Example 1:

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

Note:

  1. 1 <= len(A), len(B) <= 1000
  2. 0 <= A[i], B[i] < 100

题解:

The subarray must be continuous. 

dp[i][j] denotes the maximum length of repeated subarray between A up to index i and B up to index j.

if(A[i] == B[j]) dp[i][j] = dp[i-1][j-1]+1.

if(A[i] != B[j]) d[i][j] = 0.

Time Complexity: O(m*n).

Space: O(m*n).

AC Java:

1 class Solution { 2     public int findLength(int[] A, int[] B) { 3         if(A == null || A.length == 0 || B == null || B.length == 0){ 4             return 0; 5         } 6          7         int m = A.length; 8         int n = B.length; 9         int [][] dp = new int[m+1][n+1];10         int res = 0;11         for(int i = 1; i<=m; i++){12             for(int j = 1; j<=n; j++){13                 if(A[i-1] == B[j-1]){14                     dp[i][j] = dp[i-1][j-1]+1;15                     res = Math.max(res, dp[i][j]);16                 }17             }18         }19         20         return res;21     }22 }

类似, , .

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11451706.html

你可能感兴趣的文章
Java的内部类真的那么难以理解?
查看>>
一文搞懂Java环境,轻松实现Hello World!
查看>>
hash实现锚点平滑滚动定位
查看>>
也谈智能手机游戏开发中的分辨率自适应问题
查看>>
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
Python中Selenium的使用方法
查看>>
三月23日测试Fiddler
查看>>
20171013_数据库新环境后期操作
查看>>
poj 1654 && poj 1675
查看>>
运维派 企业面试题1 监控MySQL主从同步是否异常
查看>>
Docker 版本
查看>>
poj 1753 Flip Game
查看>>
在深信服实习是怎样的体验(研发测试岗)
查看>>
Linux免密码登陆
查看>>
SpringMVC中文件的上传(上传到服务器)和下载问题(二)--------下载
查看>>
Socket & TCP &HTTP
查看>>
osip及eXosip的编译方法
查看>>