一.代码及注释

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. Creating a Pulsing Circle Animation

    原文 https://www.kirupa.com/animations/creating_pulsing_circle_animation.htm Outside of transitions th ...

  2. Android 整合实现简单易用、功能强大的RecyclerView

    之前总是会有人在一些开发群里问,有木有比较好使且功能强大些的RecyclerVew,比如支持下来刷新,加载更多等,还有人在问,如何为RecyclerView添加分割线,尤其是如何为网格布局添加分割线? ...

  3. mysql数据库之多表查询

                                                                            准备                          ...

  4. intellij idea 搜索

    . Ctrl+N 按名字搜索类 相当于eclipse的ctrl+shift+R,输入类名可以定位到这个类文件 就像idea在其它的搜索部分的表现一样,搜索类名也能对你所要搜索的内容多个部分进行匹配 甚 ...

  5. Window setTimeout() 方法

    定义和用法 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 注意:如果你只想重复执行可以使用 setInterval() 方法. 可以使用clearTimeout()方法来阻 ...

  6. 用CSS画平行四边形

    <div class="jx">我是一个平行四边形</div> <style> .jx{ //定义div的高度宽度 transform:skew ...

  7. java表达式和三目运算符

    是由数字.运算符.数字分组符号(括号)等以能求得数值的有意义排列的序列; a + b 3.14 + a (x + y) * z + 100 boolean b= i < 10 && ...

  8. 微信小程序wx.request 请求方法

    data: 最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String .转换规则如下: 对于 GET 方法的数据,会将数据转换成 quer ...

  9. linux中添加常用应用程序的桌面图标

    在网上随处可以找到怎么样把应用程序的图标放到桌面上,我刚用ubuntu时也是按照网上的做法,一步一步的做的,现将网上的做法复制下来: 桌面配置文件简述\label{sec:desktop file} ...

  10. H3C IPv6地址配置命令