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 ...
随机推荐
- logback打印不出日志
原因: 1. 可能是jar包不完整 , 基本jar包包括: logback-access , logback-core , logback-classic , perf4j , slf4j-api ...
- mysql cluster 名词概念解读
Node Group [number_of_node_groups] = number_of_data_nodes / NoOfReplicas Partition When using ndbd, ...
- 清空easyui datagrid
$('#grid').datagrid("loadData",{total:0,rows:[]});
- POJ 3347 Kadj Squares
Kadj Squares Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2132 Accepted: 843 Descr ...
- UVaLive 6809 Spokes Wheel (模拟)
题意:给定两个16进制数,问你把它转成二进制后,把第一个向左或者向右旋转最少的次数同,使得第一个变成第二个. 析:也是比较水的,按照要求做就好,注意0的情况,可能会忘记. #pragma commen ...
- HDU 5703 Desert (找规律)
题意:一杯水有n的容量,问有多少种方法可以喝完. 析:找规律,找出前几个就发现规律了,就是2的多少次幂. 代码如下: #include <cstdio> #include <stri ...
- 分享一下jQuery UI的地址
jQuery EasyUI: http://www.jeasyui.com/ DWZ: http://j-ui.com/ Liger UI: http://www.ligerui.com/ Liger ...
- jsp页面加载readyState的五种状态根据我们状态添加进度条
这段代码放在页面最下面 原文如下: document.onreadystatechange = subSomething;//当页面加载状态改变的时候执行这个方法. function subSomet ...
- Java调用Telnet示例
import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.io.U ...
- c++中的signal机制
简介 signal是为了解决类之间通信的问题而出现的,更深入的原因是面向对象讲究封装,但是封装必然导致类之间沟通困难,但是使用接口的方式又太重量级--需要写很多代码,而且会导致接口爆炸 比如你需要把一 ...