LeetCode 3_Longest Substring Without Repeating Characters

题目描写叙述:

Given a string, find the length of the longest substring
without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the
length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

也就是寻找字符串中的不含反复元素的最长子串的长度。

首先非常easy想到的是暴力法:外两层循环在字符串中不断的扫描子串,内两层循环用来推断子串是否是有反复字符。

显然这样的方法时间复杂度有点高为O(n^4)。基本上不能够被接受。

在上面的方法中。推断子串是否有反复的过程中能够不用用两层循环来扫描。能够使用哈希表的方法推断。代码例如以下:

<span style="font-size:18px;">class Solution {
public:
int lengthOfLongestSubstring(string s)
{
int i,j;
int maxLength = 0;
int hash[256];
int n = s.size();
for(i = 0; i < n; ++i)
{
memset(hash,0,sizeof(hash));
hash[s[i]] = 1;
for(j=i+1; j<n; ++j)
{
if(hash[s[j]] == 0)
hash[s[j]] = 1;
else
break;
}
if(j-i > maxLength)
maxLength = j-i;
}
return maxLength;
}
};</span>

上面的方法时间复杂度为O(n^2),以下给出LeetCode的參考答案,时间复杂的为O(n),并且代码很easy!真实不得不佩服大牛。。。

思路:使用i和j两个指针进行搜索,i代表候选的最长子串的开头。j代表候选的最长子串的结尾。

先如果i不动。右移j。直到j到达原字符串的结尾,此时j-i就构成了一个候选的最长子串。

每次都维护一max_length,就能够选出最长子串了。如果字符j已经反复出现过(如果在位置k),就须要停止右移了。

记录当前的候选子串并和max_length做比較。

以下就是一个非常好的处理,还真没想到。

在下一次搜寻中,i应该更新到k+1。这句话的意思是。用这个样例来理解。abcdef是个不反复的子串,abcdefc中(为了方便记录为abc1defc2),c1和c2反复了。那么下一次搜寻,应该跨过出现反复的地方进行,否则找出来的候选串依旧有反复字符,且长度还不如上次的搜索。所下面一次搜索。直接从c1的下一个字符d開始进行。也就是说,下一次搜寻中,i应该更新到k+1。

代码例如以下:

class Solution {
public:
int lengthOfLongestSubstring(string s)
{
int i = 0, j = 0;
int maxLength = 0;
bool exist[256] = {false};
int n = s.length();
while (j < n)
{
if (exist[s[j]])<span style="white-space:pre"> // 由于s[j] 中的是字符。能够自己主动转换为整型</span>
{
maxLength = max(maxLength, j - i);
while(s[i] != s[j])
{
exist[s[i]] = false;
i++;
}
i++;
j++;
}
else
{
<span style="white-space:pre"> </span>exist[s[j]] = true;
j++;
}
}
maxLength = max(maxLength, n-i);<span style="white-space:pre"> // 别忘了这一步</span>
return maxLength;
}
};

LeetCode 3_Longest Substring Without Repeating Characters的更多相关文章

  1. LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium

    题目: Given a . For . 解题思路: 这个题让找一个字符串中具有不重复单词的最长子串的长度,如:ababc,子串为abc,长度为3.有这么几个方法: 方法一: 依赖字符串本身的一些特有函 ...

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

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

  3. leetcode: longest substring without repeating characters

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

  4. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  5. C++ leetcode Longest Substring Without Repeating Characters

    要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...

  6. [LeetCode]Longest Substring Without Repeating Characters题解

    Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...

  7. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  8. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

  9. LeetCode——Longest Substring Without Repeating Characters

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

随机推荐

  1. Python基础:字典(dict)与集合(set)

    查找场景下与列表的性能对比 字典与集合之所以高效的原因是:内部结构都是一张哈希表. 平均情况下插入.查找和删除的时间复杂度为 O(1). 假设有数量100,000的产品列表: import time ...

  2. (转)UITextField

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...

  3. Educational Codeforces Round 31- D. Boxes And Balls

    D. Boxes And Balls time limit per test2 seconds memory limit per test256 megabytes 题目链接:http://codef ...

  4. jquery插件编写【转载】

    如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再赘述了,用过的都知道.今天我们来讨论下jq ...

  5. x86保护模式-七中断和异常

    x86保护模式-七中断和异常 386相比较之前的cpu   增强了中断处理能力   并且引入了 异常概念 一 80386的中断和异常 为了支持多任务和虚拟存储器等功能,386把外部中断称为中断     ...

  6. BZOJ 3110 [Zjoi2013]K大数查询 ——树套树

    [题目分析] 外层区间线段树,内层是动态开点的权值线段树. SY神犇说树套树注重的是内外层的数据结构的选择问题,果然很重要啊. 动态开点的实现方法很好. [代码] #include <cstdi ...

  7. BZOJ 3831: [Poi2014]Little Bird【动态规划】

    Description In the Byteotian Line Forest there are   trees in a row. On top of the first one, there ...

  8. NCCloud 指令示例

    http://ansrlab.cse.cuhk.edu.hk/software/nccloud/ Implementation of NCCloud in C++ (updated: August 2 ...

  9. PHP文件上传类(页面和调用部分)

    <!--upform.html内容--> <form action="upload.php" method="post" enctype=&q ...

  10. [配置Cordova环境] [Alfred使用手册]

    Mac神器 Alfred使用手册http://www.tuicool.com/articles/YJJv2i 配置Cordova环境 1.到nodejs官网下载最新版本,安装pkg文件 2.终端运行 ...