程式語言 - LeetCode - C - 3. Longest Substring Without Repeating Characters



參考資訊:
https://www.cnblogs.com/grandyang/p/4480780.html

題目:


解答:

#define max(_x_, _y_) _x_ > _y_ ? _x_ : _y_;

int lengthOfLongestSubstring(char *s)
{
    int r = 0;
    int c0 = 0;
    int left = -1;
    int hash[128] = {0};
    int len = strlen(s);

    for (c0 = 0; c0 < 128; c0++) {
        hash[c0] = -1;
    }

    for (c0 = 0; c0 < len; c0++) {
         left = max(left, hash[s[c0]]);
         hash[s[c0]] = c0;
         r = max(r, (c0 - left));
    }
    return r;
}