旅鸟

导航

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;
    }
}

 

posted on 2018-08-01 15:41  旅鸟  阅读(107)  评论(0编辑  收藏  举报