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. Spark:java api实现word count统计

    方案一:使用reduceByKey 数据word.txt 张三 李四 王五 李四 王五 李四 王五 李四 王五 王五 李四 李四 李四 李四 李四 代码: import org.apache.spar ...

  2. JS 判断浏览器是否安装Flash 兼容IE、firefox

    /** * @Author: HTL * @Email: Huangyuan413026@163.com * @DateTime: 2016-06-02 11:37:05 * @Description ...

  3. Windows远程桌面连接的利器-mRemote

    mRemoteNG是Windows平台下一款开源的支持多标签.多协议的远程连接管理器.平时我们可能安装N多款管理工具,如putty.SecureCRT.xshell.SSHshell.mstsc.ex ...

  4. tail -f 然后grep,处理缓存的问题

    学习了:http://www.quwenqing.com/read-134.html 对日志记录做多次grep过滤输出,格式如下: tail -f log | grep xxx | grep yyy ...

  5. window下配置Apache2服务器

    1:去Apache.org下载安装包 http://httpd.apache.org/ 2:解压到某一个目录 3:修改httpd.conf(Apache的解压目录和端口号) 4:管理员方式启动cmd执 ...

  6. 关闭Pycharm拼写检查

    转载: https://blog.csdn.net/u013088062/article/details/50001189 Pycharm作为一款优秀的PythonIDE,唯一让我觉得不安的就是它的拼 ...

  7. Android 原生 Android ActionBar Tab (滑动)导航

    本文内容 环境 项目结构 演示一:ActionBar Tab 导航 演示二:ActionBar Tab 带滑动导航 本文演示 Tab 导航.第一个演示,是基本的 Tab 导航,第二个是带滑动的 Tab ...

  8. linux ifconfig -a

    export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/i686-pc ...

  9. HDS TrueCopy-数据远程容灾白皮书-IOPS数据

    http://wenku.it168.com/d_000767925.shtml Truecopy 安装实施-包含图 http://www.docin.com/p-261693079.html 来自: ...

  10. Spring-boot 1.5.2 下隐藏Banner

    在配置文件中增加: spring.main.banner-mode=off