# 1.leetcode#718. Maximum Length of Repeated Subarray

## 1.1题目描述

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

Example :

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

## 1.3代码

public int findLength(int[] A, int[] B) {
int max=0;
int[][] samecount=new int[A.length][B.length];
//初始化第一行第一列
for(int i=0;i<A.length;i++){
if(B[0]==A[i]){
samecount[i][0]=1;
}
}
for(int i=0;i<B.length;i++){
if(B[i]==A[0]){
samecount[0][i]=1;
}
}
//初始化最后一行
for(int row=1;row<A.length;row++){
for(int col=1;col<B.length;col++){
if(A[row]==B[col]){
//状态转移
samecount[row][col]=samecount[row-1][col-1]+1;
//全局比较
max=max>samecount[row][col]?max:samecount[row][col];
}
}
}
return max;
}

## 1.5代码

public int findLength(int[] A, int[] B) {
//时间复杂度小
if(A.length<B.length){
return findLength(B, A);
}
int max=0;
int[] samecount=new int[A.length];
//初始化第一行
for(int i=0;i<A.length;i++){
if(B[0]==A[i]){
samecount[i]=1;
}
}
//初始化最后一行
for(int i=1;i<B.length;i++){
int[] tempsamcount=new int[A.length];
for(int j=0;j<A.length;j++){
if(A[j]==B[i]){
if(j==0){
tempsamcount[j]=1;
}
else{
//状态转移
tempsamcount[j]=samecount[j-1]+1;
}
//全局比较
max=max>tempsamcount[j]?max:tempsamcount[j];
}
}
//下一次迭代
samecount=tempsamcount;
}
return max;
}


# 2.leetcode#583. Delete Operation for Two Strings

## 2.1问题描述

Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.

Example :

Input: “sea”, “eat”
Output: 2
Explanation: You need one step to make “sea” to “ea” and another step to make “eat” to “ea”.

## 2.3代码

public static int minDistance(String word1, String word2) {
if(word1==null||word1.length()==0){
return word2.length();
}
else if(word2==null||word2.length()==0){
return word1.length();
}
int max=0;
//行为word1,列为word2;
int[][] samecount=new int[word1.length()][word2.length()];
//先把第一行处理掉
for(int i=0;i<word1.length();i++){
if(word1.charAt(i)==word2.charAt(0)){
while(i<word1.length()){
samecount[i++][0]=1;
}
max=1;
}
}
//再把第一列处理掉
for(int i=0;i<word2.length();i++){
if(word2.charAt(i)==word1.charAt(0)){
while(i<word2.length()){
samecount[0][i++]=1;
}
max=1;
}
}
//对后面的进行遍历
for(int row=1;row<word1.length();row++){
for(int col=1;col<word2.length();col++){
//状态转移
samecount[row][col]=Math.max(samecount[row-1][col], samecount[row][col-1]);
//如果当前相等的话，再看是否比目前最长的还长
if(word1.charAt(row)==word2.charAt(col)){
samecount[row][col]=Math.max(samecount[row][col], samecount[row-1][col-1]+1);
}
//与全局比较
max=max>samecount[row][col]?max:samecount[row][col];
}
}
return word1.length()+word2.length()-2*max;
}

## 2.5代码

public static int minDistance(String word1, String word2) {
if(word1==null||word1.length()==0){
return word2.length();
}
else if(word2==null||word2.length()==0){
return word1.length();
}
//假设word1长
if(word1.length()<word2.length()){
return minDistance(word2, word1);
}
int max=0;
int[] samecount=new int[word1.length()];
for(int i=0;i<samecount.length;i++){
//只要有1个1，下面全部为1；
if(word2.charAt(0)==word1.charAt(i)){
while(i<samecount.length){
samecount[i++]=1;
}
max=1;
}
}
for(int i=1;i<word2.length();i++){
int[] tempsamecount=new int[word1.length()];
for(int j=0;j<word1.length();j++){
//如果是首位的话
if(j==0){
//相等，则为1，否则为0
tempsamecount[j]=word1.charAt(j)==word2.charAt(i)?1:0;
//但是如果前面相等过了，则即使不相等也为1；
tempsamecount[j]=Math.max(tempsamecount[j], samecount[j]);
}
else{
//状态转移
//先看目前最长的子序列为多少
tempsamecount[j]=Math.max(tempsamecount[j-1], samecount[j]);
//如果相等当前相等的话，再看是否比目前最长的还长
if(word1.charAt(j)==word2.charAt(i)){
tempsamecount[j]=Math.max(tempsamecount[j], samecount[j-1]+1);
}
}
//与全局比较
max=max>tempsamecount[j]?max:tempsamecount[j];
}
//进行下一次递归
samecount=tempsamecount;
}
return word1.length()+word2.length()-2*max;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120