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. JAVA图像缩放处理

    http://www.blogjava.net/kinkding/archive/2009/05/23/277552.html ———————————————————————————————————— ...

  2. 类加载器(ClassLoader)

    静态库.动态连接库 程序编制一般需经编辑.编译.连接.加载和运行几个步骤.在我们的应用中,有一些公共代码是需要反复使用,就把这些代码编译为“库”文件:在连接步骤中,连接器将从库文件取得所需的代码,复制 ...

  3. AWT从概念产生到完成实现只用了一个月

    这种糟糕的设计选择使得那些拥护Java“一次编写,到处运行 (write once, run anywhere)”信条的程序员们过得并不舒畅,因为AWT并不能保证他们的应用在各种平台上表现得有多相似. ...

  4. smartJS 0.1 API 讲解 - Trigger

    上篇介绍了PromiseEvent,本篇介绍Trigger - 基于Promise的aop的体现:(感觉自己的对这些命名一直都很挫,也懒得想了,所以就凑合的用) Trigger 在目标对象上加入触发器 ...

  5. Linux系统常用工具集

    整理Linux系统下一些日常工作中常用工具,旨在提高效率: 1.截图软件Shutter 2.通讯聊天工具pidgin 3.守护进程工具daemontools 4.远程桌面服务TigerVNC 5.Ma ...

  6. python进阶九_网络编程

    Python网络编程一 一.一些基本概念 在Python网络编程这一节中会涉及到非常多网络相关的术语.对于一些最主要的概念,如TCP/IP,Socket等等不再赘述,不明确的能够自己去查一查,对于一些 ...

  7. django用户认证系统——拓展 User 模型2

    Django 用户认证系统提供了一个内置的 User 对象,用于记录用户的用户名,密码等个人信息.对于 Django 内置的 User 模型, 仅包含以下一些主要的属性: username,即用户名 ...

  8. knowledgeroot 的配置与优化

    首先下载 KnowledgeRoot 的安装包,就是一个压缩文件,解压缩后放到 WebRoot 下面 在浏览器中打开网站,自动提示进行安装,安装的过程很简单,安装结束后即可以使用. 安装包创建的数据库 ...

  9. [HackerRank] The Longest Common Subsequence

    This is the classic LCS problem. Since it requires you to print one longest common subsequence, just ...

  10. TCL V7300A-3D升级教程

    鄙人的电视型号: 机器软件版本:V8-0MT-3201-LF1V028_000 3201是机芯 v028是版本号 设备型号:cn_mt32-v7300a-3d TCL电视升级有三个:本地.网络.自动 ...