23-Longest Substring Without Repeating Characters
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.
本题:中等难度
思路:动态规划
其中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的更多相关文章
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- Longest Substring Without Repeating Characters(C语言实现)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [LeetCode_3] Longest Substring Without Repeating Characters
LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...
- Longest Substring Without Repeating Characters (c#)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters(Difficulty: Medium)
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
随机推荐
- 认真讲说static关键字
static 关键字主要有以下四种使用场景 修饰成员变量和成员方法 静态代码块 修饰类(只能修饰内部类) 静态导包(用来导入类中的静态资源,1.5之后的新特性) 修饰成员变量和成员方法(常用) 被 s ...
- 鸿蒙轻内核M核的故障管家:Fault异常处理
摘要:本文先简单介绍下Fault异常类型,向量表及其代码,异常处理C语言程序,然后详细分析下异常处理汇编函数实现代码. 本文分享自华为云社区<鸿蒙轻内核M核源码分析系列十八 Fault异常处理& ...
- 树的子结构 牛客网 剑指Offer
树的子结构 牛客网 剑指Offer 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) # class TreeNode: # def __init_ ...
- hdu 1198 Farm Irrigation(并查集)
题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
- Python3 装逼神器---词云(wordcloud)
词云 (Word Cloud)是对文本中出现频率较高的词语给予视觉化展示的图形, 是一种常见的文本挖掘的方法. 实例: 依赖包: # pip3 install wordcloud jieba ...
- Django 实现分页功能(django 2.2.7 python 3.7.5 )
Django 自带名为 Paginator 的分页工具, 方便我们实现分页功能.本文就讲解如何使用 Paginator 实现分页功能. 一. Paginator Paginator 类的作用是将我们需 ...
- Charles--/安装/破解/支持https抓包
一.安装破解Charles 1.下载charles4.0.2版本,下面的jar包需要和charles版本对应 2.下载地址:https://www.cr173.com/soft/494576.htm ...
- python微服务
https://realpython.com/python-microservices-grpc/ https://github.com/saqibbutt/python-flask-microser ...
- JDK 之 Arrays.asList - 源码分析
Arrays工具类提供了一个方法asList, 使用该方法可以将一个变长参数或者数组转换成List . 其源代码如下: @SafeVarargs public static <T> Lis ...
- c++学习笔记(九)
引用(reference) 概念 引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字. 一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量. 用法 变量名称是变量附属在内存 ...