题目:

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.

代码:

class Solution {
public:
int lengthOfLongestSubstring(string s) {
map<char, int> visited;
for ( char c='a'; c<='z'; ++c ) visited[c]=-;
int begin = ;
int longest_substring = ;
for ( int i = ; i < s.size(); ++i )
{
char curr = s[i];
if ( visited[curr]>=begin )
{
longest_substring = std::max(longest_substring, i-begin);
begin = visited[curr]+;
}
visited[curr]=i;
}
return std::max((int)s.size()-begin, longest_substring);
}
};

tips:

Greedy解法,主要维护以下两个内容。

维护一个哈希表:<字母,上一次字母出现的位置>

维护一个begin:表示不重复字符串的起始位置

如果当前字母在之前出现过,且出现的位置大于等于begin,则证明遇到了重复的字符;更新begin的位置为该字母上一次出现的位置+1。

在整个过程中,每次遇到不重复的字符时,就往后走。

这里要注意边界条件,即最长的字符串包含最后s的最后一个字符。

则在推出循环后,要再更新一次longest_substring。

===============================================

第二次过这道题,还是用hashmap的思路,第一次超时了,原因是想删除在local长度之前的那些hashmap表中的字符。

第二次修改了一下,hashmap表中的字符即使有重复的也不要紧,只要不在local长度范围内,都可以接受继续往后走,然后更新字符最新的位置就可以了。

class Solution {
public:
int lengthOfLongestSubstring(string s) {
map<char, int> charPosition;
int global = ;
int local = ;
for ( int i=; i<s.size(); ++i )
{
if ( charPosition.find(s[i])==charPosition.end() || charPosition[s[i]]<i-local )
{
charPosition[s[i]] = i;
local++;
}
else
{
global = max(global, local);
local = i - charPosition[s[i]];
charPosition[s[i]] = i;
}
}
return max(global, local);
}
};

【Longest Substring Without Repeating Characters】cpp的更多相关文章

  1. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  2. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  3. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  4. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

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

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

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

  6. 【leetcode】Longest Substring Without Repeating Characters (middle)

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

  7. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

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

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

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

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

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

随机推荐

  1. Python开发环境Wing IDE搜索工具介绍

    Wing IDE编辑器的搜索工具提供了一个基于友好GUI的搜索和替换工具. 某些情况下搜索可能会跨越整个文件,也有可能被限制到当前所选择的区域:可以区分大小写,也可以设置为不区分:可以被限制为只匹配整 ...

  2. [opencv3.2cmake error ] sys/videoio.h no such file or directories

    I don't have /usr/include/sys/videoio.h at all Before that , I have ipp download question. So I down ...

  3. amap -bq 192.168.5.9 80 3306

    amap -bq 192.168.5.9 80 3306 查看运行在指定端口上运行的服务

  4. SQL重复记录查询-count与group by having结合查询重复记录

    查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select  peopleId  from  p ...

  5. Openfire+spark在linux上搭建内部聊天系统

    一.    实验环境 Ubuntu server14.04 openfire:http://www.igniterealtime.org/downloads/index.jsp spark:http: ...

  6. IE Proxy Swich - IE 代理切换工具

    通过此工具可方便的切换计算机系统代理设置的开关,无需重启IE 来激活设置 下载 环境要求: 可能需要.NET 4.0 以上平台, 其他平台未测试 截图与功能如下 支持快捷方式参数 我个人习惯是在桌面 ...

  7. 使用SAP云平台的destination消费Internet上的OData service

    通过SAP云平台上的destination我们可以消费Internet上的OData service或者其他通过HTTP方式暴露出来的服务. 创建一个新的destination: 维护如下属性: 点击 ...

  8. JS.match方法 正则表达式

    match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配. 该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置. <sc ...

  9. angular4路由设置笔记

    场景说明:angular4开发的一个后台项目 一.可以使用angular-cli创建一个带路由的项目,ng new 项目名称 --routing 会多创建一个app-routing.module.ts ...

  10. systemd初始化进程(转)

    Systemd初始化进程 Linux操作系统开机过程首先从BIOS开始→进入"Boot Loader"→加载内核→内核的初始化→启动初始化进程,初始化进程作为系统第一个进程,它需要 ...