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. myeclipse2014 没有maven dependencies

    myeclipse不是非常稳定,总是会出各种各样的漏子.我一直都这样认为,可是又认为比eclipse功能多多了. 这次出现的问题是maven项目被IDE识别了.可是没有出现maven dependen ...

  2. The future of scripting in Unity

    Recently we talked about Unity and WebGL . In that post we briefly spoke about how scripting works i ...

  3. 安装QT的时候出现PATH_MAX错误

      运行c:\qt\4.5.0的configure文件的时候,出现如下的错误提示: ....\corelib\io\qfsfileengine_win.cpp(1012) : error C2065: ...

  4. Android 获得图片并解码成缩略图以减少内存消耗

    本文内容 环境 演示 下载 Demo 环境 Windows 2008 R2 64 位 Eclipse ADT V22.6.2,Android 4.4.3 SAMSUNG GT-I9008L,Andro ...

  5. Redis开发 - 1. 认识redis

    1. 什么是Redis? Redis is a very fast non-relational database that stores a mapping of keys to five diff ...

  6. 使用docker api

    前提: 系统centos 7 docker version 1.10.3 使用systemd启动docker 访问方式: 修改/usr/lib/systemd/system/docker.servic ...

  7. java 从零开始,学习笔记之基础入门<集合>(十六)

    集合 集合:将多个元素放入到一个集合对象中去,对应的集合对象就可以用来存储多元素. Collection接口的子接口:Set接口和List接口. Map不是Collection接口的子接口. Coll ...

  8. 微信小程序 - 超出文字省略组件

    使用说明 sty:定义样式 text:文字 clamp: 0:代表不限制 1:超过1行省略号(默认) n:超过n行省略     点击下载:ellipsis    

  9. uni - 自定义组件

    目录结构如下 点击下载自定义组件示例

  10. kafka性能测试(转)KAFKA 0.8 PRODUCER PERFORMANCE

    来自:http://blog.liveramp.com/2013/04/08/kafka-0-8-producer-performance-2/ At LiveRamp, we constantly ...