[LeetCode] #3. Longest Substring Without Repeating Characters
May 1, 2021
[Medium[Question]:
Given a string s
, find the length of the longest substring without repeating characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Example 4:
Input: s = ""
Output: 0
Constraints:
0 <= s.length <= 5 * 104
s
consists of English letters, digits, symbols and spaces.
My Solution[C++]:
[Ideas]: 先講重點,取出每個字元與前一個重複字元的距離,取max !
先建立一個全為-1的vector,長度為256( ASCII 表共能表示 256 个字符),從最左邊起始位置找到最右邊,分為三個步驟。
step1.若此char上次有出現過,把Ptr_Start 更新到上次出現的位置
step2.把char對應到vector上的數字,update成目前指到的位置
step3.與上次的答案比大小,這次的答案為每個此字元到上次出現的位置距離