LeetCode 3_Longest Substring Without Repeating Characters
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的更多相关文章
- LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium
题目: Given a . For . 解题思路: 这个题让找一个字符串中具有不重复单词的最长子串的长度,如:ababc,子串为abc,长度为3.有这么几个方法: 方法一: 依赖字符串本身的一些特有函 ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- C++ leetcode Longest Substring Without Repeating Characters
要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...
- [LeetCode]Longest Substring Without Repeating Characters题解
Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...
- LeetCode——Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- 初识Pyhon之准备环境
安装成功python的运行环境之后,你可能要迫不及待大展身手了 如果你有一定的语言基础,那么基础这一块儿就可以简单的看看就可以了,但是你是一个编程语言的初学者.不着急,慢慢往下看 打开pycharm创 ...
- shell相关指令介绍$*和$#以及$?和if [[ ! -z $1 ]]
$#,脚本运行时后跟的参数个数 #! /bin/bash case "$#" in 0) printf "Enter a number: " read n=$R ...
- LeetCode 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- HashMap图解
HashMap的数据结构和put.get.resize等操作的图解,看图轻松掌握HashMap (目前还不包括红黑树相关的部分) HashMap数据结构如下图 HashMap之put操作如下图 Has ...
- PAT Basic 1055
1055 集体照 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每排人数为 N/K(向下取整),多出来的人全部站在最后一排: 后排所有人的个子都不比前排任何人矮: 每排 ...
- SpringData及SpringData JPA的理解和简单应用
SpringData是一个用于简化数据库访问,并支持云服务的开源框架.其主要目标是使得 数据库的访问变得方便快捷,并支持map-reduce框架和云计算数据服务.此外,它还支持 基于关系型数据库的数据 ...
- BZOJ 2295: [POJ Challenge]我爱你啊
由于是子序列,那么难度就在于读入 #include<cstdio> #include<algorithm> #include<cstring> using name ...
- 45个有用的JavaScript技巧
众所周知,JavaScript是世界上最流行的变成语言,不管是web网页,手机APP(例如PhoneGap或Appcelerator),还是服务器端(例如NodeJS或Wakanda)还有许多其他的实 ...
- Codeforces Round #417 (Div. 2) 花式被虐
A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 异常System.Threading.Thread.AbortInternal
异常信息: System.Threading.ThreadAbortException: 正在中止线程. 在 System.Threading.Thread.AbortInternal() 在 Sys ...