Share my 7ms C solution


  • 0
    I
    int not_repeated(char *s, int left, int right) {
        if (left == right) return -1; 
        while (left < right) {
            if (s[left]  == s[right]) {
                return left;
            }   
            left += 1;
        }   
        return -1; 
    }
    
    
    int lengthOfLongestSubstring(char* s) {
        int max = 0;
        int tmp = 0;
        int check_output = 0;
        int current = 0;
        int left = 0, right = 0;
        while (s[right] != 0 && left <= right) {
            check_output = not_repeated(s, left, right); 
            if (check_output == -1) {
                right += 1;
            } else {
                tmp = right - left;
                if (tmp > max) {
                    max = tmp;
                }   
                left = check_output + 1;
                right += 1;
            }   
        }   
        tmp = right - left;
        if (tmp > max) max = tmp;
        return max;
    }

Log in to reply
 

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