Leecode-没有重复的最长字符串

没有重复的最长字符串


例子说明

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

Example 4:

Input: s = ""
Output: 0


Constraints:

    0 <= s.length <= 5 * 104
    s consists of English letters, digits, symbols and spaces.

知识点

  • c语言中串以''结束

题目分析

  • 看到‘请找到满足xx的最x的区间(子串、子数组)的xx’这种题目,我想到用滑动窗口来解决。如果不了解滑动窗口原理,建议先去了解!
/**
index[128]保存的是该字符的下一个坐标值
start表示滑动窗口的开始坐标
*/
int lengthOfLongestSubstring(char * s){
    int i, count = 0, max = 0, index[128] = {0}, start = 0;
    for(i=0; s[i] != ''; i++)     
    {
        if(index[s[i]] > start)   
        {                    
            //表示当前窗口最大值 
            count = i - start; 
            if(count > max)
            {
                max = count;
            }
            start = index[s[i]];
        }
        index[s[i]] = i + 1;
    }
    //当为空串或者串中没有重复的字符的情况
    count = i - start; 
    return count > max? count : max;
}
原文地址:https://www.cnblogs.com/cwhan/p/14704221.html