LeetCode-- Longest Substring Without Repeating Characters

xiaoxiao2021-02-28  90

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.

思路:用字典数组存放下标,更新start变量。

class Solution { public: int lengthOfLongestSubstring(string s) { vector<int>dict(256,-1); int maxLen=0,start=-1; for(int i=0;i<s.size();i++) { if(dict[s[i]]>start) start=dict[s[i]]; dict[s[i]]=i; maxLen=max(maxLen,i-start); } return maxLen; } };
转载请注明原文地址: https://www.6miu.com/read-47287.html

最新回复(0)