LeetCode: Longest Consecutive Sequence
想到map了,可惜没想到用erase来节省空间,看了网上答案
1 class Solution { 2 public: 3 int longestConsecutive(vector<int> &num) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 map<int, int> rec; 7 for (int i = 0; i < num.size(); i++) rec.insert(pair<int, int>(num[i], 1)); 8 int maxcon = 0; 9 for (int i = 0; i < num.size(); i++) { 10 map<int, int>::iterator it = rec.find(num[i]); 11 if (it != rec.end()) { 12 int left = num[i]-1; 13 int right = num[i]+1; 14 while (1) { 15 map<int, int>::iterator iter = rec.find(left); 16 if (iter != rec.end()) { 17 left--; 18 rec.erase(iter); 19 } 20 else break; 21 } 22 left++; 23 while (1) { 24 map<int, int>::iterator iter = rec.find(right); 25 if (iter != rec. end()) { 26 right++; 27 rec.erase(iter); 28 } 29 else break; 30 } 31 right--; 32 maxcon = max(maxcon, right - left + 1); 33 rec.erase(it); 34 } 35 } 36 return maxcon; 37 } 38 };
贴上自己写的更加简单的代码
1 class Solution { 2 public: 3 int longestConsecutive(vector<int> &num) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 map<int, int> S; 7 int sum = 0; 8 for (int i = 0; i < num.size(); i++) S[num[i]] = 1; 9 for (int i = 0; i < num.size(); i++) { 10 if (S.count(num[i])) { 11 int left = 1; 12 int tmp = num[i]; 13 while (S.count(tmp-left)) { 14 S.erase(tmp-left); 15 left++; 16 } 17 int right = 1; 18 while (S.count(tmp+right)) { 19 S.erase(tmp+right); 20 right++; 21 } 22 S.erase(tmp); 23 sum = max(sum, left+right-1); 24 } 25 } 26 return sum; 27 } 28 };
C#
1 public class Solution { 2 public int LongestConsecutive(int[] nums) { 3 Dictionary<int, int> S = new Dictionary<int, int>(); 4 int ans = 0; 5 for (int i = 0; i < nums.Length; i++) { 6 if (!S.ContainsKey(nums[i])) S.Add(nums[i], 1); 7 } 8 for (int i = 0; i < nums.Length; i++) { 9 if (S.ContainsKey(nums[i])) { 10 int left = 1; 11 int tmp = nums[i]; 12 while (S.ContainsKey(tmp - left)) { 13 S.Remove(tmp - left); 14 left++; 15 } 16 int right = 1; 17 while (S.ContainsKey(tmp + right)) { 18 S.Remove(tmp + right); 19 right++; 20 } 21 S.Remove(tmp); 22 ans = Math.Max(ans, left + right - 1); 23 } 24 } 25 return ans; 26 } 27 }
View Code
转载于:https://www.cnblogs.com/yingzhongwen/archive/2013/04/02/2995933.html
LeetCode: Longest Consecutive Sequence相关推荐
- LeetCode: Longest Consecutive Sequence [128]
[题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...
- LeetCode:Longest Consecutive Sequence
题目链接 Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...
- [LeetCode] Longest Consecutive Sequence 求解
为什么80%的码农都做不了架构师?>>> 题目 Given an unsorted array of integers, find the length of the long ...
- leetcode Longest Consecutive Sequence
#include <iostream> #include <vector> #include <unordered_map>// 时间复杂度是O(n), 空间复杂度 ...
- [leetcode]Longest Consecutive Sequence
这道题目一看就去翻答案了,因为以前见过,知道并查集可以用来做分组,但这样的题目真的要并查集么?想想也是HashSet之类就能搞定么.果然是HashMap而不是HashSet.下面这个解法我比较容易理解 ...
- 298. Binary Tree Longest Consecutive Sequence
题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...
- leetcode 128. Longest Consecutive Sequence | 128. 最长连续序列(Java)
题目 https://leetcode.com/problems/longest-consecutive-sequence/ 题解 方法1:HashMap 解法,O(n^2) 如下图,假设 n=4 被 ...
- LeetCode 128. 最长连续序列(Longest Consecutive Sequence)
题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...
- 128. Longest Consecutive Sequence
Title 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [ ...
最新文章
- java让用户输入3个随机数_3-流程控制、随机数、键盘输入
- Java synchronized 和 volatile 的区别
- vue 加载时掉用mounted_Vue实例中生命周期created和mounted的区别详解
- 第七周实践项目4 队列数组
- 深入剖析ASP.NET的编译原理之二:预编译(Precompilation)
- 还不知道这 11 个超酷的编程新工具你就 out 了!
- csv文件-读取方式、字典
- 找不org.apache.commons.lang3.builder.EqualsBuilder和commons-lang下载链接
- VUE中友盟统计的使用方法
- springboot启动报错The Bean Validation API is on the classpath but no implementation could be found
- What?校花居然半夜问我要“软件”
- springboot整合tk-mybatis框架搭建
- maven阿里云中央仓库
- Zookeeper基础笔记——从小白到入门
- IE8允许ActiveX控件设置
- 用DOCKER安装及使用SSCHA
- 直播带货系统,带货直播系统中发布商品的逻辑处理流程
- 线状图斑的平均宽计算
- 计算机网络毕业实训报告书,计算机网络实训报告书
- 拓嘉辰丰:多多买菜提货点怎样获益?为什么很便宜?