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, ...
随机推荐
- 爬虫必备:Python 执行 JS 代码 —— PyExecJS、PyV8、Js2Py
在使用爬虫中,经常会遇到网页请求数据是经过 JS 处理的,特别是模拟登录时可能有加密请求.而目前绝大部分前端 JS 代码都是经过混淆的,可读性极低,想理解代码逻辑需要花费大量时间.这时不要着急使用 S ...
- volatile随笔见解
1.volatile可以保证可见性,不能保证一致性,但是与cas操作结合在实现并发上性能很不错,java并发包下不少类都有这种实现方式. 2.相比synchronized执行成本更低,因为它不会引起线 ...
- server 08 R2 NBL 报错:RPC 服务器在指定计算机上不可用
排查步骤如下: 1.检查并确保 Remote Procedure Call (RPC) 和 Remote Procedure Call (RPC) Locator这两项服务是否都已经启动 2.确认此2 ...
- Java-使用哈希码比较对象的值
在一些特殊的情况下使用 package com.tj; import java.io.File; public class MyHash { public static void main(Strin ...
- luogu2604 [ZJOI2010]网络扩容
先做一遍普通的dinic 然后再更改源点为超级源,超级源向原源加一条capacity=k && cost=0的边,再加上有费用的边跑最小费用最大流 #include <iostr ...
- 【UML】UML所扮演的角色(视频总结)
导读:在国庆中,把UML视频看完了.看完了之后,对于自己到底留下了什么呢,在此就总结一下,前面总结了UML的9种图以及主要的关系,本篇博客,就从整体上对UML做一个说明. 一.总体概述 UML一共讲了 ...
- iOS阴影
但是如果把masksToBounds设置为yes就没有阴影了 UIButton *view = [[UIButton alloc]initWithFrame:CGRectMake(, , , ...
- XPosed框架_简单的应用
0. Xposed框架简介 关于Xposed框架相信大家应该不陌生了,他是Android中Hook技术的一个著名的框架,而Xposed框架是免费的而且还是开源的,本文主要介绍如何通过这个框架来进行系统 ...
- noip2018——题解&总结
近期正在疯狂复习某些东西,这篇博客尽量年底更完……(Day2T2除外) 好了,所有的希望都破灭了,原来这就是出题人的素质.——一个被欺骗的可怜 $OIer$ 人生中倒数第三次 $noip$ (Mayb ...
- 如何将一个int转换成cstring
如:int a = 5;CString b;b.Format("%d",a);补充:如果a是double,或a是float的就是:b.Format("%f",a ...