一.代码及注释

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n = s.size(); //字符串的长度
int ans = ; //最长无重复子串的长度
//建立map表 key为字符 val为该字符的下一个坐标位置
//val取符号坐标+1用于更新start
unordered_map<char,int> m;
//定义start和end end即为当前字符,不断+1
for(int start=,end=;end<n;end++){
//当前字符为c
char c = s[end];
//如果map中有当前字符(即出现了重复的字符),则进入if语句
if(m.find(c)!=m.end()){
/*
两种情况:
1.如果start比m[c]小,如pwwkew,start为2,end为5两个w相等
则start就更新为3,以end为结尾的最长无重复子串才能是5-3+1=3;
2.如果start比m[c]大,如kwawk,如图所示,start为2,end为5,需要
start保持不变
*/
start = max(m[c],start);
}
//ans更新,end-start+1即以当前end为结尾的最长无重复串
ans = max(ans,end-start+);
//添加数据
m.erase(c);
m.insert(make_pair(c,end+));
}
return ans;
}
};

二.图解

三.需要map的知识点(常用总结)

map的插入:常常需要和删除配合使用

map.erase(key);

map.insert(make_pair(key,val));

map的访问:直接通过访问下标

m[下标];

map的循环

auto iter = m.begin();

while(iter!=m.end()){

  cout<<iter.first()<<endl;  //输出key

  cout<<iter.second()<<endl;//输出val

}

map的查找

m.find(key);返回的是迭代器,可用m.find(key)!=m.end()判断是否存在该key。

m.count(key);返回迭代器的数量,用于可重复的map。

最长无重复子串问题 leetcode 3的更多相关文章

  1. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

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

  2. LeetCode03 最长无重复子串

    题目 给定一个字符串,找出不含有重复字符的最长子串的长度. 解答 刚开始以为只是一遍遍历后来的字符和前面一样便开始算新子串,给的案例都过了,但是卡在了"dvdf" 后来经过重重试验 ...

  3. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  4. [LeetCode] 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 3 Longest Substring Without Repeating Characters最长无重复子串

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

  7. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  8. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  9. LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)

    这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...

随机推荐

  1. XAML 很少人知道的科技 - walterlv

    原文:XAML 很少人知道的科技 - walterlv XAML 很少人知道的科技 发布于 2019-04-30 02:30 更新于 2019-04-30 11:08 本文介绍不那么常见的 XAML ...

  2. laravel 随笔

    laravel5.5 1.laravel 查询数据库默认返回对象,如何改成 返回值为数组 答:在  App\Providers\EventServiceProvider 文件中 第一步: use Il ...

  3. IOS开发之UISearchBar自定义外观

      MySearchBar.h如下: @interface MySearchBar : UISearchBar - (void)layoutSubviews; @end MySearchBar.m如下 ...

  4. ansible api 通过python 方式调用

    pip3 install ansible Linux下面安装 Windows 安装没成功 from ansible.parsing.dataloader import DataLoader #读取ya ...

  5. springmvc使用javabean作为请求参数

    1  首先写两个javabean对象  person 和 address 代码如下.两个类之间的关系如代码中 package cn.bean.demo.bo; public class Person ...

  6. 冒泡排序&直接插入排序&快速排序

    一.冒泡排序 0       1      2      3      4      5 假设有一个6个数的数组,0,1,2,3,4,5是索引,冒泡排序就是相邻两个对比,比如5和4比,如果满足条件就互 ...

  7. 【Activiti工作流引擎】官方快速入门demo

    Activiti官方快速入门demo 地址: https://www.activiti.org/quick-start 0. 版本 activiti 5.22.0 JDK 1.8 1. 介绍 这个快速 ...

  8. PHP 面试题三

    1.nginx使用哪种网络协议? nginx是应用层 我觉得从下往上的话 传输层用的是tcp/ip 应用层用的是http fastcgi负责调度进程 2. <? echo 'hello tush ...

  9. HDU 5971"Wrestling Match"(二分图染色)

    传送门 •题意 给出 n 个人,m 场比赛: 这 m 场比赛,每一场比赛中的对决的两人,一个属于 "good player" 另一个属于 "bad player" ...

  10. Navicat for MySQL 使用SSH方式链接远程数据库

    第一步:ssh部分: 端口号:22 用户名为:在xshell中用来登录服务器的账号密码 第二步: 端口:3306 账号密码:在MySQL中的登录账号密码