[LeetCode] Longest Substring Without Repeating Characters
生活随笔
收集整理的這篇文章主要介紹了
[LeetCode] Longest Substring Without Repeating Characters
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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.
解題思路
設置左右兩個指針。左右指針之間的字符沒有出現反復,則右指針向右移動。否則左指針向右移動(直到沒有反復字符為止)。而且統計最大字符數。
實現代碼
// 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;} };轉載于:https://www.cnblogs.com/blfbuaa/p/7222319.html
總結
以上是生活随笔為你收集整理的[LeetCode] Longest Substring Without Repeating Characters的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python入门系列——第2篇
- 下一篇: Educational Codeforc