题目:

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. linux修改系统时间为北京时间(CentOS)

    删除本地时间 rm -rf /etc/localtime 设置时区为上海 ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 利用date查看 ...

  2. 编译出freeswitch的java调用的 jar和so

    假设freeswitch 源码路径为 /usr/local/src/freeswitch 1. cd /usr/local/src/freeswitch(源代码的根目录) 执行./configure, ...

  3. 开发Maven插件

    Mojo: Maven plain Old Java Object 1.插件命名规则:maven-<yourplugin>-plugin是Maven的保留字段,不允许使用,我们可以用< ...

  4. redis在Windows下以后台服务一键搭建集群(单机--伪集群)

    redis在Windows下以后台服务一键搭建集群(单机--伪集群) 一.概述 此教程介绍如何在windows系统中同一台机器上布置redis伪集群,同时要以后台服务的模式运行.布置以脚本的形式,一键 ...

  5. Mac Office 2016 卸载

    https://support.office.com/zh-cn/article/%E5%8D%B8%E8%BD%BD-Office-2016-for-Mac-eefa1199-5b58-43af-8 ...

  6. javascript:理解try...catch...finally

    以前,我一直喜欢用console.log(do some thing)去执行输出的类型和值,想马上看到弹出的信息,就会直接在浏览器alert()一下,这些是基础知识. 稍微复杂一点点,就要用到判断语句 ...

  7. hive对有null值的列进行avg,sum,count等操作时会不会过滤null值

    在hive中,我们经常会遇到对某列进行count.sum.avg等操作计算记录数.求和.求平均值等,但这列经常会出现有null值的情况,那这些操作会不会过滤掉null能呢? 下面我们简单测试下: wi ...

  8. c++连接mysql并提示“无法解析的外部符号 _mysql_server_init@12”解决方法&提示缺少“libmysql.dll”

    课程作业要用c++连接mysql server,但是出现些小问题,经查阅资料已经解决,做一下笔记. 环境:vs2017, mysql版本是8.0.16-winx64. 设置项目属性   项目 -  C ...

  9. 多GPU设备处理点积示例

    多GPU设备处理点积示例,项目打包下载 /* * Copyright 1993-2010 NVIDIA Corporation. All rights reserved. * * NVIDIA Cor ...

  10. C#事件与接口编程实例

    很多初学c#的朋友对于事件与接口感到迷惑不解,不明白它们之间的关系,下面我就用实例来简单的分析讲解一下. 事件,用event修饰符来代表一个事件,我们要创建一个C#事件必须按以下顺序来扫行: 1,创建 ...