给定一个未排序的整数数组,找出最长连续序列的长度。
例如,
给出 [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 一个无序整数数组中找到最长连续序列的更多相关文章

  1. LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...

  2. Java算法-------无序数组中的最长连续序列---------leetcode128

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  3. [LeetCode] 128. Longest Consecutive Sequence 解题思路

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  4. [leetcode]128. Longest Consecutive Sequence最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

  5. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  6. 128. Longest Consecutive Sequence(leetcode)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. 【LeetCode】128. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  8. 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. 128. Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

随机推荐

  1. Mac OS用docker Desktop安装单节点kubernetes

    方案: 安装方式:阿里云minikube,k8s官方minikube,kubeadm, docker Desktop中自带第k8s 安装环境:在linux虚拟机中安装k8s,在macos中安装k8s, ...

  2. 简单的shell脚本编写

    http://www.cnblogs.com/wuyuegb2312/p/3399566.html

  3. HTML 客户端存储

    在客户端存储数据 HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前, ...

  4. (28)java web的hibernate使用

    Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自 ...

  5. linux shell 突破

    targetDate=$(cat maxdayid); targartMonth=${targetDate::}; targartYear=${targetDate::}; echo $targart ...

  6. confluence的使用

    搜索文档的技巧 在confluence中进行搜索的时候,也需要使用通配符.比如搜索cmscontext,需要这么搜索cmscontex*,如果搜索的话,cmscontext.geturl是会被过滤掉的 ...

  7. 关于encodeURIComponent的用法

    定义和用法 encodeURIComponent() 函数可把字符串作为 URI 组件进行编码. 语法 encodeURIComponent(URIstring) 参数  描述  URIstring  ...

  8. codeforces 673B B. Problems for Round(模拟)

    题目链接: B. Problems for Round time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. codeforces 665E E. Beautiful Subarrays(trie树)

    题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...

  10. JS截取与分割字符串常用技巧总结

    本文实例讲述了JS截取与分割字符串的常用方法.分享给大家供大家参考,具体如下: JS截取字符串可使用 substring()或者slice() 函数:substring() 定义:substring( ...