LeetCode3 Longest Substring Without Repeating Characters
题意:
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. (Medium)
解法1
自己开始AC的想法,用两重循环,搜索以s[i]开头的字串中的最长无重字串,所以一旦有重复元素出现(利用letters判重),内层的循环就可以直接跳出;
复杂度不会超过O(256(n)),因为内层最对走256步必有重复,可以不再走下去。 时间跑出来是60ms
代码1:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int result = ;
int letters[] = {};
int tempResult;
for (int i = ; i < s.size(); ++i) {
if (i + result >= s.size()) {
break;
}
tempResult = ;
memset(letters, , sizeof(letters));
letters[s[i]] = ;
for (int j = i + ; j < s.size(); ++j) {
if (letters[s[j]] == ) {
tempResult++;
letters[s[j]] = ;
}
else {
break;
}
}
result = max(result, tempResult);
}
return result;
}
};
解法2
滑动窗口方法
用left记录当前考察的可行字串的最左端,i循环遍历整个字串。
如果遇到不重复元素,则添加进hash表内,并更新最大值;
如果遇到重复元素,将left开始递增,直至跳过重复元素,然后同上操作,将该元素添加进hash表,并更新最大值。
代码2利用unodered_set实现(估计find方法效率又低了,只有72ms), 代码3利用单独开的数组实现,效率更高(16ms)。
盗用网上一幅图,出处不详...
复杂度O(2n) = O(n) ; left,i各自遍历一遍O(2n)
代码2:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_set<char> hash;
int left = ;
int result = ;
for (int i = ; i < s.size(); ++i) {
if (hash.find(s[i]) != hash.end()) {
while (s[left] != s[i]) {
hash.erase(s[left]);
left ++;
}
hash.erase(s[left]);
left ++;
}
hash.insert(s[i]);
result = max(result, i - left + );
}
return result;
}
};
代码3:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int letters[] = {};
int left = ;
int result = ;
for (int i = ; i < s.size(); ++i) {
if (letters[s[i]] != ) {
while (s[left] != s[i]) {
letters[s[left]] = ;
left ++;
}
letters[s[left]] = ;
left ++;
}
letters[s[i]] = ;
result = max(result, i - left + );
}
return result;
}
};
解法3
解法3是在2的基础上的一个简单优化, letters数组只用来保存存在与否有些浪费,同时用while循环递增left找到重复元素出现位置下一位效率略低;
可以将letters数组改为存储某个字符最近出现的位置lastIndex[256],这样更新left时,直接将left = lestIndex[s[i]] + 1即可, 将 O(2n)变为真正的一次循环 O(n)
代码4:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int lastIndex[]; // 上次出现该字符的下标
int left = ; //当前维护的字串的最左端
int result = ;
memset(lastIndex, -, sizeof(lastIndex));
for (int i = ; i < s.size(); ++i) {
if (lastIndex[s[i]] >= left) { //在这个字串内有重复 >= left
left = lastIndex[s[i]] + ;
}
lastIndex[s[i]] = i;
result = max(result, i - left + );
}
return result;
}
};
LeetCode3 Longest Substring Without Repeating Characters的更多相关文章
- Leetcode3:Longest Substring Without Repeating Characters@Python
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 最长子串(Leetcode-3 Longest Substring Without Repeating Characters)
Question: Given a string, find the length of the longest substring without repeating characters. Exa ...
- 滑动窗口解决最小子串问题 leetcode3. Longest Substring Without Repeating Characters
问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...
- Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
- LeetCode3:Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- LeetCode[3] 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, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- 文本读写vs二进制读写
[文本读写vs二进制读写] 在学习C语言文件操作后,我们都会知道打开文件的函数是fopen,也知道它的第二个参数是 标志字符串.其中,如果字符串中出现'b',则表明是以打开二进制(binary)文件, ...
- mvn安装jar文件到本地
mvn install:install-file -DgroupId=com.jfinal -DartifactId=jfinal -Dversion=2.3 -Dpackaging=jar -Dfi ...
- Struts2数据校验方法
方法: 1.在Action类中execute()方法中进行校验. 优点:Action类无需继承框架中的类. 缺点:(1)当有多个校验时,代码重复,违反高内聚,低耦合. 2.重写框架ActionSupp ...
- utf8 和 UTF-8 的区别
只有在MySQL中可以使用“utf-8”的别名“utf8”,但是在其他地方一律使用大写“UTF-8”.
- unigui TUniTreeView demo
unit untTree; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- csu oj 1811: Tree Intersection (启发式合并)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 给你一棵树,每个节点有一个颜色.问删除一条边形成两棵子树,两棵子树有多少种颜色是有 ...
- HDU1963Investment(DP)
简单DP,题解见代码
- ios实用wifi分析仪——AirPort
AirPort(wifi分析仪) android系统上免费的wifi分析仪很多,但是当我在AppStore上搜索时,找了半天也没找到想要的,后来还是问前辈才知道一款非常好用的app——AirPort, ...
- Fragment实现底部选项卡切换效果
现在很多APP的样式都是底部选项卡做为首页的,实现这样的效果,我们一般有这样几种方式,第一,最屌丝的做法,我直接自定义选项卡视图,通过监听选项卡视图,逻辑控制内容页的切换,这样做的想法一般是反正这几个 ...
- (剑指Offer)面试题22:栈的压入、弹出序列
题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等. 例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序 ...