class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int res = ;
unordered_map<int,int> m;
for(int i=;i < nums.size();i++){
if(m.count(nums[i])) continue;
int left = (m.count(nums[i]-) > ? m[nums[i]-]:);
int right = (m.count(nums[i]+) > ? m[nums[i]+]:);
int sum = left + right + ;
m[nums[i]] = sum; // 为什么要加这个,不是改变两端么
res = max(res,sum);
m[nums[i]-left] = sum;
m[nums[i]+right] = sum;
}
return res;
}
};

还有bug,太晚了不想想了,下次再补

Leetcode 128 *的更多相关文章

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

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

  2. Java for LeetCode 128 Longest Consecutive Sequence

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

  3. 图解leetcode —— 128. 最长连续序列

    前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). ...

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

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

  5. Java实现 LeetCode 128 最长连续序列

    128. 最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连 ...

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

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

  7. Leetcode#128 Longest Consecutive Sequence

    原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...

  8. [LeetCode#128]Word Ladder II

    Problem: Given two words (start and end), and a dictionary, find all shortest transformation sequenc ...

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

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

随机推荐

  1. 案例1:写一个压缩字符串的方法,例如aaaabbcxxx,则输出a4b2c1x3。

    public static String zipString(String str){ String result = "";//用于拼接新串的变量 char last = str ...

  2. C++ getline判断空行

    C++中getline用于逐行读取字符, 格式 getline(字符串,字符数) 将该行“字符数”个的字符读入“字符串” 如何判断所读是否为空行呢? strlen(字符串)==0就是空行

  3. gawk命令详解

    GNU awk: sort.cut.uniq.wc等参考: https://blog.csdn.net/lk07828/article/details/46324807 https://blog.cs ...

  4. Node前后端分离基本概括

    首先从一个重要的概念“模板”说起. 广义上来说,web中的模板就是填充数据后可以生成文件的页面. 严格意义上来说,应该是模板引擎利用特定格式的文件和所提供的数据编译生成页面.模板大致分为前端模板(如e ...

  5. 基于 Python 和 Pandas 的数据分析(5) --- Concatenating and Appending

    这一节我们将会介绍几种不同的合并数据的方法. 在我们这个不动产投资的例子中, 我们希望获取 51 个州的房产数据, 并把它们组合起来. 我们这样做有很多原因. 这样做既便于我们做分析, 同时也可以占用 ...

  6. re模块与subprocess模块介绍

    一:re模块       处理正则表达式的模块,正则表达式就是一些带有特殊含义的符号或者符号的组合. 作用:对字符串进行过滤,在一堆字符串中找到你所关心的内容,你就需要告诉计算机你的过滤的 规则是什么 ...

  7. P1230 智力大冲浪

    题目描述 小伟报名参加中央电视台的智力大冲浪节目.本次挑战赛吸引了众多参赛者,主持人为了表彰大家的勇气,先奖励每个参赛者m元.先不要太高兴!因为这些钱还不一定都是你的?!接下来主持人宣布了比赛规则: ...

  8. Java获得日期相差的天数

    文章来源: http://www.jb51.net/article/75551.htm 这篇文章主要介绍了java计算两个时间相差天数的方法,感兴趣的小伙伴们可以参考一下: 问题描述: 输入:两个日期 ...

  9. 手工生成AWR报告方法记录

    AWR(Automatic Workload Repository)报告是我们进行日常数据库性能评定.问题SQL发现的重要手段.熟练掌握AWR报告,是做好开发.运维DBA工作的重要基本功. AWR报告 ...

  10. (转)c#反射

    1. 什么是反射2. 命名空间与装配件的关系3. 运行期得到类型信息有什么用4. 如何使用反射获取类型5. 如何根据类型来动态创建对象6. 如何获取方法以及动态调用方法7. 动态创建委托 1.什么是反 ...