最长非重复字串

来源:互联网 发布:决战武林进阶数据坐骑 编辑:程序博客网 时间:2024/05/21 03:16
/*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. */#include <iostream>#include <string>#include <set>class Solution{public:int lengthOfLongestSubstring(std::string s){if(s.size()==0)return 0;std::set<char> state;int length=0;auto begin=s.begin();auto end=s.begin();while(end!=s.end()){while(end!=s.end() && state.find(*end)==state.end()){state.insert(*end);++end;}if(end-begin>length)length=end-begin;while(begin<=end && end!=s.end()){if(*begin==*end){++begin;++end;break;}elsestate.erase(*begin++);}}return length;}};int main(int argc,char* argv[]){Solution s;std::string str("abcaadfeac");std::cout<<s.lengthOfLongestSubstring(str)<<std::endl;return 0;}//程序貌似有误

原创粉丝点击