Longest Consecutive Sequence——Leetcode
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
Your algorithm should run in O(n) complexity.
题目大意:给一个无序数组,找出最大的连续元素的长度。
说真的,一开始这题,我想半天只想到用boolean数组mark,后来看了别人的才发现可以用set做。
解题思路:
把所有的数组放到set中,然后遍历数组的元素,然后分别向前、向后寻找set中是否存在,记录max。
public int longestConsecutive(int[] nums) {
if(nums==null||nums.length==0){
return 0;
}
Set<Integer> set = new HashSet<>();
for(int num:nums){
set.add(num);
}
int max = 0;
for(int i=0;i<nums.length;i++){
Integer num = nums[i];
int cnt = 1;
while(set.contains(--num)){
cnt++;
set.remove(num);
}
num=nums[i];
while(set.contains(++num)){
cnt++;
set.remove(num);
}
max=Math.max(max,cnt);
}
return max;
}
Longest Consecutive Sequence——Leetcode的更多相关文章
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence -- LeetCode
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- Longest Consecutive Sequence [LeetCode]
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Longest Consecutive Sequence leetcode java
题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- Spring各种传播特性源码实现的概览
这几天都在分析Spring的源码实现,看到事务管理的部分 我们知道事务的传播特性有下面几种,我标黄的就是最常用的3中传播特性, Sping在发生事务嵌套的时候,会依据内层事务的传播特性,来决定内层是事 ...
- Android本地JUnit Text
Android本地JUnit Text 步骤 创建一个和source文件,里面添加和src目录相同的包. 在AndroidManifest.xml文件manifest根节点添加如下文件 <ins ...
- 2.RABBITMQ 入门 - WINDOWS - 生产和消费消息 一个完整案例
关于安装和配置,见上一篇 1.RABBITMQ 入门 - WINDOWS - 获取,安装,配置 公司有需求,要求使用winform开发这个东西(消息中间件),另外还要求开发一个日志中间件,但是也是要求 ...
- KVC 和 OC字典
KVC(键值编码)和OC 字典很相似,都是键值存储.但是OC 字典比较灵活,它是一种映射. [dict setObject:<#(id)#> forKey:<#(id<NSCo ...
- WPF常用控件应用demo
WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...
- Java RMI 框架_远程方法调用(2016-08-16)
概念: Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可 ...
- thinkphp框架使用心得
接触的第一个PHP框架就是TP,在使用的了一段时间后就放弃了,说实话TP的弊端挺多,之后又接触laravel框架,慢慢的就爱上laravel这个框架了.这段时间由于公司的原因,又不得不使用thinkp ...
- Git新手入门手册
1.配置email及name git config --global user.email "guxuelong@f-road.com.cn" git config --globa ...
- CodeIgniter框架介绍
入口 index.php :一些路径常量定义: => CodeIgniter.php : 1. 初始化一些 system/core内的类,例如Config, Utf8, URI 和Router等 ...
- Linux进程和进程边界
1. 进程和线程 2. 手机操作系统的发展 3. 进程的地址空间边界 4. 进程边界的安全围栏: Crash的不可扩延性 5. 进程边界的安全围栏: 全局数据和服务的不可访问性 http://www. ...