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的更多相关文章

  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. 【leetcode】Longest Substring Without Repeating Characters

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

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

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

  6. leetcode: longest substring without repeating characters

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

  7. [LeetCode_3] Longest Substring Without Repeating Characters

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

  8. Longest Substring Without Repeating Characters (c#)

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

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

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

随机推荐

  1. [EF2]Sneak Preview: Persistence Ignorance and POCO in Entity Framework 4.0

    http://blogs.msdn.com/b/adonet/archive/2009/05/11/sneak-preview-persistence-ignorance-and-poco-in-en ...

  2. JavaScript模板引擎artTemplate.js——template.helper()方法

    上一篇文章我们已经讲到了helper()方法,但是上面的例子只是一个参数的写法,如果是多个参数,写法就另有区别了. <div id="user_info"></d ...

  3. 俄罗斯方块C#版

    using System; using System.Windows.Forms; using System.Drawing; using System.Media; class me : Form ...

  4. chpasswd-批量修改用户密码

    批量修改用户密码 工作原理: 从系统的标准输入读入用户的名称和口令,并利用这些信息来更新系统上已存在的用户的口令 语法: :# echo 用户名:密码 | chpasswd :# chpasswd & ...

  5. python网络编程-OSI七层模型详解

    OSI 七层模型通过七个层次化的结构模型使不同的系统不同的网络之间实现可靠的通讯,因此其最主要的功能就是帮助不同类型的主机实现数据传输 . 完成中继功能的节点通常称为中继系统.在OSI七层模型中,处于 ...

  6. C/C++头文件区别

    在从C迁移到C++时,引用的头文件经常忘记是C的还是C++特有的 1. *.h   limits.h ctype.h 2. c* climits cctype [结尾不含.h] 3. 其余的都属于C+ ...

  7. Python后台分页删除编辑查询

    「POST 数据」通常指 POST 时 body 中的数据.而 QueryString (URL)中也有可以带参数(通常是 GET 时的参数).如果 POST 时同时存在 QueryString 和 ...

  8. Git简介

    Git从入门到学会 Git简介 Git是什么? Git和SVN一样都是一种高效的管理代码的系统. Git是目前世界上最先进的分布式版本控制系统(没有之一). 创建版本库 什么是版本库呢?版本库又名仓库 ...

  9. 北京培训记day2

    后缀三姐妹 P.S.后缀大家族关系:后缀自动机fail指针=后缀树,后缀树前序遍历=后缀数组 一.后缀数组:orz罗穗骞集训队论文 给每个后缀按字典序排序 rank[]表示从i开始的后缀排名多少 sa ...

  10. Linux安装库文件(环境变量和makefile)

    CFLAGS 表示用于 C 编译器的选项,CXXFLAGS 表示用于 C++ 编译器的选项.这两个变量实际上涵盖了编译和汇编两个步骤. CFLAGS/CPPFLAGS: 指定头文件(.h文件)的路径, ...