[LeetCode] Longest Substring Without Repeating Characters
生活随笔
收集整理的這篇文章主要介紹了
[LeetCode] Longest Substring Without Repeating Characters
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.
解題思路
設(shè)置左右兩個指針。左右指針之間的字符沒有出現(xiàn)反復(fù),則右指針向右移動。否則左指針向右移動(直到?jīng)]有反復(fù)字符為止)。而且統(tǒng)計最大字符數(shù)。
實現(xiàn)代碼
// Rumtime: 60 ms class Solution { public:int lengthOfLongestSubstring(string s) {set<int> res;int maxLen = 0;int left = 0;int i;for (i = 0; i < s.size(); i++){if (res.find(s[i]) != res.end()){maxLen = max(maxLen, i - left);while (s[left] != s[i]){res.erase(s[left++]);}left++;}else{res.insert(s[i]);}}maxLen = max(maxLen, i - left);return maxLen;} };轉(zhuǎn)載于:https://www.cnblogs.com/blfbuaa/p/7222319.html
總結(jié)
以上是生活随笔為你收集整理的[LeetCode] Longest Substring Without Repeating Characters的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python入门系列——第2篇
- 下一篇: Educational Codeforc