leetcode 较难题

1.Longest Consecutive Sequence

自动插入排序

class Solution {  
public:  
  int longestConsecutive(vector<int> &num)
  {  
       map<int,int>hmap;
       hmap.clear();  
       int n = num.size();  
       for(int i=0; i<n; i++)
       {  
           hmap[num[i]]=1;
       }  
       int ans = 1; 
       int max=1;
       int pre_k = 0;  
       int pre_v = 0;        
       map<int, int>::iterator it;  
       for(it = hmap.begin(); it != hmap.end(); it++)  
       {  
          if(it!=hmap.begin())
          {
              if(it->first == pre_k + 1)
              {     
                ans++;
             }
             else ans=1;
          }
           pre_k = it->first;  
           pre_v = it->second; 
           max=max>ans?max:ans;
       }  
      return max;  
  }  
};  
原文地址:https://www.cnblogs.com/tgkx1054/p/3076858.html