当前位置:网站首页>Leetcode 718. Longest repeating subarray (violence enumeration, to be solved)

Leetcode 718. Longest repeating subarray (violence enumeration, to be solved)

2022-06-26 23:22:00 I'm not xiaohaiwa~~~~

Give two arrays of integers nums1 and nums2 , return In two arrays Public 、 The length of the longest subarray .

Example 1:

 Input :nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
 Output :3
 explain : The longest common subarray is  [3,2,1] .

Example 2:

 Input :nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
 Output :5
 

Tips :

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

Code:

int findLength(vector<int>& nums1, vector<int>& nums2) {
    

    pair<map<vector<int>, int>::iterator, bool> ret;

    map<vector<int>,int>mymap;
    for(int l=0;l<nums1.size();l++)
    {
    
        for(int i=1+l;i<=nums1.size();i++)
        {
    
            vector<int>vec;
            for(int j=l;j<i;j++)
            {
    
                // cout<<nums1[j]<< " ";
                vec.push_back(nums1[j]);
            }
            mymap.insert(pair<vector<int>,int>(vec,0));
            // cout<<endl;
        }
    }


    int maxlen=0;
    for(int l=0;l<nums2.size();l++)
    {
    
        // for(int i=1+l;i<=nums2.size();i++)
        for(int i=nums2.size();i>=1+l;i--)
        {
    
            vector<int>vec;
            for(int j=l;j<i;j++)
            {
    
                cout<<nums2[j]<< " ";
                vec.push_back(nums2[j]);
            }

            cout<<endl;
            if(mymap.find(vec)!=mymap.end())
            {
    
                maxlen=max(maxlen,(int)vec.size());
            }
        }
    }
    return maxlen;
}

原网站

版权声明
本文为[I'm not xiaohaiwa~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206262308245412.html