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 ...
随机推荐
- CenterOS 7安装Nginx
1.wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm下载对 ...
- html/css 图片展示效果
demo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- struts2结果跳转和参数获取
一.结果跳转方式 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC ...
- 【leetcode】1007. Minimum Domino Rotations For Equal Row
题目如下: In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino. ( ...
- Redis原理及拓展
Redis是单线程程序.单线程的Redis为何还能这么快? 1.所有的数据都在内存中,所有的运算都是内存级别的运算(因此时间复杂度为O(n)的指令要谨慎使用) 2.单线程操作,避免了频繁的上下文切换 ...
- Redis事件通知示例
1. redis如同zk一样,提供了事件监听(或者说是回调机制), 下面是redis的配置说明: ############################# EVENT NOTIFICATION ## ...
- Servlet 第一天
package com.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet. ...
- LDD3 第10章 中断处理
各种硬件和处理器打交道的周期不同,并且总是比处理器慢.必须有一种可以让设备在产生某个事件时通知处理器----中断. 中断仅仅是一个信号,如果硬件需要,就可以发送这个信号.Linux处理中断方式和用户空 ...
- ckeditor实现WORD粘贴图片自动上传
自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...
- 【HDU6609】Find the answer【线段树】
题目大意:给你一个序列,对于每个i,你可以选择1~i-1中任意多的数并将它删去,剩余的数(包括i)∑≤m,问对于每个i最少删几个数可以达到要求 题解: 考虑朴素的思想,对于每个i,我只需要删去最大的若 ...