From c21781366eaffb729178f08844ca105ba1e5dd8d Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Tue, 10 Aug 2021 16:19:00 +0800 Subject: [PATCH] =?UTF-8?q?1027:=E6=9C=80=E9=95=BF=E7=AD=89=E5=B7=AE?= =?UTF-8?q?=E6=95=B0=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/LongestArithmeticSubsequence.java | 80 +++++++++++++++++++ .../editor/cn/LongestArithmeticSubsequence.md | 39 +++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/LongestArithmeticSubsequence.java create mode 100644 src/main/java/leetcode/editor/cn/LongestArithmeticSubsequence.md diff --git a/src/main/java/leetcode/editor/cn/LongestArithmeticSubsequence.java b/src/main/java/leetcode/editor/cn/LongestArithmeticSubsequence.java new file mode 100644 index 0000000..4ccd36e --- /dev/null +++ b/src/main/java/leetcode/editor/cn/LongestArithmeticSubsequence.java @@ -0,0 +1,80 @@ +//给定一个整数数组 A,返回 A 中最长等差子序列的长度。 +// +// 回想一下,A 的子序列是列表 A[i_1], A[i_2], ..., A[i_k] 其中 0 <= i_1 < i_2 < ... < i_k <= A +//.length - 1。并且如果 B[i+1] - B[i]( 0 <= i < B.length - 1) 的值都相同,那么序列 B 是等差的。 +// +// +// +// 示例 1: +// +// 输入:[3,6,9,12] +//输出:4 +//解释: +//整个数组是公差为 3 的等差数列。 +// +// +// 示例 2: +// +// 输入:[9,4,7,2,10] +//输出:3 +//解释: +//最长的等差子序列是 [4,7,10]。 +// +// +// 示例 3: +// +// 输入:[20,1,15,3,10,5,8] +//输出:4 +//解释: +//最长的等差子序列是 [20,15,10,5]。 +// +// +// +// +// 提示: +// +// +// 2 <= A.length <= 2000 +// 0 <= A[i] <= 10000 +// +// Related Topics 数组 哈希表 二分查找 动态规划 +// 👍 144 👎 0 + +package leetcode.editor.cn; + +import java.util.Arrays; + +//1027:最长等差数列 +class LongestArithmeticSubsequence { + public static void main(String[] args) { + //测试代码 + Solution solution = new LongestArithmeticSubsequence().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int longestArithSeqLength(int[] nums) { + int max = 0; + for (int num : nums) { + max = Math.max(max, num); + } + int[][] dp = new int[nums.length][max*2+1]; + for (int[] t:dp){ + Arrays.fill(t,-1); + } + + int result = 2; + for (int i = 0; i < nums.length; i++) { + for (int j = 0; j < i; j++) { + int index = nums[i]-nums[j]+max; + dp[i][index]=Math.max(2,dp[j][index]+1); + result = Math.max(result,dp[i][index]); + } + } + return result; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/LongestArithmeticSubsequence.md b/src/main/java/leetcode/editor/cn/LongestArithmeticSubsequence.md new file mode 100644 index 0000000..4f32908 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/LongestArithmeticSubsequence.md @@ -0,0 +1,39 @@ +

给定一个整数数组 A,返回 A 中最长等差子序列的长度

+ +

回想一下,A 的子序列是列表 A[i_1], A[i_2], ..., A[i_k] 其中 0 <= i_1 < i_2 < ... < i_k <= A.length - 1。并且如果 B[i+1] - B[i]0 <= i < B.length - 1) 的值都相同,那么序列 B 是等差的。

+ +

 

+ +

示例 1:

+ +
输入:[3,6,9,12]
+输出:4
+解释: 
+整个数组是公差为 3 的等差数列。
+
+ +

示例 2:

+ +
输入:[9,4,7,2,10]
+输出:3
+解释:
+最长的等差子序列是 [4,7,10]。
+
+ +

示例 3:

+ +
输入:[20,1,15,3,10,5,8]
+输出:4
+解释:
+最长的等差子序列是 [20,15,10,5]。
+
+ +

 

+ +

提示:

+ +
    +
  1. 2 <= A.length <= 2000
  2. +
  3. 0 <= A[i] <= 10000
  4. +
+
Related Topics
  • 数组
  • 哈希表
  • 二分查找
  • 动态规划
  • \n
  • 👍 144
  • 👎 0
  • \ No newline at end of file