Using unordered_map~~


  • 0
    H
    int longestConsecutive(vector<int>& nums){
        	unordered_map<int,int> m;
        	int i,j,temp;
        	for(i=0;i<nums.size();i++)
        	{
        		temp=nums[i];
        		m[temp]=i+1;
        	}
        	int size=nums.size();
        	int longest=0,templen;
        	for(i=0;i<size;i++)
        	{
        		temp=nums[i]-1;
        		if(m[temp]==0)
        		{
        		    templen=1;
        		    temp=nums[i]+1;
        		    while(m[temp]!=0){
        		        templen++;
        		        temp++;
        		    }
        		    if(templen>longest)
        		      longest=templen;
        		}
        	}
        	return longest;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.