最长重复子数组(java)

    技术2022-07-11  133

    问题描述:

    给两个整数数组 A 和 B ,返回两个数组中公共的、长度最长的子数组的长度。

    样例: 代码见下:

    package Leetcode; import java.util.Scanner; public class FindLength { public static int findLength(int[] A, int[] B) {//暴力破解 int len1=A.length; int len2=B.length; int max=0; for (int i = 0; i <len1 ; i++) { int temp=0;//记录临时的最大值 for (int j = 0; j <len2 ; j++) { if (A[i]==B[j])//当两个值开始相同时 { int temp1=0; temp1++; int st1=i+1; int st2=j+1; while (st1<len1&&st2<len2&&A[st1]==B[st2]) { st1++; st2++; temp1++; } if (temp1>temp) temp=temp1;//更新最大值 } if (temp>max) max=temp; } } return max; } public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int lena=scanner.nextInt(); int lenb=scanner.nextInt(); int []A=new int[lena]; int []B=new int[lenb]; for (int i = 0; i <lena ; i++) { A[i]=scanner.nextInt(); } for (int i = 0; i <lenb ; i++) { B[i]=scanner.nextInt(); } int res=findLength(A,B); System.out.println(res); } }

    运行结果如下:

    Processed: 0.013, SQL: 9