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表示去掉重复字符(包含)之前的字符$$

  1. class Solution {
  2. public:
  3. int lengthOfLongestSubstring(string s) {
  4. int n = s.size();
  5. if(n<=1)return n;
  6. vector<vector<char> > vm(n);
  7. vector<int> vi(n);
  8. vi[0]=1;
  9. vm[0].push_back(s[0]);
  10. for(int i=1;i<n;++i)
  11. {
  12. vector<char>::iterator it= find(vm[i-1].begin(),vm[i-1].end(),s[i]);
  13. if(it!=vm[i-1].end())
  14. {
  15. vector<char> vtmp(it+1,vm[i-1].end());
  16. vm[i]=vtmp;
  17. vm[i].push_back(s[i]);
  18. vi[i]=vm[i-1].end()-it;
  19. }
  20. else{
  21. vi[i]=vi[i-1]+1;
  22. vm[i]=vm[i-1];
  23. vm[i].push_back(s[i]);
  24. }
  25. }
  26. return *max_element(vi.begin(),vi.end());
  27. }
  28. };

23-Longest Substring Without Repeating Characters的更多相关文章

  1. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  4. 3. Longest Substring Without Repeating Characters(c++) 15ms

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  5. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  6. Longest Substring Without Repeating Characters(C语言实现)

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  7. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  8. [LeetCode_3] Longest Substring Without Repeating Characters

    LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...

  9. Longest Substring Without Repeating Characters (c#)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  10. Longest Substring Without Repeating Characters(Difficulty: Medium)

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

随机推荐

  1. 《基于SIR的路边违停行为传播模型研究》

    My Focus: 路边违停 行为的传播模型; 学习基于SIR XXX模型的可行性分析.建立和结论分析 Author: 左忠义,王英英,包蕴 Mind Map:

  2. [源码解析] Pytorch 如何实现后向传播 (3)---- 引擎动态逻辑

    [源码解析] Pytorch 如何实现后向传播 (3)---- 引擎动态逻辑 目录 [源码解析] Pytorch 如何实现后向传播 (3)---- 引擎动态逻辑 0x00 摘要 0x01 前文回顾 0 ...

  3. # Host xx.xxx.x.xxx found: line 1 /root/.ssh/known_hosts updated. Original contents retained as /root/.ssh/known_hosts.old

    一直可以ssh登录远程服务器,突然不行了. 原因:远程服务器最近打过安全补丁,安全标识已经更新. 清理本机的安全密匙即可 解决办法: #ssh-keygen -R "需要远程服务器ip地址& ...

  4. Kali安装Parallels Tools过程记录

    最近两天又参加了公司一年一度的网络安全劳动竞赛,之前用过的一个 Kali 忘记密码进不去了 -_- .重新安装了 Kali 2021.3a 之后发现 Parallels Tools 安装失败,记录了一 ...

  5. k8s入坑之路(13)kubernetes重要资源(namespace隔离 resources资源管理 label)

    Namespace --- 集群的共享与隔离 语言中namespace概念 namespace核心作用隔离 以上是隔离的代码.namespace隔离的是: 1.资源对象的隔离:Service.Depl ...

  6. 某企业桌面虚拟化项目-Citrix虚拟桌面解决方案

    xxx桌面虚拟化项目Citrix解决方案 xxx桌面虚拟化项目 Citrix解决方案 1 项目背景 秉承"尊重个性.创造价值.贡献于社会"的企业理念和开拓创新的精神,xxx所制造. ...

  7. Python推导式详解,带你写出比较精简酷炫的代码

    Python推导式详解,带你写出比较精简酷炫的代码 前言 1.推导式分类与用法 1.1 列表推导 1.2 集合推导 1.3 字典推导 1.4 元组推导?不存在的 2.推导式的性能 2.1 列表推导式与 ...

  8. gcc: fatal error: limits.h: No such file or directory on macos

    重装gcc brew install gcc 软链接链到新的gcc和g++ https://stackoverflow.com/questions/56280122/gcc-fatal-error-l ...

  9. 论文解读(DeepWalk)《DeepWalk: Online Learning of Social Representations》

    一.基本信息 论文题目:<DeepWalk: Online Learning of Social Representations>发表时间:  KDD 2014论文作者:  Bryan P ...

  10. Java学习(十一)

    今天学习了this和static关键字,这两个都是c++中学过的,但讲师还是讲了2个小时... 学得东西大部分都知道吧. this是当前对象的地址,类中带有static的方法不能使用this. 类中带 ...