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] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  2. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  3. NoSql系列目录

    mongodb系列学习 Mongodb学习笔记一(Mongodb环境配置) Mongodb学习笔记二(Mongodb基本命令) Mongodb学习笔记三(Mongodb索引操作及性能测试) Mongo ...

  4. 微信支付(.NET版)

    前段时间做了网页版微信支付,遇到很多问题,不过最终还是解决了,现在在这里记录下开发流程以及说明,给其他人一些参考. 一.准备工作     首先肯定得先要开通微信支付功能,之前开通微信支付需要三万的押金 ...

  5. Codeforces Round #385(div 2)

    A =w= B QwQ C 题意:n个点m条边的无向图,其中有k个特殊点,你在这张图上尽可能多的连边,要求k个特殊点两两不连通,问最多能连多少边 分析:并查集 对原图做一次并查集,找出特殊点所在集合中 ...

  6. openssl+前端jsrsa签名+后端nodejs验签

    内容如标题所示,总体分为三个部分: 一.win10下安装openssl,然后通过openssl工具生成RSA的公钥和私钥 (1)win10下安装openssl需要的工具有:VS2013,Perl,na ...

  7. 单独使用Mybatis的配置文件

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC & ...

  8. HYSBZ 2038 莫队算法

    小Z的袜子(hose) Time Limit:20000MS     Memory Limit:265216KB     64bit IO Format:%lld & %llu Submit  ...

  9. Mysql触发器

    触发器(trigger)作用:监视某种情况,并触发某种操作. 触发器创建语法四要素: 1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/b ...

  10. FineUI配置文件

    <configuration> <configSections> <section name="FineUI" type="FineUI.C ...