Leetcode 128 *
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 *的更多相关文章
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 图解leetcode —— 128. 最长连续序列
前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Java实现 LeetCode 128 最长连续序列
128. 最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连 ...
- 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
原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...
- [LeetCode#128]Word Ladder II
Problem: Given two words (start and end), and a dictionary, find all shortest transformation sequenc ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
随机推荐
- Bytom矿池接入协议指南
矿机配置 https://gist.github.com/HAOYUatHZ/a47400bde4a138825faef415387b532c 固件升级 https://service.bitmain ...
- Redis-Sentinel
Redis-Sentinel是Redis官方推荐的高可用性(HA) 解决方案,Redis-sentinel本身也是一个独立运行的进程,它能监控多个master-slave集群,发现master宕机后能 ...
- Eclipse卸载插件SpringSoource-tool-suite
Eclipse卸载插件SpringSoource-tool-suite **系统环境:**Mac OS X 引子: 一直在纠结的一个问题,就是Eclipse开发java 项目在配置了InternalR ...
- springmvc异步上传图片并回调页面函数插入图片url代码示例
<tr> <td class="search_td">属性值图片值:</td> <td> <input type=" ...
- 3、python内置类型(0529)
python的内置对象类型以及支持的运算 python对象的相关术语 python程序中保存的所有数据都是围绕对象这个概念展开的 程序中存储的所有数据都是对象 每个对象都有一个身份.一个类型和一个值 ...
- hdu 5724 Chess 博弈sg+状态压缩
Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem De ...
- Tomcat日志系统详解
综合:Tomcat下相关的日志文件 Cataline引擎的日志文件,文件名catalina.日期.log Tomcat下内部代码丢出的日志,文件名localhost.日期.log(jsp页面内部错误的 ...
- Qt532.线程(_beginthread)
1.(20180928)环境:Win7x64.Qt5.3.2 MSVC2010 OpenGL.ms2010 2.测试代码: ZC:我记得 之前在 VC6.vs08 上,还要选择 使用的是哪种 运行时线 ...
- VS2010_慢
ZC:IntelliSense 一旦关闭,代码提示 也就没有了... ZC:IntelliSense 和 IntelliTrace,不是一个东西... 1.http://blog.csdn.net/c ...
- 《剑指offer》第六十二题(圆圈中最后剩下的数字)
// 面试题62:圆圈中最后剩下的数字 // 题目:0, 1, …, n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里 // 删除第m个数字.求出这个圆圈里剩下的最后一个数字. #inclu ...