• 0003 无重复字符的最长子串


    给定一个字符串,找出不含有重复字符的最长子串的长度。

    示例:

    给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3。

    给定 "bbbbb" ,最长的子串就是 "b" ,长度是1。

    给定 "pwwkew" ,最长子串是 "wke" ,长度是3。请注意答案必须是一个子串,"pwke" 是 子序列  而不是子串。

    public class Solution {
        public int LengthOfLongestSubstring(string s) {
            List<int> nexts = new List<int>();
            for(int i=0; i<s.Length; i++)
            {
                int j = i + 1;
                for(; j<s.Length; j++)
                {
                    if(s[j] == s[i])
                    {
                        nexts.Add(j);
                        break;
                    }
                }
                if(j == s.Length)
                {
                    nexts.Add(j);
                }
            }
            List<Section> sections = new List<Section>();
            for(int i=0; i<s.Length; i++)
            {
                Section section = new Section();
                section.start = i;
                section.end = i;
                bool flag = true;
                if (nexts[i] == s.Length)
                    {
                        if(i == s.Length-1)
                        {
                            section.end = s.Length-1;
                            flag = false;
                        }
                        else
                        {
                            int min = nexts[i + 1];
                            int j = i + 2;
                            for (j = i + 2; j < s.Length; j++)
                            {
                                if (nexts[j] < min)
                                {
                                    min = nexts[j];
                                }
                            }
                            section.end = min - 1;
                            flag = false;
                        }
                        
                    }
                    else
                    {
                        int min = nexts[i + 1];
                        int j = i + 2;
                        for (j = i + 2; j < nexts[i]; j++)
                        {
                            if (nexts[j] < min)
                            {
                                min = nexts[j];
                            }
                        }
                        if(min > nexts[i])
                        {
                            section.end = nexts[i] - 1;
                        }
                        else
                        {
                            section.end = min - 1;
                        }
                        
                        flag = false;
    
                    }
                if(flag == true)
                {
                    section.end = nexts[i] - 1;
                }
                sections.Add(section);
            }
    
            if(sections.Count <= 0){
                return 0;
            }
            int max = sections[0].end - sections[0].start + 1, pos = 0;
            for(int i=1; i<sections.Count; i++)
            {
                int length = sections[i].end - sections[i].start + 1;
                if(length > max)
                {
                    max = length; pos = i;
                }
            }
            return max;    
        }
        class Section
        {
            public int start;
            public int end;
        }
    }
  • 相关阅读:
    Holiday、Vacation、Days off、Leave、Break
    python3对urllib和urllib2进行了重构
    python解析json
    NeuHub图像垃圾分类api和百度图像识别api
    base64加密与解密
    wafer2的keng
    HTTP 中 GET 与 POST 的区别
    如何禁用Antimalware Service Executable
    通过SimpleHTTPServer实现树莓派与主机传输文件
    电脑通过网线连接树莓派
  • 原文地址:https://www.cnblogs.com/lvniao/p/9401613.html
Copyright © 2020-2023  润新知