题意:

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的更多相关文章

  1. Leetcode3:Longest Substring Without Repeating Characters@Python

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  2. 最长子串(Leetcode-3 Longest Substring Without Repeating Characters)

    Question: Given a string, find the length of the longest substring without repeating characters. Exa ...

  3. 滑动窗口解决最小子串问题 leetcode3. Longest Substring Without Repeating Characters

    问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...

  4. Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串

    给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...

  5. LeetCode3:Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  6. [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  7. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  8. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

随机推荐

  1. 深入.Net字符串类型

    .Net的字符串其实还是有很多东西可以写的.但是最近在学习SQL Server,只好先做下最近学习到的一些巧用,妙用之类的东西. 巧用String.Join拼接字串数组,字符串集合为字符串.如果在之前 ...

  2. Linux下的paste合并命令详解

    paste单词意思是粘贴.该命令主要用来将多个文件的内容合并,与cut命令完成的功能刚好相反. 粘贴两个不同来源的数据时,首先需将其分类,并确保两个文件行数相同.paste将按行将不同文件行信息放在一 ...

  3. gcc -D

    [gcc -D] -D name Predefine name as a macro, with definition 1. 通常debug和release版的区别就在于是否有DEBUG宏,DEBUG ...

  4. Java反射机制(Class类的使用)

    1:通过无参构造实例化对象 package cn.itcast; /* * 通过无参构造实例化对象 * 通过Class类本身实例化对象,使用newInstance方法 * 需要注意的是:实例化类中存在 ...

  5. CodeForces 7C Line

    ax+by+c=0可以转化为ax+by=-c: 可以用扩展欧几里德算法来求ax1+by1=gcd(a,b)来求出x1,y1 此时gcd(a,b)不一定等于-c,假设-c=gcd(a,b)*z,可得z= ...

  6. 用python实现远程复制 (scp + expect )

    scp 功能很强大,但需要人工输入 password, 当然可以通过把 公钥保存在远程主机的 ~/.ssh 目录中,而后就不用输入password,但这需要配置. 用 sshpass 可能在命令输入 ...

  7. datagridview自动填充列头

    //填充datagridview dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

  8. IIS6.0禁止用户下载txt文件

    服务器win2003+IIS6.0,在IIS上限制下载.txt文件,方法比较简单,在IIS的应用程序扩展名映射中为资源扩展名指定一个错误的可执行文件即可. 打开记事本,直接将空内容保存为C:\WIND ...

  9. sql server中的decimal或者numeric的精度问题

    何谓精度:最多可以存储的十进制数字的总位数,包括小数点左边和右边的位数.该精度必须是从 1 到最大精度 38 之间的值.默认精度为 18. 小数位数:小数点右边可以存储的十进制数字的最大位数.小数位数 ...

  10. hdoj 5375 Gray Code

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 编码规则:tmp = XOR(gr[i],gr[i-1]); 算是找规律的题目吧,考虑?前后字符 ...