[Swift]LeetCode718. 最长重复子数组 | Maximum Length of Repeated Subarray

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: 3
Explanation:
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


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

示例 1:

输入:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
输出: 3
解释:
长度最长的公共子数组是 [3, 2, 1]。

说明:

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


308ms

 1 class Solution {
 2     func findLength(_ A: [Int], _ B: [Int]) -> Int {
 3         var ret = 0
 4         let lenA = A.count, lenB = B.count
 5         for k in 1..<(lenA + lenB) {
 6             var i = max(0, lenA - k)
 7             var j = max(0, k - lenA)
 8             var len = 0
 9             while i < lenA && j < lenB {
10                 len = (A[i] == B[j]) ? (len + 1) : 0
11                 ret = max(ret, len)
12                 i += 1
13                 j += 1
14             }
15         }
16         return ret
17     }
18 }


1304ms

 1 class Solution {
 2     func findLength(_ A: [Int], _ B: [Int]) -> Int {
 3         let m = A.count, n = B.count
 4         var res = 0
 5
 6         var dp = [[Int]](repeating:[Int](repeating: 0, count: n+1), count: m + 1)
 7
 8         for i in 1...m {
 9             for j in 1...n {
10                 if A[i-1] == B[j-1] {
11                     dp[i][j] = 1 + dp[i-1][j-1]
12                     res = max(res, dp[i][j])
13                 }
14             }
15         }
16         return res
17     }
18 }


Runtime: 2028 ms

Memory Usage: 25.4 MB

 1 class Solution {
 2     func findLength(_ A: [Int], _ B: [Int]) -> Int {
 3         var res:Int = 0
 4         var dp:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:B.count + 1),count:A.count + 1)
 5         for i in 1..<dp.count
 6         {
 7             for j in 1..<dp[i].count
 8             {
 9                 dp[i][j] = (A[i - 1] == B[j - 1]) ? dp[i - 1][j - 1] + 1 : 0
10                 res = max(res, dp[i][j])
11             }
12         }
13         return res
14     }
15 }

原文地址:https://www.cnblogs.com/strengthen/p/10509887.html

时间: 2024-08-02 10:47:46

[Swift]LeetCode718. 最长重复子数组 | Maximum Length of Repeated Subarray的相关文章

LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)

718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. LeetCode718. Maximum Length of Repeated Subarray 示例: 输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连

[LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组

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: 3 Explanation: The repeated subarray with maximum length is [3, 2, 1]. Note: 1 <= len(A),

[leetcode] 718. Maximum Length of Repeated Subarray

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: 3 Explanation: The repeated subarray with maximum length is [3, 2, 1]. Note: 1 <= len(A),

718. Maximum Length of Repeated Subarray

#week9 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: 3 Explanation: The repeated subarray with maximum length is [3, 2, 1]. Note: 1 <= l

Swift入门(五)——数组(Array)

集合 集合的定义 Swift中提供了两种数据结构用于存放数据的集合,分别是数组(Array)和字典(Dictionary).他们的主要区别在于数组中的元素由下标确定,而字典中的数据的值由数据的键(Key)决定.以下我们认为集合就是数组或字典. 集合的可变性 我们可以定义一个集合常量或者集合变量.一旦定义为常量,就意味着集合的长度.内容和顺序都不能再修改了.比如,定义为常量的数组,不能再向其中添加新的元素. 数组的创建 由于swift中变量的创建遵循" var 变量名:变量类型 "的语法

最长连续公共子串、最长公共子串(可以非连续)、最长回文串(连续)、最长回文串(可以不连续)、最长递增数组的求解

问题:最长连续公共子串.最长公共子串(可以非连续).最长回文串(连续).最长回文串(可以不连续).最长递增数组.长方形镶嵌最多的求解 方法:上述问题有相似性,都可以采用动态规划进行求解. (1)最长连续公共子串: 如果A[i]==B[j], dp[i][j]=dp[i-1][j-1]+1; 否则,dp[i][j]=0; (2)最长公共子串(可非连续): 如果A[i]==B[j], dp[i][j]=dp[i-1][j-1]+1; 否则,dp[i][j]=dp[i-1][j-1]; (3)最长回文

Swift学习笔记四:数组和字典

最近一个月都在专心做unity3d的斗地主游戏,从早到晚,最后总算是搞出来了,其中的心酸只有自己知道.最近才有功夫闲下来,还是学习学习之前的老本行--asp.net,现在用.net做项目流行MVC,而不是之前的三层,既然技术在更新,只能不断学习,以适应新的技术潮流! 创建MVC工程 1.打开Visual studio2012,新建MVC4工程 2.选择工程属性,创建MVC工程 3.生成工程的目录 App_Start:启动文件的配置信息,包括很重要的RouteConfig路由注册信息 Conten

*Maximum Length Palindromic Sub-Sequence of an Array.

/* Write a function to compute the maximum length palindromic sub-sequence of an array. A palindrome is a sequence which is equal to its reverse. A sub-sequence of an array is a sequence which can be constructed by removing elements of the array. Ex:

Maximum length of a table name in MySQL

http://dev.mysql.com/doc/refman/5.7/en/identifiers.html The following table describes the maximum length for each type of identifier. Identifier Maximum Length (characters) Database 64 (NDB storage engine: 63) Table 64 (NDB storage engine: 63) Column