首页 > 编程语言 > 详细

无重复字符的最长字串(C++实现)

时间:2020-08-30 13:48:43      阅读:58      评论:0      收藏:0      [点我收藏+]

代码:

#include<iostream>
#include<unordered_set>
#include<string>
using::std::unordered_set;
using::std::string;
using::std::max;
class Solution{
public:
  unordered_set<char> slen;
  int lengthoflongestsubstring(string s)
  {
    int p=-1,len=0;
    int n = s.size();
    for(int i=0;i<n;i++)
    {
      if(i!=0)
      {
        slen.erase(s[i-1]);
      }
      while(p+1<n&&!slen.count(s[p+1]))
      {
        slen.insert(s[p+1]);
        p++;
      }
      len = max(len,p-i+1);
    }
    std::cout << len << ‘\n‘;
    return len;
}
};
int main()
{
  Solution sol;
  string s = "abcdefab";
  sol.lengthoflongestsubstring(s);
  return 0;
}
 
测试结果:
 
技术分享图片

 

 

无重复字符的最长字串(C++实现)

原文:https://www.cnblogs.com/shiheyuanfang/p/13584675.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!