• 4、无重复字符的最长子串


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

    示例 1:

    输入: "abcabcbb"
    输出: 3
    解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。

    示例 2:
    输入: "bbbbb"
    输出: 1
    解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
    

    示例 3:

    输入: "pwwkew"
    输出: 3
    解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
         请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
     class Program
        {
            static void Main(string[] args)
            {
               var t=  ComputeString.ComputeM("bacdcab");
    
                Console.WriteLine(t);
                Console.ReadKey();
            }
        }
    
    
    
    
        class ComputeString
        {
            public static int ComputeM(string str)
            {
                int len = str.Length;
    
                HashSet<char> set = new HashSet<char>();
    
                int ans=0, start = 0, end = 0;
    
                while (start < len && end < len)
                {
                    if (!set.Contains(str[end]))
                    {
                        set.Add(str[end++]);
                        ans = Math.Max(ans, end - start);
                    }
                    else
                    {
                        set.Remove(str[start++]);
                    }
                }
    
                return ans;
            }
    
            public static int LengthOfLongestSubstring(string s)
            {
                //字符串转化为char[]数组
                var array = s.ToCharArray();
                int n = array.Length, ans = 0;
                Dictionary<char, int> map = new Dictionary<char, int>();
    
                for (int j = 0, i = 0; j < n; j++)
                {
                    if (map.ContainsKey(array[j]))
                    {
                        //map[array[j]]
                        //i(赋值号两边)
                        i = Math.Max(map[array[j]], i);
                    }
                    ans = Math.Max(ans, j - i + 1);
                    map[array[j]] = j + 1;
                }
                return ans;
            }
        }

      

  • 相关阅读:
    关于binary log那些事
    Ubuntu常用软件安装与使用
    Ubuntu 12.04系统优化清理
    Ubuntu 12.04开机自动挂载Windows分区
    wubi安装Ubuntu后扩充Ubuntu系统空间
    sudo找不到命令:修改sudo的PATH路径
    JS 获取触发事件的对象
    NOIP 2002
    NOIP 2011 聪明的质监员
    NOIP 2011 计算系数
  • 原文地址:https://www.cnblogs.com/coderblog/p/10858097.html
Copyright © 2020-2023  润新知