My progress only 16ms, O(n)


  • 1
    O

    Here is

    int lengthOfLongestSubstring(string s) {
    	int *locat = new int[256];
    	for(int i = 0; i < 256; ++i)
    		locat[i] = -1;
    
    	int maxlen = 0;
    	string::size_type i = 0, j = 0;
    	for( ; i < s.size(); ++i)
    	{
    		if(locat[s[i]] != -1)
    		{
    			if(i - j > maxlen)
    				maxlen = i - j;
    			if(locat[s[i]] + 1 > j)
    				j = locat[s[i]] + 1;
    			locat[s[i]] = i;
    		}
    		else
    			locat[s[i]] = i;
    	}
    	if(i - j > maxlen)
    		maxlen = i - j;
    	delete [] locat;
    	return maxlen;
    }

Log in to reply
 

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