[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插件化-apkplugdemo源代码阅读指南-10
			
阅读本节内容前可先了解 apkplug基础教程 本教程是基于apkplug V1.6.8 版本号编写 最新开发方式以官网为准 可下载最新的apkplugdemo源代码http://git.oschi ...
 - 关于matlab鼠标响应
			
今天看了一下Matlab中响应鼠标的事件,整理如下, (1)函数WindowButtonMotionFcn,当鼠标在窗口上运动的时候就会相应此函数,于是在此函数中可以设置运动时想要的代码,如:改变鼠标 ...
 - matlab GUI之常用对话框(二)-- 进度条的使用方法
			
常用对话框(二) 进度条 waitbar 调用格式: h = waitbar(x,'message') waitbar(x,'message','CreateCancelBtn','button ...
 - hive 学习笔记精简
			
创建表: drop table t create table if not exists t (t string) partitioned by (log_date string) row forma ...
 - 从 NSURLConnection 到 NSURLSession
			
iOS 7 和 Mac OS X 10.9 Mavericks 中一个显著的变化就是对 Foundation URL 加载系统的彻底重构. 现在已经有人在深入苹果的网络层基础架构的地方做研究了,所以我 ...
 - mysql 正确的关闭方式
			
./bin/mysqladmin -uroot -p123456 -S /home/allen/var/mysql/mysql.sock shutdown
 - MySQL 基础学习
			
http://www.w3school.com.cn/sql/ 1.limit x,y 或 limit z :选取从x开始的y条数据 或 选取最开始的 z条数据 , 2.like '%N%' : ...
 - PDO简介
			
php链接数据库 半年后需要更换mysql为集群模式或者有钱了升级oracl数据库,这时的改动相当大,成本高.如果再之前使用PDO,之后再遇见这样的问题就很轻松. 开启PDO: 打开php.ini文件 ...
 - php功能---删除空目录
			
header('content-type:text/html;charset:utf-8'); function display($dir){ //判断是否是一个目录 if(!is_dir($dir) ...
 - 安装64位Oracle 10g超详细教程
			
安装64位Oracle 10g超详细教程 1. 安装准备阶段 1.1 安装Oracle环境 经过上一篇博文的过程,已经完成了对Linux系统的安装,本例使用X-Manager来实现与Linux系统的连 ...