首页 > 其他 > 详细

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

时间:2021-04-26 15:51:50      阅读:16      评论:0      收藏:0      [点我收藏+]

没有重复的最长字符串


例子说明

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语言中串以‘\0‘结束

题目分析

  • 看到‘请找到满足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] != ‘\0‘; 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;
}

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

原文:https://www.cnblogs.com/cwhan/p/14704221.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!