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

For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

Your algorithm should run in O(n) complexity.

題解:
因為整數的范圍非常大,而且有負數,所以用map來標記和查找元素。
無序找連續元素,可以從某一已有元素開始,遞增,遞減的查找和它連續的元素是否存在,找過的元素及時從map中刪除。
本來想用hash_map,但是沒有這個庫。。。所以用了map
class Solution {
public:
    
int longestConsecutive(vector<int> &num) {
        
// Start typing your C/C++ solution below
        
// DO NOT write int main() function
        map<intint> hmap;
        hmap.clear();
        
int n = num.size();
        
for(int i=0; i<n; i++){
            hmap.insert(pair
<intint>(num[i], i));
        }
        
int ans=0, cnt=0;
        map
<intint>::iterator it;
        
for(int i=0; i<num.size(); i++){
            
int cur = num[i];
            it 
= hmap.find(num[i]);
            cnt
++;
            
if(it!=hmap.end()){
                map
<intint>::iterator iter;
                
while(1){
                    iter 
= hmap.find(++cur);
                    
if(iter==hmap.end())
                        
break;
                    cnt
++;    
                    hmap.erase(iter);
                }
                cur
=num[i];
                
while(1){
                    iter 
= hmap.find(--cur);
                    
if(iter==hmap.end())
                        
break;
                    cnt
++;    
                    hmap.erase(iter);
                }
                
if(ans<cnt)
                    ans 
= cnt;           
                cnt
=0;
            }
            cnt
=0;
        }
        
return ans;
    }
};