[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 ...
随机推荐
- jeecms 2012 源码分析(2) 前台栏目页静态化分析
还是要说到web.xml文件 <welcome-file-list> <welcome-file>index.html</welcome-file> <wel ...
- javascript DOM,它到底是什么-------Day32
这一晚上看的我是头疼不已啊,为什么呢? 终究是半路出家,我对javascript的理解仅仅停留在:调用javascript,改变页面样式,元素和实现一些事件的响应,尽管须要的时候可能会用,可是到底使用 ...
- WEBAPP组件化时代, Web Components
polymer ==> http://docs.polymerchina.org/ angular ==> http://www.ngnice.com/docs/guide scr ...
- 根据输出设置select的被选中值
$("#startupStatus").find("option").map(function(i) { if ($('#st-status').val() = ...
- 2014.8.30.ref,out,params,enum,递归
(一)ref 函数形参变量的输入有两种方式:传值,传址.而ref则为传址.eg: static int Add(ref int n) { Console.WriteLine("Add---- ...
- 修改UISearchBar背景颜色
UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性.方法一:是直接将 UISearc ...
- iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见
iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见 (2015-12-05 12:48:20)[编辑][删除] 转载▼ 首先我们先明确一下问题: 1.因为UI是在主线 ...
- 对于System.Net.Http的学习(一)——System.Net.Http 简介(转)
最新在学习System.Net.Http的知识,看到有篇文章写的十分详细,就想转过来,自己记录下.原地址是http://www.cnblogs.com/chillsrc/p/3439215.html? ...
- 多关键字排序(里面有关于操作符(<<运算符 和 >>运算符 )的重载)
一种排序 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数:现 ...
- BZOJ 3230: 相似子串( RMQ + 后缀数组 + 二分 )
二分查找求出k大串, 然后正反做后缀数组, RMQ求LCP, 时间复杂度O(NlogN+logN) -------------------------------------------------- ...