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. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. D3D三层Texture纹理经像素着色器实现渲染YUV420P

    简单记录一下这两天用Texture实现渲染YUV420P的一些要点. 在视频播放的过程中,有的时候解码出来的数据是YUV420P的.表面(surface)通过设置参数是可以渲染YUV420P的,但Te ...

  3. Eclipse开发环境JDK版本问题和校验问题

    今天遇到的两个问题: 1.启动程序报错:Unsupported major.minor version 52.0 这是JDK版本过低的问题,统一一下Build Path和java Complie中的版 ...

  4. sstream

    sstream用法 #include<iostream> #include<sstream> #include<string> using namespace st ...

  5. Mysql的连接状态

    对应mysql的连接,任何时刻都有一个状态.表示mysql当前正在做什么. command里面的状态: sleep:线程正在等待客户发送新的请求. query:正在执行查询或者正在将结果发送客户端 这 ...

  6. 【转】微软教学:三种方法屏蔽Win7/Win8.1升级Win10推送

    原文地址:http://www.ithome.com/html/win10/199961.htm 微软在2015年6月就开启了Win10升级推送工作,主要是靠<获取Windows10>工具 ...

  7. Redis集群(九):Redis Sharding集群Redis节点主从切换后客户端自动重新连接

    上文介绍了Redis Sharding集群的使用,点击阅读 本文介绍当某个Redis节点的Master节点发生问题,发生主从切换时,Jedis怎样自动重连新的Master节点 ​一.步骤如下: 1.配 ...

  8. Xcode 运行程序,左侧memory 不显示内存

    运行程序后,xcode 不显示当前使用的内存情况,问题是打开了僵尸--enable zoombie Objects,关闭即可 打开 product--->SCheme-->EditSChe ...

  9. django models进行数据库增删查改

    在cmd 上运行 python manage.py shell   引入models的定义 from app.models import  myclass   ##先打这一行    ------这些是 ...

  10. UICollectionViewCell 网格显示数据

    using System; using System.Collections.Generic; using Foundation; using UIKit; namespace ddd { publi ...