3. Longest Substring Without Repeating Characters(c++) 15ms
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.
思路1:既然是找没有重复的字符串,那么重复字符出现的地方就很重要了,于是使用容器放不重复的字符串,遇到重复的字符就清空重复字符串前的字符。执行时间29ms.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<char> vc;
int maxlen = ;
int n = s.size();
for (int i = ; i < n; i++)
{
int cc = s[i];
for (int j = ; j < vc.size(); j++)
{
if (vc[j] == cc)
{
if (maxlen < vc.size())
{
maxlen = vc.size();
}
vc.erase(vc.begin(), vc.begin()+j+);
break;
}
}
vc.push_back(cc);
}
if (maxlen < vc.size())
{
maxlen = vc.size();
}
return maxlen;
}
};
思路2:这个问题实际上是一个动态规划问题,动态规划是通过拆分问题,定义问题状态和状态之间的关系,使得问题能够以递推(或者说分治)的方式去解决。比如这一题,要找没有重复的最长字符串,首先考虑已经找到了第n个字符时最大字符串长度为L,
即S(n)=L,那么遍历第n+1个字符时,如果这个字符已经在前面重复了,可知S(n+1)=L,否则S(n+1)=L+1. 此方法时间复杂度O(n),执行时间15ms.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> vc(,-);//使用vector来记录出现的字符
int start=-,maxLen=;
for(int i=;i!=s.size();i++)
{
if(vc[s[i]]>start)
start = vc[s[i]];
vc[s[i]]=i;
maxLen = max(maxLen,i-start);
}
return maxLen;
}
};
3. Longest Substring Without Repeating Characters(c++) 15ms的更多相关文章
- 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 ...
- 【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: ...
随机推荐
- [LeetCode] Plus One 加一运算
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- C语言学习 第九次作业总结
本次作业练习的内容是二维数组.下面我先简单的说下二维数组的基本知识点: 二维数组其实这个中文概念颇有误导--会让人感觉这是一个两个维度的概念.所以很多的国外的C语言书籍上会称这种数组为多下标数组:即首 ...
- 测试对于list的sort与sorted的效率
sorted from time import clock from random import randint start = clock() a = [randint(0,1000000) for ...
- jquery-读取form表单中的所有数据列表
代码: <script> $(function() { $('#submit').click(function() { var d = {}; var t = $('form').seri ...
- ASP.NET web.config中的连接字符串
在ASP.NET的web.config中,可以用两种方式来写连接字符串的配置. <configuration> <appSettings> <add key=" ...
- C#面向对象设计模式纵横谈——5.Factory Method 工厂方法模式(创建型模式)
动机 (Motivation) 在软件系统中,经常面临着“某个对象”的创建工作; 由于需求的变化,这个对象经常面临着剧烈的变化,但是它却拥有比较稳定的接口. 如何应对这种变化?如何提供一种“封装机制” ...
- 编译OpenCV文档
概述 使用OpenCV的过程中经常查看文档,每次都去官网查看,不过国内访问速度很慢,有一份本地的文档就好了.本文列出了在Linux(Fedora)系统上从OpenCV源码编译出documentatio ...
- sql查询,不在某一范围问题的新思路
新思路: A为学生表 B为中间表(学生和课程的) C为课程表 新的思路是用left join,(right join应该也可以) 查询没有选课的学生 ... C left join B on A.si ...
- Beta版本冲刺第五天
Aruba 408 409 410 428 429 431 完成任务: 数据库对于分类新建/删除的更新 调整图片再编辑界面的合适大小 调整常驻通知栏按钮的跳转逻辑 微调数据库 立会照片: 燃尽图: c ...
- 爬虫框架--webmagic
官方有详细的使用文档:http://webmagic.io/docs/zh/ 简介:这只是个java爬虫框架,具体使用需要个人去定制,没有图片验证,不能获取js渲染的网页,但简单易用,可以通过xpat ...