[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 ...
随机推荐
- MySQL 5.7.10 免安装配置
# 配置环境:windows 64bit # 安装版本:mysql-5.7.10-win32(zip archive版本) 1. ZIP Archive版是免安装的,只需把mysql-5.7.10-w ...
- 使用 AngularJS 从零构建大型应用
0.导言 1.准备工作 2.构建框架 3.丰富你的directives 4.公用的services 5.用controllers组织业务 导言 纵览线上各种AngularJS教程,大部分都是基础与一些 ...
- node.js的一些知识
什么是node.js node.js是构建于chrome浏览器v8引擎上的一个js运行环境 可以解析和执行js代码 可以当做另一种上下文,脱离浏览器环境(后端)运行js代码,而代码解析就是基于V8引擎 ...
- 成功的背后!(给所有IT人)----转载:来自CSDN第一名博主
转载:来自CSDN第一名博主:http://blog.csdn.net/phphot/article/details/2187505 放在这里激励你我! 正文: 成功的背后,有着许多不为人知的故事,而 ...
- .Net跨平台中遇到的问题小结
一:创建MVC网站,需要修改Views视图文件夹下的Web.config 二:需要添加web.config中的 <system.web> <compilation debug=&qu ...
- composer api 参考
composer note 简介 composer 是php的依赖管理工具.可以声明项目所依赖的库,composer会帮我们安装上 composer 默认基于项目来管理和安装库(包),将依赖的库安装到 ...
- Scale-up(纵向扩展) vs Scale-out(横向扩展)
转载:http://wuaner.iteye.com/blog/1843799 http://www.javaworld.com/article/2077780/java-web-developmen ...
- Jsoup代码解读之三-Document的输出
Jsoup代码解读之三-Document的输出 Jsoup官方说明里,一个重要的功能就是output tidy HTML.这里我们看看Jsoup是如何输出HTML的. HTML相关知识 分析代码前 ...
- HelloX项目github协同开发指南
概述 为了提高协同开发效率,HelloX项目已托管到github网站上.根据目前的开发进展,创建了下列几个子项目: HelloX操作系统内核项目:https://github.com/hellox-p ...
- ubuntu openStack icehouse dashboard theme自定义
1,ubuntu openStack 语言包locate