leetcode 之Longest Consecutive Sequence(六)

这题要仔细体会下哈希表的用法,要注意的是数组本身是无序的,因此需要向左右进行扩张。
另外这个思路可以进行聚类,把连续的标记为一类。
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(六)的更多相关文章
- [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】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [Leetcode][JAVA] 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 ...
随机推荐
- Java导出数据行写入到Excel表格:基于Apache POI
Java导出数据行写入到Excel表格:基于Apache POI import java.io.File; import java.io.FileOutputStream; import org.ap ...
- Closest Number in Sorted Array
Given a target number and an integer array A sorted in ascending order, find the index i in A such t ...
- Classical Binary Search
Find any position of a target number in a sorted array. Return -1 if target does not exist. 与题目 Firs ...
- 自动化测试常用断言的使用方法(python)
自动化测试中寻找元素并进行操作,如果在元素好找的情况下,相信大家都可以较熟练地编写用例脚本了,但光进行操作可能还不够,有时候也需要对预期结果进行判断. 这里介绍几个常用断言的使用方法,可以一定程度上帮 ...
- 【bzoj4571】美味
Portal -->bzoj4571 Solution emmm持续智力康复.. 虽然说因为统计的是加上\(x\)的跟\(b\)异或的最大值所以可持久化trie用不了了 但是按位贪心的思想还 ...
- Java配置jdk图文教程
1.计算机 ==>右键属性 2.高级系统设置 3.环境变量 4.找path变量 5.找到jdk的bin目录并复制路径到path环境变量里(jdk5.0以后就可以只配path路径了). 6.pat ...
- C++调用函数模仿数字钟表
调用系统函数,可以得到系统时间 #include<iostream> #include<windows.h> #include<time.h> using name ...
- ubuntu 安装微信开发者工具
https://github.com/cytle/wechat_web_devtools 实测:64位 32位的可以... 参考命令; 2030 sudo apt-get install wine1 ...
- scp 从本地往线上传文件
scp /home/wwwroot/default/tf_ment.sql root@IP:/home/wwwroot/default/
- uniqid()
uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID.