Java for LeetCode 128 Longest Consecutive Sequence
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.
解题思路:
由于是O(n)的时间复杂度,因此不能用排序实现,本题有点类似于第一题Java for LeetCode 001 Two Sum也需要用HashMap来实现,具体思路是先把nums装进Map中,然后对nums的每一个元素,检查其“势力范围"(本题不涉及重复元素),JAVA实现如下:
public int longestConsecutive(int[] nums) {
HashMap<Integer, Boolean> hm = new HashMap<Integer, Boolean>();
int res = 0;
for (int num : nums)
hm.put(num, false);
for (int num : nums) {
if (hm.get(num))
continue;
int width = 1;
for (int j = 1; hm.containsKey(num - j); j++, width++)
hm.put(num - j, true);
for (int j = 1; hm.containsKey(num + j); j++, width++)
hm.put(num + j, true);
res = Math.max(res, width);
}
return res;
}
Java for LeetCode 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] 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 ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- Leetcode 128. Longest Consecutive Sequence (union find)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
- Leetcode#128 Longest Consecutive Sequence
原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...
- 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 ...
随机推荐
- 聊聊、Zookeeper API
今天我们来说说 Zookeeper 客户端启动,整个文章分三个部分:第一部分是 Zookeeper 原生 API 客户端,第二部分是开源客户端 ZkClient,第三部分是开源客户端 Curator. ...
- 后台CMS日志处理记录
自从上一次添加了极光推送之后,我的工程就像是着魔了一样,不管怎么调整,日志级别都是DEBUG. 启动一次工程会打印很多无用日志,今天决定抽时间去研究了一下,最终解决了问题,下面记录一下解决过程. 1. ...
- Python Random随机数
Python产生随机数的功能在random模块中实现.实现了各种分布的伪随机数生成器 该模块能够生成0到1的浮点随机数,也能够在一个序列中进行随机选择.产生的随机数能够是均匀分布.高斯分布,对数正态分 ...
- 【Python数据分析】IPython基础
一.配置启动IPython 打开cmd窗口,在dos界面下输入ipython,结果报错了!!! 出现这个问题是由于环境变量未配置(前提:已经安装了ipython),那么接下来配置环境变量 我的电脑→右 ...
- Solr局部或指定字段更新之set用法
solr wiki文档也有 http://yonik.com/solr/atomic-updates/ java code public static void up ...
- StringBuilder作用
String 类型和 StringBuffer 类型的主要性能区别其实在于 String 是不可变的对象因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然 ...
- scikit-learn(project中用的相对较多的模型介绍):2.3. Clustering(可用于特征的无监督降维)
參考:http://scikit-learn.org/stable/modules/clustering.html 在实际项目中,我们真的非常少用到那些简单的模型,比方LR.kNN.NB等.尽管经典, ...
- 蓝桥杯OJ PREV-19 九宫重排
题目描写叙述: 历届试题 九宫重排 时间限制:1.0s 内存限制:256.0MB 问题描写叙述 如以下第一个图的九宫格中,放着 1~8 的数字卡片.另一个格子空着.与空格子相 ...
- python学习(六)元组学习
元组就是列表的一种,不过元组具有不可变性,而且是用圆括号访问的. 索引(下表索引或者键索引都是用的中括号) #!/usr/bin/python # 这节来学习元组, tuple, 基本上就像一个不可以 ...
- linux SPI驱动——spidev之driver(六)
一: spidev_init注册spidev 1: static int __init spidev_init(void) 2: { 3: int status; 4: 5: /* Claim o ...