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. 前端开发中提到的“脚手架”到底指什么,CLI?gulp 和 gulp-cli有什么区别

    一般来说,脚手架是帮你减少「为减少重复性工作而做的重复性工作」的工具. gulp和gulp-cli的区别可以看这个task - what does gulp-"cli" stand ...

  2. A Fast and Easy to Use AES Library

    http://www.codeproject.com/Articles/57478/A-Fast-and-Easy-to-Use-AES-Library Introduction EfAesLib i ...

  3. scheduleWithFixedDelay和scheduleAtFixedRate源码分析

    先放张图,有兴趣的话我再加细节说明. scheduleWithFixedDelay和scheduleAtFixedRate的执行流程都是一样的,如下 ScheduledThreadPoolExecut ...

  4. 快速入门Matplotlib

    十分钟快速入门Matplotlib 函数式绘图 这个库主要有两种绘图方式,一种是像这样的类matlab的函数式绘图方法. import matplotlib.pyplot as plt import ...

  5. django的rest framework框架——安装及基本使用

    一.django的FBV 和 CBV 1.FBV(基于函数的视图): urlpatterns = [ url(r'^users/', views.users), ] def users(request ...

  6. Hive 启动报错 URI

    Exception in thread "main"java.lang.RuntimeException: java.lang.IllegalArgumentException:j ...

  7. xtu数据结构 I. A Simple Tree Problem

    I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld     ...

  8. Leetcode 388.文件的最长绝对路径

    文件的最长绝对路径 假设我们以下述方式将我们的文件系统抽象成一个字符串: 字符串 "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" 表示: dir ...

  9. numpy之flatnonzero函数

    Return indices that are non-zero in the flattened version of a. This is equivalent to a.ravel().nonz ...

  10. 【Luogu】P1330封锁阳光大学(bfs染色)

    题目链接 这题恶心死我了. bfs染色,统计每个联通块两色的个数,ans加它们的最小值. #include<cstdio> #include<cctype> #include& ...