leetcode-hard-array-128. Longest Consecutive Sequence
mycode 92.62%
class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
nums = sorted(set(nums))
res = 1
final = 1
for i in range(len(nums)-1):
print(nums[i],nums[i+1])
if nums[i+1] == (nums[i] + 1):
res += 1
else:
final = max(final,res)
res = 1
final = max(final,res)
return final
参考
class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums=set(nums)
maxi=0
for i in nums:
if i-1 not in nums:
y=i+1
while y in nums:
y=y+1
maxi=max(maxi,y-i)
return maxi
leetcode-hard-array-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计算QPS
首先连接上mysql: $ mysql -h .x -P3306 -uusername -p123456 进入Mysql之后,查询general_log: mysql> SHOW VARIABL ...
- Activity的跳转及返回值,activity的生命周期
Activity生命周期 从创建到销毁的生命周期: onCreate()→onStart()→onResume()→onPouse()→onStop()→onDestroy() 从起动到后台再到前台: ...
- ELK对nginx日志进行流量监控
ELK对nginx日志进行流量监控 一.前言 线上有一套ELK单机版,版本为5.2.1.现在想把nginx访问日志接入到elk里,进行各个域名使用流量带宽的统计分析.要把nginx日志传输到elk上, ...
- 4.Nginx配置文件Nginx.conf_虚拟主机配置规则
1.Nginx配置文件及各个配置项含义 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全 ...
- 编译gpu集群版caffe
在这个版本安装之前,要先装好opencv,openmpi等. 下载地址:https://github.com/yjxiong/caffe.git 我的opencv是2.4.12版本 编译是用了: cm ...
- H5微信分享相关规范
微信分享 用户调用微信的分享功能,可以自定义分享的title和描述,以及小图标和链接.可以分享到群.好友.朋友圈.QQ.QQ空间等. 分享设计规范 分享标题:14字以内,建议使用朋友般亲切的口吻 分享 ...
- STM32F103C8T6最小板搞定CMSIS-DAP和SWO功能
转载:http://www.stmcu.org.cn/module/forum/forum.php?mod=viewthread&tid=616081&extra=page%3D&am ...
- 解决telnet: connect to address 127.0.0.1: Connection refused的错误信息问题
1.检查telnet是否已安装: rpm -qa telnet 2.有输出说明已安装,如果没有输出则没有安装,使用yum install telnet进行安装 3.检查telnet-server是否已 ...
- Axure RP 8 破解汉化(实测可用)
Axure的安装教程初学乍练,从安装应用程序开始1.下载 下载地址:https://www.axure.com.cn/3510/ 2.破解:注册码(源于博主“前端大白兔”,原博客网址:http://b ...
- yii ActiveRecord
在活动记录里自定义属性(数据表里没有的属性), 起初没有注意到问题. 在这个继承了activeRecord的模型中, 还自定义了很多方法, 此为前提. 出现的问题是: 使用属性获取不到数据库的字段 ...