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 Explanation: The answer is "b", with the length of 1.
问题很清晰易理解,就是找到连续的不存在重复字符的子串。
思路:遍历string的每个字符,判断新字符是否在之前出现过(这个用unordered_map数据结构很好实现),如果出现过,那么需要更新冲突的位置,以便计算包含新字符的最大不重复子串;不管新字符有没有出现过,都要更新该字符对应的下标志值。最后就是综合结果,对于包含新字符最大不重复子串和不包含新字符最大不重复子串取最大值。
附加代码: