LeetCode--Longest Consecutive Sequence(最长连续序列) Python
题目描述:
Longest Consecutive Sequence(最长连续序列)
中文:
给定一个未排序的整数数组,找出最长连续序列的长度。
要求算法的时间复杂度为 O(n)。
英文:
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Your algorithm should run in O(n) complexity.
class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
Dict = {}
for i in nums:
Dict[i] = 1
List = Dict.keys()
count_max = 0
while(len(Dict)):
List = Dict.keys()
count = 1
temp_left = List[0]
temp_right = List[0]
Dict.pop(temp_left)
while(1):
if temp_left-1 in Dict and temp_right+1 in Dict:
count = count+2
temp_left = temp_left-1
temp_right = temp_right+1
Dict.pop(temp_left)
Dict.pop(temp_right)
continue
elif temp_left-1 not in Dict and temp_right+1 in Dict:
count = count+1
temp_right = temp_right+1
Dict.pop(temp_right)
continue
elif temp_left-1 in Dict and temp_right+1 not in Dict:
count = count+1
temp_left = temp_left-1
Dict.pop(temp_left)
continue
else:
if count>count_max:
count_max=count
break
else:
break
return count_max
题目来源:力扣
LeetCode--Longest Consecutive Sequence(最长连续序列) Python的更多相关文章
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- 298. Binary Tree Longest Consecutive Sequence最长连续序列
[抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...
- 128. Longest Consecutive Sequence最长连续序列
[抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- LeetCode——Longest Consecutive Sequence
LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode: Longest Consecutive Sequence 解题报告
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [Leetcode] Longest Consecutive Sequence 略详细 (Java)
题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
- LeetCode--128--最长连续序列(python)
给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2]输出: 4解释: 最长连续序列是 [1, 2, 3, ...
- LeetCode: Longest Consecutive Sequence [128]
[题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...
随机推荐
- 批量定时任务将rtf文件转为docx,入参是rtf文件夹,生成一个docx文件夹
java,python等语言对于rft的处理很受限,rtf提供了很少的api供外部调用处理,但是对于docx我们却又很多api来处理,所以很多人会有需求将rtf批量转为docx的需求,接下来就来说说解 ...
- Python 语音识别
调用科大讯飞语音听写,使用Python实现语音识别,将实时语音转换为文字. 参考这篇博客实现的录音,首先在官网下载了关于语音听写的SDK,然后在文件夹内新建了两个.py文件,分别是get_audio. ...
- python tkinter开始
tkinter是python自带的GUI库,所以用起来会比较简单 运行一个什么都没有的窗口 import tkinter window=tkinter.Tk()#窗口类定义 window.mainlo ...
- springMVC带参数请求重定向
SpirngMVC返回逻辑视图名 可以分下面几种情况: 1. servlet进行请求转发,返回到jsp页面,如 return "index.jsp" ; 2. servlet 返 ...
- 透明的UITableView
// // ViewController.m // 透明table // // Created by LiuWei on 2018/4/23. // Copyright © 2018年 xxx. Al ...
- Ubuntu Visual code安装与使用
1.直接启动软件中心,输入visual studio code,点击install即可,千万千万不要去装逼搞什么linux指令安装,死都不知道怎么死的 2.Visual code是以文件夹为工程目录的 ...
- 使用CSS在页面中嵌入字体
http://jingyan.baidu.com/article/3065b3b6e9b2d9becff8a4c1.html 首先感谢css9.net照抄原话: 字体使用是网页设计中不可或缺的一部分. ...
- [CSP-S模拟测试]:涂色游戏(DP+组合数+矩阵快速幂)
题目描述 小$A$和小$B$在做游戏.他们找到了一个$n$行$m$列呈网格状的画板.小$A$拿出了$p$支不同颜色的画笔,开始在上面涂色.看到小$A$涂好的画板,小$B$觉得颜色太单调了,于是把画板擦 ...
- [TensorFlow 2] [Keras] fit()、fit_generator() 和 train_on_batch() 分析与应用
前言 是的,除了水报错文,我也来写点其他的.本文主要介绍Keras中以下三个函数的用法: fit()fit_generator()train_on_batch()当然,与上述三个函数相似的evalua ...
- error C2065: ‘_bstr_t’ : undeclared identifier
转自VC错误:http://www.vcerror.com/?p=828 问题描述: error C2065: '_bstr_t' : undeclared identifier 解决方法: 详细的解 ...