这个一个比较经典的考题,恰巧我在面试中也遇到了,现在把它写下来记录一下,原题的描述是这样的,下面是 LeetCode 的原文:
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.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
解题思路:
HashMap
基本思路:创建一个 HashMap,字符串中的字符作为 key 值,相应的其出现的位置作为 value 值;创建两个指针 i 和 j ,这两个指针决定了最大子串的长度,i 是左指针,j 是右指针;移动右指针去扫描字符串,同时更新 hashmap,如果这个字符已经出现过(已经在 hashmap 中),那么将左指针移动到同一个字符最后一次出现的地方,也就是右指针当前的位置。
|
|
Set
|
|
HashSet
|
|
Array
|
|