• 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;
    }
    
  • 相关阅读:
    iOS URL中汉字的编码和解码
    指针函数和函数指针
    面试题1:赋值运算符函数
    线程安全的单实例模式
    大数相加
    网格走法
    stringstream字符串流的妙用
    判断一棵二叉树是否为二叉排序树
    idea 从javadoc中复制内容出来
    idea开启jquery提示及如何找到学习目标
  • 原文地址:https://www.cnblogs.com/cwhan/p/14704221.html
Copyright © 2020-2023  润新知