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 ...
随机推荐
- mysql null与not null
http://blog.csdn.net/fwkjdaghappy1/article/details/7703974/ NULL表示 值 可为NULL,并非空的意思, NOT NULL表示 值不能为N ...
- win7 32位用pyinstaller打包Python和相关html文件 成exe
http://tieba.baidu.com/p/3060401749?traceid= 安装 pyinstaller 然后 第一步你的脚本里面要做相应处理,添加一个函数:def resource_p ...
- Android HAL实例解析
一.概述 本文希望通过分析台湾的Jollen的mokoid 工程代码,和在s5pc100平台上实现过程种遇到的问题,解析Andorid HAL的开发方法. 二.HAL介绍 现有HAL架构由Patric ...
- grep 精确匹配
使用grep实现精确过滤的五种方法 (1)当被过滤的内容占据一行时 [root@MySQL scripts]# cat oldboy.log 200 0200 2000 [root@My ...
- java查看工具jstack-windows
Prints Java thread stack traces for a Java process, core file, or remote debug server. This command ...
- vuex mapState使用
<template> <div> {{count}} <button @click="handleIncrease">+5</button ...
- ORACLE数据库导表
今天在公司的server上面装一个系统,在数据库导表的时候一直导不进去,原先是10g的.dmp文件,导入11g.怀疑版本号不兼容,后来把.dmp表打开,把里面的版本号号改为11g,发现导入还是不行.i ...
- 翻翻git之---一个丰富的通知工具类 NotifyUtil
转载请注明出处王亟亟的大牛之路 P1(废话板块.今天还加了个小广告) 昨天出去浪,到家把麦麦当当放出来玩一会就整到了12点多..早上睡过头了. .简直心酸. ... 近期手头上有一些职位能够操作,然后 ...
- js函数的Json写法
https://zhidao.baidu.com/question/83401454.html
- hdfs笔记
Distributed File System 数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来管理多台机器上的文 ...