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. (转载)MySQl数据库-批量添加数据的两种方法

    方法一:使用excel表格 方法二:使用insert语句(FileWriter批量写入) 使用excel表格 1.打开数据表,按照表的字段在excel中添加数据.注意:表中字段名必须和excel中的名 ...

  2. CAS Client集群环境的Session问题及解决方案 不能退出登录

    casclient源代码下载链接:https://github.com/apereo/java-cas-client cas官网链接:https://www.apereo.org/projects/c ...

  3. js之鼠标随动后面跟随事件(类似于长龙跟着跑)

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  4. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) D. Office Keys time limit per test2 seconds 二分

    D. Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  5. SqlServer 中的触发器

    SqlServer 触发器实现多表之间同步增加.删除与更新 定义: 何为触发器?在SQL Server里面也就是对某一个表的一定的操作,触发某种条件,从而执行的一段程序.触发器是一个特殊的存储过程. ...

  6. JDBC连接数据库的简单介绍

    休息10天后重新看了下jdbc,开始振作继续学习(休息10天主要是因为驾照考试太累,2333),希望自己能够调整好心态,继续对程序有着一如既往的喜爱(加油) Connection con=null; ...

  7. tkinter拦截关闭事件

    import tkinter as tk from tkinter import messagebox root = tk.Tk() def on_closing(): if messagebox.a ...

  8. leecode第一百三十六题(只出现一次的数字)

    class Solution { public: int singleNumber(vector<int>& nums) { int len=nums.size(); ; ;i&l ...

  9. unbuntu安装Node.js

    在官网https://nodejs.org/en/下载 手动创建链接的话,新安装的angular的ng   typescript的tsc都要自己手动建立软链接,要不就每个工程里npm install一 ...

  10. @Scope注解设置创建bean的方式和生命周期

    1.1.1            Scope注解创建bean的方式和生命周期 作用 Scope设置对象在spring容器(IOC容器)中的生命周期,也可以理解为对象在spring容器中的创建方式. 取 ...