Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

双指针begin,end.扩展end,收缩begin.

[begin,end]之间为无重复字母的子串。

可与Longest Substring with At Most Two Distinct Characters对照看。

解法一:

使用unordered_map记录每个字母最近出现下标。

[begin, end]表示无重复字符的窗口。

在end指向一个窗口中的字符时,需要将begin收缩到该字符之后,以免与end重复。

时间复杂度:O(n)

空间复杂度:O(n)

class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s == "")
return ; unordered_map<char, int> m; //char-index map
int ret = ;
int begin = ;
m[s[begin]] = ;
int end = ;
while(end < s.size())
{
//extend
if(m.find(s[end]) == m.end() || m[s[end]] < begin)
{
;
}
//shrink
else
{
begin = m[s[end]]+;
}
ret = max(end-begin+, ret);
m[s[end]] = end;
end ++;
}
return ret;
}
};

解法二:

使用record[128]记录A~Z,a~z在窗口中的出现次数。

由于无重复的要求,因此在end指向一个已经出现过的字母(即record[s[end]]不为零)

需要收缩begin直到begin遇到end或者record[s[end]]为零。

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int longest = ;
int begin = ;
int end = ;
int record[] = {}; //initial to all 0
while(end < s.size())
{
while(record[s[end]]> && begin<end)
{
record[s[begin]] --;
begin ++;
}
//record[s[end]]==0 || begin==end
record[s[end]] ++;
longest = max(longest, end-begin+);
end ++;
}
return longest;
}
};

【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)的更多相关文章

  1. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

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

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

  3. 【LeetCode】3. Longest Substring Without Repeating Characters

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

  4. 【LeetCode】003. Longest Substring Without Repeating Characters

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

  5. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  6. leetcode-【中等题】3. Longest Substring Without Repeating Characters

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

  7. leetcode题解 3. Longest Substring Without Repeating Characters

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

  8. 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

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

随机推荐

  1. 让App中增加LruCache缓存,轻松解决图片过多造成的OOM

    上次有过电话面试中问到Android中的缓存策略,当时模糊不清的回答,如今好好理一下吧. Android中普通情况下採取的缓存策略是使用二级缓存.即内存缓存+硬盘缓存->LruCache+Dis ...

  2. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(八)安装zookeeper-3.4.12

    如何搭建配置centos虚拟机请参考<Kafka:ZK+Kafka+Spark Streaming集群环境搭建(一)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网.& ...

  3. 命令行下从bak文件恢复sqlserver数据库方法

    命令行下从bak文件恢复sqlserver数据库方法 注:本文所示访问从SqlServer 2000 - 2014版都是通用的 参考:http://blog.sina.com.cn/s/blog_5c ...

  4. [React] Write a stateful Component with the React useState Hook and TypeScript

    Here we refactor a React TypeScript class component to a function component with a useState hook and ...

  5. MyBatis对于Java对象里的枚举类型处理

    平时咱们写程序实体类内或多或少都会有枚举类型属性,方便嘛.但是mybatis里怎么处理他们的增删改查呢? 要求: 插入的时候,会用枚举的定义插入数据库,我们希望在数据库中看到的是数字或者其他东西: 查 ...

  6. asp.net正则表达式

    导入引用命名空间:using System.Text.RegularExpressions //Regex类,常用方法: //摘要:1.IsMatch(String);2.IsMatch(String ...

  7. 牛客网-《剑指offer》-矩形覆盖

    题目:http://www.nowcoder.com/practice/72a5a919508a4251859fb2cfb987a0e6 C++ class Solution { public: in ...

  8. 搭建持续集成单元测试平台(Jenkins+Ant+Java+Junit+SVN)

    一.环境准备 Jenkins: 到官网下载jenkins.war包:http://jenkins-ci.org/ 安装方法有两种: 把下载下来的jenkins.war包放到文件夹下,如C:\jenki ...

  9. winf

    真的,先亮注册码!!(直接复制即可) 注册码: <第1组> 用户名:大眼仔~旭(Anan) 注册码:01000000007002140V <第2组> 用户名:大眼仔~旭(Ana ...

  10. openerp 7.0邮件多用户发送失败问题 解决方法

    方法一(推荐): 修改代码/usr/lib/pymodules/python2.7/openerp/addons/base/ir/ir_mail_server.py #425 line: #mail_ ...