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. jQuery EasyUI 入门简介

    对于前端开发者来说,在开发过程中应用“框架”这一工具,可以极大的缩短开发时间,提高开发效率.今天我们就开介绍一款常用的框架——jQuery EasyUI. 那什么是jQuery EasyUI呢? jQ ...

  2. Spring(十九):Spring AOP(三):切面的优先级、重复使用切入点表达式

    背景: 1)指定切面优先级示例:有的时候需要对一个方法指定多个切面,而这多个切面有时又需要按照不同顺序执行,因此,切面执行优先级别指定功能就变得很实用. 2)重复使用切入点表达式:上一篇文章中,定义前 ...

  3. OpenNebula学习第四节之磁盘镜像的制作

    一.准备工作 需要准备一个Ubuntu16.04的ISO文件上传至服务器,如下图所示 二.操作步骤 2.1:创建操作目录 创建一个/var/tmp/cloud_image目录,把Ubuntu16.04 ...

  4. 获取sevlet response值

    调用: PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8 ...

  5. eclipse,myeclipse综合

    1.Myeclipse点击发布无反应 进入workspace目录,删除.metadata\.plugins\org.eclipse.core.runtime\.settings\com.genuite ...

  6. yii源码三 -- db

    <AR> CActiveRecord:path:/framework/db/ar/CActiveRecord.phpoverview:is the base class for class ...

  7. Discuz常见小问题-如何人为地添加用户并分配小组

    进入后台,在用户-添加用户中可以人为添加用户并分配权限

  8. 微软BI 之SSRS 系列 - 报表邮件订阅中 SMTP 服务器匿名访问与 Windows验证, 以及如何成功订阅报表的实例

    这篇文章源于在上一篇博文中有园友提出订阅 SSRS 报表时的一个问题,  于是就好好总结了一下,把有关 SSRS 报表订阅的要点和容易出现问题的地方写出来,希望对大家有所帮助! 参看上一篇博文 - S ...

  9. NUMA and vNUMA

    NUMA and vNUMA posted by szamosattila on march 04, 2012Tutorial, Virtualization With the spread of S ...

  10. 算法笔记_227:填写乘法算式(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 观察下面的算式: * * × * * = * * * 它表示:两个两位数字相乘,结果是3位数.其中的星号(*)代表任意的数字,可以相同,也可以不同, ...