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.

本题:中等难度

思路:动态规划

f[j]表示以j结尾的无重复子串的最大长度
f[j]={f[j−1]+1f[j−1]−num+1A[j]notinvs[j−1]A[j]invs[j−1]

其中vs[j-1]保存连续子串,num表示去掉重复字符(包含)之前的字符$$

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n = s.size();
if(n<=1)return n;
vector<vector<char> > vm(n);
vector<int> vi(n);
vi[0]=1;
vm[0].push_back(s[0]);
for(int i=1;i<n;++i)
{
vector<char>::iterator it= find(vm[i-1].begin(),vm[i-1].end(),s[i]);
if(it!=vm[i-1].end())
{
vector<char> vtmp(it+1,vm[i-1].end());
vm[i]=vtmp;
vm[i].push_back(s[i]);
vi[i]=vm[i-1].end()-it;
}
else{
vi[i]=vi[i-1]+1;
vm[i]=vm[i-1];
vm[i].push_back(s[i]);
}
}
return *max_element(vi.begin(),vi.end());
}
};

最新文章

  1. 点燃圣火! Ember.js 的初学者指南
  2. 字符串模板替换方法 MessageFormat.format
  3. I Hate It(线段数组基础题)
  4. jdbc 连接 mysql 获取 数据集 条数
  5. C# winform 最小化到电脑右下角
  6. Python 命令行非阻塞输入
  7. rpc和websocket的区别
  8. osg复制多个相同物体修改材质属性问题
  9. Linux安装JDK步骤
  10. 开启irqbalance提升服务器性能
  11. docker使用代理(测试docker 17.06)
  12. 异常 Exception 知识点总结 MD
  13. centos5&amp;6的启动过程
  14. CSS表单2 组件排版
  15. [模板]LCA
  16. 报错ORA-19809 ORA-19804
  17. auxre7使用安装
  18. CF1065E Side Transmutations
  19. Python之进度条
  20. 乱码之UTF-8 &amp;GBK

热门文章

  1. 微信小程序的实现原理
  2. RocketMQ源码详解 | Producer篇 &#183; 其一:Start,然后 Send 一条消息
  3. Linux内核漏洞精准检测如何做?SCA工具不能只在软件层面
  4. 【做题记录】CF1451E2 Bitwise Queries (Hard Version)
  5. linux ar
  6. hdu 1083 Courses(二分图最大匹配)
  7. docker 简单总结
  8. robotframework-ride快捷方式打不开
  9. Linux usb 2. 协议分析
  10. sqlalchemy 执行sql