这题要仔细体会下哈希表的用法,要注意的是数组本身是无序的,因此需要向左右进行扩张。

另外这个思路可以进行聚类,把连续的标记为一类。

int longestConsecutive(const vector<int> &num)
{
unordered_map<int, bool> used;
for (auto i : num)used[i] = false;
int longest = ;
for (auto i : num)
{
if (used[i])continue; int length = ;
used[i] = true;
for (int j = i + ; used.find(j) != used.end(); j++)
{
used[j] = true;
length++;
}
for (int j = i - ; used.find(j) != used.end(); j--)
{
used[j] = true;
length++;
} longest = max(longest, length); } return longest;
}

leetcode 之Longest Consecutive Sequence(六)的更多相关文章

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

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

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

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

  3. 【leetcode】Longest Consecutive Sequence

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

  4. Java for LeetCode 128 Longest Consecutive Sequence

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

  5. 【leetcode】Longest Consecutive Sequence(hard)☆

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

  6. [Leetcode][JAVA] Longest Consecutive Sequence

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

  7. leetcode 128. Longest Consecutive Sequence ----- java

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

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

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

  9. Leetcode 128. Longest Consecutive Sequence (union find)

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

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

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

随机推荐

  1. Project Euler 453 Lattice Quadrilaterals 困难的计数问题

    这是一道很综合的计数问题,对于思维的全面性,解法的过渡性,代码能力,细节处理,计数问题中的各种算法,像gcd.容斥.类欧几里德算法都有考察.在省选模拟赛中做到了这题,然而数据范围是n,m小于等于100 ...

  2. 51nod 1225 数学

    F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n).其中%表示Mod,也就是余数. 例如F(6) = 6 % 1 + 6 % 2 + 6 % 3 + ...

  3. Codeforces Round #404 (Div. 2)A B C二分

    A. Anton and Polyhedrons time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  4. POJ--2752

    原题链接:http://poj.org/problem?id=2752 分析:no! #include<cstdio> #include<cstring> #include&l ...

  5. ctsc2009 移民站选址

    分析:非常非常好的一道题! 首先需要对问题进行转化: 行列无关,对于行单独处理,对于列单独处理 必然存在一个最优方案使得每一个新站与旧站重合. 转化1很显然,对于转化2,是一类非常经典的“中位数问题” ...

  6. 杨辉三角之c实现任意行输出

    #include<stdio.h> #include<stdlib.h> int** fmalloc(int n){ int** array; //二维指针 int i; ar ...

  7. wx.showToast 延时跳转~~~

    //提交预约订单 wx.request({ url: 'http://www.pusonglin.cn/app/index.php?i=2&c=entry&do=api&op= ...

  8. sqlserver 2008备份与还原

    一.SQL数据库的备份: 1.依次打开 开始菜单 → 程序 → Microsoft SQL Server 2008 → SQL Server Management Studio → 数据库:Dside ...

  9. CentOS下安装JDK1.8

    0.卸载旧版本 键入命令java-version,查询当前JDK版本 如果版本号不是想要的,键入rpm -qa|grep gcj 键入命令 yum -y remove (后接查询得到的版本),移除老版 ...

  10. 笔记(二)TabLayout + ViewPager + FragmentPagerAdapter 组合用法

    TabLayout的xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...