[Leetcode] Longest Substring Without Repeating Characters (C++)

题目:

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.

Tag:

String; Hash Table; Two Pointers

体会:

这道题对我来说挺难,看了很多答案,还是不太会。。。现在也只是知道了个大概意思。

大致的意思就是,首先我们要有一个Map, 其中<Key, Value>=<char, char在string s 中最后一次出现的位置>。然后开始逐个检查string中的char。

假设string s = "cxyzabdecb", 现在我们的substring是"abd",那么有两种情况我们可以直接增加当前substring的长度,一个是现在的char是第一次出现,(比如出现了e,那么就可以变成abde);另一个是虽然这个char在先前出现过,但是并没有在当前substring中出现过,比如abde后面的那个c, 判断条件是 map[s[i]] < i - curLen,意思就是前面那个c出现在abdec这个范围之外。综上,两种情况的共同特点都是char要在当前的substring中没有出现过。

除了上面两种情况,那么就是char在现在的substring中出现过了,那我们的substring就只能保留后来的这个部分了,即从上一次这个char后面的那一位到当前位置。例子:abdec现在往后检查遇到了b,为了能把b加进来,就只能保留dec,变成decb了。

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         // map<char, last index that char appears in the string
 5         map<char, int> indexes;
 6         // length of current substring
 7         int curLen = 0;
 8         // length of max substring length so far
 9         int maxLen = 0;
10         int n = s.length();
11
12         for (int i = 0; i < n; i++) {
13             char ch = s[i];
14             // if this char appears the first time or
15             // is not part of the current substring
16             if ((indexes.count(s[i]) == 0) ||
17             (curLen < i - indexes[s[i]])) {
18                 curLen++;
19             } else {
20                 curLen = (i - indexes[s[i]]);
21             }
22             // update
23             indexes[s[i]] = i;
24             maxLen = (curLen > maxLen) ? curLen : maxLen;
25         }
26         return maxLen;
27     }
28 };
时间: 2024-11-12 09:57:17

[Leetcode] Longest Substring Without Repeating Characters (C++)的相关文章

LeetCode: 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 subst

LeetCode——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 subst

leetcode——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

LeetCode: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

[LeetCode] Longest Substring Without Repeating Characters [15]

题目 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 su

[LeetCode] 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 subst

[LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++语言 java语言实现

Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explana

LeetCode Longest Substring Without Repeating Characters 最长不重复子串

题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离是多少,若大于max,则更新max.若小于,则不更新.每扫到一个字符就需要更新他的出现位置了.这里边还有个注意点,举例说明: 假如有长为16串 s="arbtbqwecpoiuyca" 当扫到第2个b时,距离上一个b的距离是2:(直接减) 当扫到第2个c时,距离上一个c的距离是6:(直接减

leetcode : Longest Substring Without Repeating Characters 解题报告

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.