128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
给定一个未排序的整数数组,找出最长连续序列的长度。
例如,
给出 [100, 4, 200, 1, 3, 2],
这个最长的连续序列是 [1, 2, 3, 4]。返回所求长度: 4。
要求你的算法复杂度为 O(n)。
详见:https://leetcode.com/problems/longest-consecutive-sequence/description/
Java实现:
方法一:
- class Solution {
- public int longestConsecutive(int[] nums) {
- int res=0;
- Set<Integer> s=new HashSet<Integer>();
- for(int num:nums){
- s.add(num);
- }
- for(int num:nums){
- if(s.remove(num)){
- int pre=num-1;
- int next=num+1;
- while(s.remove(pre)){
- --pre;
- }
- while(s.remove(next)){
- ++next;
- }
- res=Math.max(res,next-pre-1);
- }
- }
- return res;
- }
- }
方法二:
- class Solution {
- public int longestConsecutive(int[] nums) {
- int res = 0;
- Map<Integer, Integer> m = new HashMap<Integer, Integer>();
- for (int num : nums) {
- if (!m.containsKey(num)){
- int pre = m.containsKey(num - 1) ? m.get(num - 1) : 0;
- int next = m.containsKey(num + 1) ? m.get(num + 1) : 0;
- int sum = pre + next + 1;
- m.put(num, sum);
- res = Math.max(res, sum);
- m.put(num - pre, sum);
- m.put(num + next, sum);
- }
- }
- return res;
- }
- }
参考:https://www.cnblogs.com/grandyang/p/4276225.html
128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列的更多相关文章
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
- Java算法-------无序数组中的最长连续序列---------leetcode128
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最长连续序列
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. F ...
- 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 ...
- 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 128. Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- iOS开发——高级篇——多线程的安全隐患
资源共享 1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源 比如多个线程访问同一个对象.同一个变量.同一个文件 当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题 一.解 ...
- HDU 6096 String 排序 + 线段树 + 扫描线
String Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Problem De ...
- yum报错File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:
原因:学python的时候,把centos7自带的python2.7改成了python3.6.2.而yum使用的是python2,所以会出现yum报错. 解决方法: 在文件/usr/bin/yum./ ...
- luogu3379 【模板】最近公共祖先(LCA) 倍增法
题目大意:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 整体步骤:1.使两个点深度相同:2.使两个点相同. 这两个步骤都可用倍增法进行优化.定义每个节点的Elder[i]为该节点的2^k( ...
- mysql优化----大数据下的分页,延迟关联,索引与排序的关系,重复索引与冗余索引,索引碎片与维护
理想的索引,高效的索引建立考虑: :查询频繁度(哪几个字段经常查询就加上索引) :区分度要高 :索引长度要小 : 索引尽量能覆盖常用查询字段(如果把所有的列都加上索引,那么索引就会变得很大) : 索引 ...
- gson如何转化json数组
String.JsonObject.JavaBean 互相转换 User user = new Gson().fromJson(jsonObject, User.class); User user = ...
- UVA11613 Acme Corporation —— 最小费用流(流量不固定的最小费用流)
题目链接:https://vjudge.net/problem/UVA-11613 题意: 商品X在第i个月内:生产一件需要花费mi元,最多可生产ni件,销售一件(在这个月内销售,而不管它是在那个月生 ...
- Oracle:sequence问题研究
一直以来,以为sequence是不间断地持续增长的:但今天发现sequence是会跳号,这种情况发生在RAC环境下.在单实例环境下,应该不存在的. sequence截图如下: 数据库表中发生了跳号: ...
- 单片机和Linux都想学_换个两全的方法学习单片机
本节教你如何学习单片机,如何选择合适的开发板和开发工具. 现在我们知道单片机是要学习的,那么怎么去学习单片机?在上一课我们说不要使用老一套的方法学习,实际上是指的两个问题. 第一:选择什么开发板: 第 ...
- linux-3.0内核移植到fl2440开发板(以s3c2410为模板)
1.新建kernel文件夹,用于存放内核文件 [weishusheng@localhost ~]$ mkdir kernel 2.进入kernel,上传压并解压压缩文件 [weishusheng@lo ...