zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    JQ插件
    jQuery radio的取值与赋值
    Js获取当前日期时间
    jquery 整理之一
    2014-9月收集整理之二(原生)
    Codeforces Round #279 (Div. 2) E-Restoring Increasing Sequence
    HDU-4431 麻将
    hdu-2222 AC自动机模板题
    Codeforces Round #460 (Div. 2) E. Congruence Equation
    Codeforces Round #459 (Div. 2) D. MADMAX
  • 原文地址:https://www.cnblogs.com/lvniao/p/9401613.html
Copyright © 2011-2022 走看看