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 ...
随机推荐
- 最新Cocoapods 安装及使用
1.移除现有Ruby默认源 gem sources --remove https://rubygems.org/ 2.使用新的源 gem sources -a https://ruby.taobao. ...
- 【转】FireMonkey ListView 自动计算行高
说明:展示 ListView 视其每一行 Item 的 Detail 字串长度自动调整高度(可每行高度不同). 适用:Delphi XE7 / XE8 源码下载:[原创]ListView_自动计算行高 ...
- axios表单提交,delete,get请求(待完善)
import { mapMutations} from 'vuex' import axios from 'axios' const mixins = { data() { return { } }, ...
- Java学习笔记【五、字符串】
String类 11种构造,不一一列举 常用方法 s.length() 返回字符串长度 s1.contact(s2) 连接s1.s2 String.format("aaa %f bbb %d ...
- linux之网络命令
本文整理了在实践过程中使用的Linux网络工具,这些工具提供的功能非常强大,我们平时使用的只是冰山一角,比如lsof.ip.tcpdump.iptables等. 本文不会深入研究这些命令的强大用法,因 ...
- 使用比特币轻量钱包Electrum
一.选择对应的钱包安装程序 浏览器打开 https://electrum.org/#download 选择windwos版本的安装程序 点击下载,并安装 二.运行Electrum的 testnet版本 ...
- MYSQL获得查询记录的行号
对于获得查询记录的行号这一功能,Oracle 中可以使用标准方法(8i版本以上),也可以使用非标准的ROWNUM,MS SQL Server 则在 2005 版本中提供了ROW_NUMBER()函数. ...
- Linux文件归档管理
Linux怎样保存文件 数据 - 这里数据就是文件的内容 元数据 - 在linux系统中,所有与某个文件相关的额外信息都保存在一个叫做i-节点(inode)的结构中 文件名 - 文件名保存在名为目录项 ...
- 电脑同时安装了python2和python3后,随意切换版本并使用pip安装
第一步: python2安装路径下python.exe重命名为python2.exe,python3安装路径下python.exe重命名为python3.exe; 第二步: 分别为python2.ex ...
- 制作自定义系统iso镜像
一.制作自己的ISO启动盘篇 在需要安装特定系统的时候,我们使用原版的linux系统盘镜像来安装,需要手动操作N多步,在机器非常多的环境下,这种方式显然不理想,这是我我们就需要制作我们特定的系统盘来简 ...