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 ...
随机推荐
- python之时间日期calendar
calendar是与日历相关的模块,calendar模块文件里定义了很多类型,主要有Calendar,TextCalendar以及HTMLCalendar类型.其中,Calendar是TextCale ...
- SSH安装配置
一.环境准备 二.SSH配置 1.root用户进入home目录,确实有无隐藏文件夹 .ssh cd ~ ls -lrta 2.有,则跳过本步骤:没有,执行如下命令 ##根据提示输入当前用户密码 ssh ...
- java_day10_多线程
第十章:线程 1.进程和线程的概述 1)进程和线程定义 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位. 线程是进程的一个实体,是CPU调度和 ...
- deep_learning_Function_One-host参数
在很多机器学习任务中,特征并不总是连续值,而有可能是分类值.比如星期类型,有星期一.星期二.…….星期日,比如浏览器类型有["uses Firefox", "uses C ...
- 异步通信&同步通信
首先是两者的不同: 同步通信要求接收端时钟频率和发送端时钟频率一致,发送端发送连续的比特流:异步通信时不要求接收端时钟和发送端时钟同步,发送端发送完一个字节后,可经过任意长的时间间隔再发送下一个字节. ...
- 基于partition的递归
partition算法可以应用在快速排序算法中,也可以应用到 Selection algorithm(在无序数组中寻找第K大的值) Partition 实现 快速排序中用到的 partition 算法 ...
- String类型为什么不可变
在学习Java的过程中,我们会被告知 String 被设计成不可变的类型.为什么 String 会被 Java 开发者有如此特殊的对待?他们的设计意图和设计理念到底是什么?因此,我带着以下三个问题,对 ...
- Linux根文件系统和目录结构及bash特性4
文件管理工具:cp,mv,rm cp命令:copy cp命令主要用于复制文件或目录 语法: 单源复制 cp [OPTION]... [- ...
- 使用metamask钱包
一.安装火狐浏览器metamask插件 打开火狐浏览器的附件组件,搜索metamask 点击第一个 点击“添加到Firefox” 添加成功后,浏览器右上角有一个狐狸标志 点击这个标志,打开插件 二.创 ...
- Python3之Requests模块详解
# 导入 Request模块 # 若本机无自带Request模块,可自行下载或者使用pip进行安装 # python版本Python3 import requests import json #### ...