[LeetCode]题解(python):128-Longest Consecutive Sequence
题目来源:
https://leetcode.com/problems/longest-consecutive-sequence/
题意分析:
给定一个没有排好序的数组,找到最长的连续序列的长度。要求时间复杂度是O(n)。比如[100, 4, 200, 1, 3, 2],其最长长度是[1,2,3,4]长度为4.
题目思路:
对于每个数记录他所在的目前最长的序列,将其±1,如果其也在给定序列中,那么更新他所在的最长序列,比如上面的例子。所对应的序列变化是:
1. 100:[100,100];
2.100:[100,100];4:[4,4];
3.100:[100,100];4:[4,4];200:[200,200];
4.100:[100,100];4:[4,4];200:[200,200];1:[1,1];
5. 100:[100,100];4:[3,4];200:[200,200];1:[1,1];3 :[3,4];
6.100:[100,100];4:[1,4];200:[200,200];1:[1,4];3 :[3,4],2:[1,2];
所以得到答案是4
代码(python):
class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count = {}
ans = 0
if len(nums) != 0: ans = 1
for i in nums:
#ans = max(ans,1)
if i in count: continue
count[i] = [i,i]
if i - 1 in count:
#print(i - 1,count[i - 1])
count[count[i - 1][0]][1],count[i][0] = count[i][1],count[i - 1][0]
#print(count[i - 1][0],count[count[i - 1][0]],i,count[i])
ans = max(ans,count[count[i - 1][0]][1] + 1 - count[count[i - 1][0]][0])
if i + 1 in count:
#print(i + 1,count[i + 1])
count[count[i + 1][1]][0],count[count[i][0]][1] = count[i][0],count[i+1][1]
ans = max(ans,count[count[i + 1][1]][1] - count[count[i + 1][1]][0] + 1)
#print(count[i + 1][1],count[count[i + 1][1]],count[i][0],count[count[i][0]])
#for i in count:
#print(i,count[i])
#ans = max(ans,count[i][1] - count[i][0] + 1)
return ans
[LeetCode]题解(python):128-Longest Consecutive Sequence的更多相关文章
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【LeetCode】128. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- LeetCode 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- android 回调
调函数(callback Function),顾名思义,用于回调的函数. 回调函数只是一个功能片段,由用户按照回调函数调用约定来实现的一个函数.回调函数是一个工作流的一部分,由工作流来决定函数的调用 ...
- strtus2.3 java.lang.NoSuchFieldException: DEFAULT_PARAM>
strtus2.3.15.1 的bug请下载 http://download.csdn.net/detail/livalue/6229373 或加群到群共享中下载.214579879
- Spring MVC 简单介绍
Spring MVC 是典型的mvc架构,适合web开发. controler 输入输出的控制器,也是对外view提供数据的接口,调用service层. model 数据,由bean组成(相应表),关 ...
- IOS7 position:fixed 定位问题
在IOS7下position:fixed定位会出一些bug. 输入框 focus 状态下 fixed会随之改变.参见该页面详细描述(http://www.cnblogs.com/zhangdaipin ...
- web前端学习之路
test 随着自己对于web前端知识了解的越多,越来越发现自己真的好菜 一脸茫然阶段 两年前大学接触网页设计,那时对于网页设计一窍不通,只是看了一本自己大学编的一本入门教材,我甚至不知道那些网页设计的 ...
- Android程序捕获未处理异常,处理与第三方方法冲突时的异常传递
自己的android程序对异常进行了处理,用的也是网上比较流行的CrashHandler,代码如下,就是出现了未处理的异常程序退出,并收集收集设备信息和错误信息仪器保存到SD卡,这里没有上传到服务器. ...
- node中的get请求和post请求的不同操作【node学习第五篇】
获取get的请求内容 /** * Created by Administrator on 2016/8/5. */ var http = require("http"); var ...
- hdu4099
要想通这个题目应该很容易,由于斐波纳契数在近100项之后很大,早就超出long long了.而输入最长的序列才40个数字,所以大约保留前50位,前40位是没有误差的!!!其实,想想我们判断double ...
- IIS发布网站后局域网其他用户不能访问问题(转)
如果本机能正常访问,而局域网其他用户不能访问,那么判断的结果很可能是防火墙问题. 解决方法: 既然问题出在Windows7或Windows Server 2008 R2的防火墙上,那么我们可以有以下两 ...
- XSS【跨站脚本攻击】
从客户端(txt="<script><a href="www...")中检测到有潜在危险的 Request.Form 值. 如果你使用的是.NET 3. ...