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.

暴力:时间0(n2)

空间o(n)

 class Solution {
public:
int lengthOfLongestSubstring(string s) {
const int n = s.size();
std::unordered_set<char> s_set;
int res = ;
for (int i = ; i < n; i++) {
int j = i;
s_set.clear();
for (; j < n ;++j) {
if (s_set.find(s[j])==s_set.end()) {
s_set.emplace(s[j]);
} else {
break;
}
}
res = std::max(res,j-i);
}
return res;
}
};
 class Solution {
public:
int lengthOfLongestSubstring(string s) {
const int n = s.size();
std::unordered_set<char> s_set;
int res = ;
int j = ;
for (int i = ; i < n;++i) {
while(s_set.find(s[i])!=s_set.end()) {
s_set.erase(s[j]);
j++;
}
res = std::max(res,i-j+);
s_set.emplace(s[i]);
}
return res;
}
};

这个题的关键在于重复的字符!

一个子串不可能出现重复的字符,那么我们可以用两个指针,一个指针用来遍历字符串,两个指针之间保持无重复字符,那么两个指针之间的长度即最大子串的长度。当发现有重复的字符时,另一个指针指向这个字符上一次出现的位置的下一个字符,继续遍历,直到找到最长子串。

 class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
j = 0
maxlen = 0
used ={}
for i ,c in enumerate(s):
if c not in used or used[c] < j:
maxlen = max(maxlen,i-j + 1)
else:
j = used[c] + 1
used[c] = i
return maxlen

代码中:

 used[c] < start  是为了处理"tmmzuxt" 这种连续出现的2个字母。

3. Longest Substring Without Repeating Characters(最长子串,双指针+hash)的更多相关文章

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

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

  2. 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium

    Examples: Description: Given a string, find the length of the longest substring without repeating ch ...

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  4. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

    Given a string, find the length of the longest substring without repeating characters. Example 1:    ...

  5. LeetCode Longest Substring Without Repeating Characters 最长不重复子串

    题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...

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

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

  7. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

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

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  9. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

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

随机推荐

  1. pom打包参数选择

    pom.xml配置 <profiles> <profile> <id>dev</id> <properties> <token> ...

  2. linux split

    说来惭愧,用了这么久linux会的命令也只有常用的那么几个.. 今天刚刚学到的一个很实用的split命令,原本就只是知道开发语言中有split方法用来切分字符串,linux命令行也提供了这样一个方法. ...

  3. c++封装的发邮件类CSendMail

    项目需要做发邮件的功能,在网上找了一下代码,比较出名的SMailer编译不过(把那个Base64的encode拉到MailSender中实现就能过,但我搞不懂原来出错的原因,就不想用),另外找到了一个 ...

  4. css3学习笔记(一)

    1. IE下的渐变:  filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff',endColorstr='# ...

  5. head管理EC下载,配置启动

    参考文档:https://blog.csdn.net/yx1214442120/article/details/55102298

  6. 第二百一十七节,jQuery EasyUI,NumberSpinner(数字微调)组件

    jQuery EasyUI,NumberSpinner(数字微调)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 NumberSpinner ...

  7. mac 干掉Dashboard

    打开终端,输入下面的命令: defaults write com.apple.dashboard mcx-disabled -boolean YES   然后再重启一下 Dock,在终端输入 kill ...

  8. html table 上移下移

    js操作表格操方法,增加,修改,删除,一行记录 随机选择行 添加一行 删除选定行 上移选定行 下移选定行 按第一列排序 按数据和排序   <!DOCTYPE html PUBLIC " ...

  9. 常用的tagVARIANT结构【整理】

    VARIANT数据结构包含两个域(如果不考虑保留的域).vt域描述了第二个域的数据类型.为了使多种类型能够在第二个域中出现,我们定义了一个联合结构.所以,第二个域的名称随着vt域中输入值的不同而改变. ...

  10. 在ASP.NET MVC3 中利用JSONP跨域登录WEB系统

    在信息系统开发的时,根据相关业务逻辑难免会多系统之间互相登录.一般情况下我们需要在多系统之间使用多个用户名和密码.这样客户就需要在多个系统之间重复登陆.每次登录都需要输入用户名和密码.最近比较流行的就 ...