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.

問題很清晰易理解,就是找到連續的不存在重複字符的子串。

思路:遍歷string的每個字符,判斷新字符是否在之前出現過(這個用unordered_map數據結構很好實現),如果出現過,那麼需要更新衝突的位置,以便計算包含新字符的最大不重複子串;不管新字符有沒有出現過,都要更新該字符對應的下標誌值。最後就是綜合結果,對於包含新字符最大不重複子串和不包含新字符最大不重複子串取最大值。

附加代碼:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章