Description
LINK: https://leetcode.com/problems/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.
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.
Solution
1 | static int _ = [](){ |
Discussion
采用hashmap记录每个字符最后出现的位置。
采用滑窗法,每次向右滑动一位,判断有没有出现过。如果出现过的话,就把滑窗的前端移动到该字符上一次出现的位置(查找hashmap)或者不滑动。
假如已知字符串中的字符都是ascii字符的话,还可以直接采用数组作为hashmap。
一般来说,涉及到子串的算法,大多采用滑窗法。