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());
}
};

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. OKR与影响地图,别再傻傻分不清

    摘要:OKR和影响地图虽然都是为了一个目标去进行规划的方法,但是两者侧重的内容却不一致. 本文分享自华为云社区<一分钟读懂OKR与影响地图>,作者: 敏捷的小智. 什么是OKR及影响地图 ...

  2. 必备的60个常用的Linux命令

    Linux必学的60个命令Linux提供了大量的命令,利用它可以有效地完成大量的工 作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命令. ...

  3. c++ get keyboard event

    #include <string> #include <iostream> #include "windows.h" #include <conio. ...

  4. python教程-(三)使用字符串

    一.设置字符串的格式:精简版 方法1 >>> format = "Hello %s, welcome to %s" >>> values = ( ...

  5. python编程中的流程控制

    内容概要 成员运算 身份运算 流程控制 详细 1.成员运算 定义:判断某个个体在不在某个群体内 关键词:in(在) /// not in(不在) 例: num_list = [1, 2, 3, 4, ...

  6. swoole、swoft环境配置

    一.服务器环境 1.lnmp wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz -cO lnmp1.5.tar.gz && tar zxf ...

  7. Azure Virtual NetWoker(一)对等互连网络

    一,引言 Virtual NetWork Peering 可以无缝连接两个 Azure Virtual NetWork,Virtual NetWork Peering 直接的网络流量是专用的.在实际项 ...

  8. 第三课 Dubbo设计中的设计模式

    责任链模式  责任链模式在Dubbo中发挥的作用举足轻重,就像是Dubbo框架的骨架.Dubbo的调用链组织是用责任链模式串连起来的. 责任链中的每个节点实现Filter接口,然后由ProtocolF ...

  9. c++学习笔记(四)

    动态内存分配 使用new分配内存 1.指针的用处是在运行阶段分配未命名的内存以储存值. 2.在此情况下,只能通过指针来访问内存. int *ptr_int=new int;//在内存阶段为一个int值 ...

  10. pyhon-高并发测试

    使用gevent.对注册.接口进行200的并发量进行测试. #!/usr/bin/env python # -*- coding:utf-8 -*- #设置路径:Defualt Settings--- ...