题目如下:

Given an array A of integers, return the length of the longest arithmetic subsequence in A.

Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).

Example 1:

Input: [3,6,9,12]
Output: 4
Explanation:
The whole array is an arithmetic sequence with steps of length = 3.

Example 2:

Input: [9,4,7,2,10]
Output: 3
Explanation:
The longest arithmetic subsequence is [4,7,10].

Example 3:

Input: [20,1,15,3,10,5,8]
Output: 4
Explanation:
The longest arithmetic subsequence is [20,15,10,5].

Note:

  1. 2 <= A.length <= 2000
  2. 0 <= A[i] <= 10000

解题思路:首先用字典记录A中每个元素出现的下标,接下来求出任意A[i]与A[j]的差值d,依次判断A[j] += d是否存在于A中,并且要求A[j] + d的下标的最小值要大于j,最终即可求出最长的等差数列。

代码如下:

class Solution(object):
def longestArithSeqLength(self, A):
"""
:type A: List[int]
:rtype: int
"""
import bisect
res = 0
dic = {}
for i,v in enumerate(A):
dic[v] = dic.setdefault(v,[]) + [i]
for i in range(len(A)):
for j in range(i+1,len(A)):
count = 2
diff = A[j] - A[i]
next = A[j] + diff
smallestInx = j
while True:
if next not in dic:
break
inx = bisect.bisect_right(dic[next],smallestInx)
if inx == len(dic[next]):
break
smallestInx = dic[next][inx]
next = next + diff
count += 1
res = max(res,count)
return res

【leetcode】1027. Longest Arithmetic Sequence的更多相关文章

  1. 【leetcode】1218. Longest Arithmetic Subsequence of Given Difference

    题目如下: Given an integer array arr and an integer difference, return the length of the longest subsequ ...

  2. 【LeetCode】128. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  3. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  4. LeetCode 1027. Longest Arithmetic Sequence

    原题链接在这里:https://leetcode.com/problems/longest-arithmetic-sequence/ 题目: Given an array A of integers, ...

  5. 【LeetCode】159. Longest Substring with At Most Two Distinct Characters

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...

  6. 【leetcode】300.Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  7. 【LeetCode】521. Longest Uncommon Subsequence I 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【leetcode】1048. Longest String Chain

    题目如下: Given a list of words, each word consists of English lowercase letters. Let's say word1 is a p ...

  9. 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...

随机推荐

  1. benchmarks

    系统性能测试 stream SPARK 测试 streaming benchmark https://github.com/yahoo/streaming-benchmarks

  2. 家用路由器网络设置DMZ区

    2分钟看懂DMZ区 装载 原文链接 最近看到一个名词“DMZ区”,一直充满疑问,今天对其进行了查询,理解如下: 1.DMZ是什么? 英文全名“Demilitarized Zone”,中文含义是“隔离区 ...

  3. struts2 基础3 文件上传、拦截器

    文件上传: 1.将头设置为enctype=”multipart/form-data” <form action="${pageContext.request.contextPath } ...

  4. Python笔记(二十八)_魔法方法_迭代器

    迭代器用于遍历容器中的数据,但它不是容器,它是一个实现了__next__方法的对象 与迭代器相关的内置函数: iter(): 将一个对象转换成一个迭代器 next(): 访问迭代器中的下一个变量,直到 ...

  5. oracle linux 7使用udev绑盘操作

    参考:Oracle Linux 7: Udev rule for ASM Cannot Place the ASM Disk in a Directory under /dev (Doc ID 221 ...

  6. Git016--Work

    GIT常用命令 git常用命令: //初始化git目录: $ git init //把当前目录变成git可以管理的仓库 //添加文件到暂存区 $ git add file //把文件添加到仓库 $ g ...

  7. Linq查询语法(2)

    转:http://www.cnblogs.com/knowledgesea/p/3897665.html 1.简单linq查询 var ss = from r in db.Am_recProSchem ...

  8. sql select as

    as 可理解为:用作.当成,作为:一般式重命名列名或者表名.例如有表table, 列 column_1,column_2 你可以写成 select column_1 as 列1,column_2 as ...

  9. python函数-语句

    一.def语句和参数 #!/usr/bin/env python #coding:utf-8 def hello(name): print('Hello ' +name) hello('dingkai ...

  10. [Python3 填坑] 011 元组中有多个最值,索引出来的是哪一个

    目录 1. print( 坑的信息 ) 2. 开始填坑 (1) max() (2) min() (3) 结论 1. print( 坑的信息 ) 挖坑时间:2019/01/11 明细 坑的编码 内容 P ...