-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path128.cpp
More file actions
32 lines (31 loc) · 786 Bytes
/
128.cpp
File metadata and controls
32 lines (31 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//
// 128.cpp
// leetcode
//
// Created by R Z on 2018/3/29.
// Copyright © 2018年 R Z. All rights reserved.
//
#include <stdio.h>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int res=0;
unordered_map<int, int> hash;
for(int n : nums){
if(hash.find(n)==hash.end()){
int left = hash.find(n-1)!=hash.end()?hash[n-1]:0;
int right = hash.find(n+1)!=hash.end()?hash[n+1]:0;
int sum=left+right+1;
res=max(res,sum);
hash[n]=sum;
hash[n-left]=sum;
hash[n+right]=sum;
}
else continue;
}
return res;
}
};