My 4ms c solution


  • 0
    W
    int lengthOfLongestSubstring(char* s) {
        if (s[0] == 0) return 0;
    	int start = 0;
    	int end = 0;
    	int ans = 0;
    	int i = 0;
    	int *hm = (int *)malloc(sizeof(int) * 128);
    	while (s[i] != 0) {
    		hm[s[i++]] = -1;
    	}
    	hm[s[start]] = start;
    	while (true) {
    		if (s[end + 1] == 0) {
    			if (end - start + 1 > ans) ans = end - start + 1;
    			break;
    		}
    		if (hm[s[end + 1]] >= start && hm[s[end + 1]] <= end) {
    			if (end - start + 1 > ans) ans = end - start + 1;
    			start = hm[s[end + 1]] + 1;
    			end++;
    			hm[s[end]] = end;
    			continue;
    		}
    		end++;
    		hm[s[end]] = end;
    	}
    	return ans;
    }

Log in to reply
 

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