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 ...
随机推荐
- 当在terminal中输入一行命令的时候,查找的顺序如何看
大多数时候,尤其是安装了anaconda的时候,我们常常会知道,实际上因为conda的环境变量写到了该用户下的.bashrc下面,所以在terminial敲如python的时候,会显示conda的py ...
- c#传入类名插入多条数据
public int Insert<T>(IReadOnlyCollection<T> models) where T : class, new() { int sucess ...
- jvm监控工具jconsole进行远程监控配置
[环境] SUSE linux11 + jdk1.6 + tomcat7 [场景] 最近在做性能测试,想通过我本地(win7)上的jdk来远程监控上述服务器的jvm相关信息. [配置] 配置上述服务器 ...
- 【异常】Phoenix异常:java.lang.ArithmeticException: Rounding necessary
1 异常sql upsert into WMBIGDATA.ODS_ES_CHARGING_STATION(id,evcosType,address,serviceTel,supportOrder,o ...
- stm32定时器计数功能
stm32的外部时钟源模式2和外部时钟源模式1都可以用来实现计数功能,他们的区别是什么呢? 以上2种模式对应不同的管脚输入: 外部时钟源模式2 <-->TIMx_ETR 外部时钟源模式1 ...
- 网络初级篇之VLAN(原理)
一.早期网络的问题 1.若某时刻有多个节点同时试图发送数据,极易产生冲突域,这样使得网络传输效率大大降低. 2.从一节点发送的数据都会被送到各个节点,极易形成广播域,这样会使得产生太多的广播流量而耗费 ...
- GitHub 上最热的10款国产开源软件
衡量一个开源产品好不好,看看产品在 GitHub 的 Star 数量就知道了.由此可见,GitHub 已经沦落为开源产品的“大众点评”了.一个开源产品希望快速的被开发者知道.快速的获取反馈,放到 Gi ...
- 高并发下的 Nginx 优化与负载均衡
高并发下的 Nginx 优化 英文原文:Optimizing Nginx for High Traffic Loads 过去谈过一些关于Nginx的常见问题; 其中有一些是关于如何优化Nginx. ...
- ssh远程连接centos7故障排除
导致故障的原因在两个方面 1.网络问题---物理链路就不通可以通过在客户端 telnet目标主机地址,例如:telnet 192.168.1.107 22 千万别忘了端口号!!!,如果通了还连不上则按 ...
- Python之hashlib模块的使用
hashlib模块主要的作用: 加密保护消息安全,常用的加密算法如MD5,SHA1等. 1.查看可用的算法有哪些 #!/usr/bin/env python # -*- coding: utf-8 - ...