Leetcode:Longest Substring Without Repeating Characters

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.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: 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.


方法一,暴力穷举法,不多说了。

方法二,使用HashSet维护一个字符串中的滑动窗口,在最多O(2n)的时间内便利完。

方法三,使用HashMap保存字符对应下标, 使得左边窗口边界可以快速滑动。



代码地址:https://github.com/chy996633/leetcode/blob/master/src/LongestSubstringWithoutRepeatingCharacters.java




posted @ 2018-12-22 11:52  andrew-chen  阅读(118)  评论(0编辑  收藏  举报