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 ...
随机推荐
- Arc Object开发,概述2
简介 Esri公司推出的ArcGIS产品是一个非常强大的体系,里面包含ArcGIS Desktop.ArcGIS Server.ArcGIS Engine.ArcSDE.ArcGIS Online等, ...
- iOS 给三方日历加上农历
首先创建一个农历文件 LunarCalendar.h // // LunarCalendar.h // Hnair4iPhone // // Created by yingkong1987 on 13 ...
- Android为什么方法数不能超过65535
言归正传,来聊聊为什么方法数不能超过65535?搬上Dalvik工程师在SF上的回答,因为在Dalvik指令集里,调用方法的invoke-kind指令中,method reference index只 ...
- HDU1087上升子序列的最大和
解法一 此题是一个简单的动态规划问题,用dp[i]记做最后一步经过第i个数所得到的最大sum值,则结果=max(dp[i]),i=1,...n.考虑dp[i]的前一步会经过那里?假设dp[i]的前一 ...
- 微信小程序-wxs
你想在页面上使用JavaScript代码吗? 对不起,小程序不支持! 最近,一个项目就有这样的需求,我也就用上了wxs 使用方法很简单: 项目中用的是取小数点2位以及5位 具体请看官方API:WXS
- 如何使用优化代码段替代WordPress插件
每一个WordPress网站,都可以通过使用插件来获得更多的功能.但是太多的插件,会拖慢站点的运行速度,并且让站点看上去臃肿不堪. 一些插件的功能让你舍不得卸载,但是其实你可以使用简单的PHP代码来替 ...
- Vue DOM事件
本文参考自:https://mp.weixin.qq.com/s?src=3×tamp=1527154113&ver=1&signature=tWGeTa86gyK* ...
- Vmware+gdb调试Linux内核——工欲善其事,必先利其器
今天我最终忍受不了qemu的低速跟不可理喻的各种bug,開始寻找新的调试内核的方法.然后想到了Vmware,那么成熟的虚拟机怎么可能调试不了内核.于是尝试了一番,发现结果很的棒!所以立刻奋笔疾书.把这 ...
- 【DB2】DB2使用IMPORT命令导入含有自增长列的表报错处理
1.启动数据库:db2start 2.创建数据库:create db TestDB using codeset gbk territory CN collate using identity 3.连 ...
- 朴素贝叶斯分类算法-----java
1.贝叶斯分类的基础--贝叶斯定理 已知某条件概率.怎样得到两个事件交换后的概率,也就是在已知P(A|B)的情况下怎样求得P(B|A). 这里先解释什么是条件概率: 表示事件B已经发生的前提下,事件A ...