GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/code/lc1027.java at master · mJackie/leetcode

Expand file tree

/

lc1027.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
24 lines (24 loc) · 928 Bytes
/

lc1027.java

Copy path

File metadata and controls

24 lines (24 loc) · 928 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package code;
/*
* 1027. Longest Arithmetic Sequence
* 题意:最长等差数列
* 难度:Medium
* 分类:Dynamic Programming
* 思路:还是要在每个位置上都记录一下,不是说一个大的数组就可以了。因为相同长度的等差,后续数字往上接的时候不一定接哪一个,都要保存,并不是说一定去接小的那个。
* 二维dp, 一维记录位置,一维记录差分
* Tips:很棒的题
*/
public class lc1027 {
public static int longestArithSeqLength(int[] A) {
int res = 0;
int[][] dp = new int[A.length][20000];
for (int i = 0; i < A.length ; i++) {
for (int j = 0; j < i ; j++) {
int ind = A[i]-A[j]+10000; //可能是负数,做一个偏移
dp[i][ind] = dp[j][ind] + 1;
res = Math.max(res, dp[i][ind]);
}
}
return res+1; //加1
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.