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 ...
随机推荐
- ActiveMQ基础简介
1. 什么是ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现 ...
- vue 入门1 组件管理
全局 组件.局部组件 // Vue.component('todo-list',{ // template:'<li >item</li>' // }); //全局 // va ...
- 【异常记录(十)】 接口调用,提示跨域 Cross-domain calling interface, Access-Control-Allow-Origin
头的 Access-Control-Allow-Origin(允许访问的域) 改成 * : Response.AddHeader("Access-Control-Allow-Origin&q ...
- C#DataTable导出Excel,并实现合并单元格
asp.net webwofrm后台代码----------建议Framework4.0及以上,3.5试过出现好多莫名错误... 首先导入两个程序集.我的是 office2003,引用的COM里面的 ...
- matlab 基础知识1
一.数组和矩阵注意 逗号 和 分号 的区别 向量生成方式: 传统方式:行向量 :空格,逗号列向量 :分号,回车 函数方式: x = linspace(a,b,n) 等分关系,从a 到 b, n等分.n ...
- Web界面开发必看!Kendo UI for jQuery编辑功能指南第二弹
Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...
- 3 监控项、触发器、web监控、nginx监控
1.自定义监控项 Item 参数文档 https://www.zabbix.com/documentation/3.0/manual/config/items/itemtypes/zabbix_age ...
- 1 FBV与CBV,前后端分离(初识),postman
yuan的Blog:https://www.cnblogs.com/yuanchenqi/articles/8715364.html alice的Blog:https://www.cnblogs.co ...
- Neko Performs Cat Furrier Transform CodeForces - 1152B 二进制思维题
Neko Performs Cat Furrier TransformCodeForces - 1152B 题目大意:给你一个x,在40步操作以内把x变成2m−1,m为非负整数.对于每步操作,奇数步可 ...
- 洛谷 P1135 奇怪的电梯 (dfs)
题目描述 呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都可以停电梯,而且第i层楼(1<=i<=N)上有一个数字Ki(0<=Ki<=N).电梯只有四个按钮:开 ...