• Longest Substring Without Repeating Characters


    题目三: Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters.

    For example, the longest substring without repeating letters for "abcabcbb" is "abc",

    which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

    题目大意:

       给出一个字符串s,找出字符串中最长的且没有重复的字符串的长度。比如: "abcabcbb", 这个最长且没有重复的字符串就是"abc",那么应该返回3

    解决的思路:

    1.通过建立字典,保存每个字符的位置

    2.求当前不重复的字符串的长度,比较求出最大

     1 int lengthOfLongestSubstring(string s) {
     2     if (s.size() == 0)
     3         return 0;
     4     map<int, int> dict;
     5 
     6     int maxLen = 0;
     7     int len = 0;
     8 
     9     for (int i = 0; i < s.size(); i++) {
    10         len++;
    11         //如果是重复的字符,且在当前字符串中,则计算出当前字符串的长度(不包含该字符)
    12         if (dict.count(s[i]) && len > (i - dict[s[i]]))
    13             len = i - dict[s[i]];
    14         maxLen = max(maxLen, len);
    15         dict[s[i]] = i;
    16     }
    17     return maxLen;
    18 }
  • 相关阅读:
    c#冒泡排序
    C# 虚方法(virtual)覆盖(override) 隐藏(new) 重载
    Javascript 大括号
    C# const.static.readonly.
    热点链接(img map area)
    WeiBo返回错误码的二种方式
    Cookie跨域操作
    synchronized(this)与synchronized(class)
    线程安全场景备忘
    git新建一个分支setupstream
  • 原文地址:https://www.cnblogs.com/the-game-over/p/4596454.html
Copyright © 2020-2023  润新知