最长无重复子串问题 leetcode 3
一.代码及注释
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的更多相关文章
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode03 最长无重复子串
题目 给定一个字符串,找出不含有重复字符的最长子串的长度. 解答 刚开始以为只是一遍遍历后来的字符和前面一样便开始算新子串,给的案例都过了,但是卡在了"dvdf" 后来经过重重试验 ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)
这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...
随机推荐
- 快速完成智能数据构建,Dataphin公共云版本全面解读
公测两个月,Dataphin公共云版本已经受到了阿里云上众多轻量级用户的关注.事实上,Dataphin作为一款大数据智能构建与管理的产品,其核心功能是面向各行各业大数据建设.管理及应用诉求,一站式提供 ...
- 1176. Two Ends
题目链接地址:http://soj.me/1176 题目大意:两头取数.第一个人随机取,第二个人用贪婪算法(每次都取大的),求两人取数在第一个人赢的情况下的最大分差.使用贪婪算法时,如果左右两边相等, ...
- 从零学React Native之03页面导航
之前我们介绍了RN相关的知识: 是时候了解React Native了 从零学React Native之01创建第一个程序 从零学React Native之02状态机 本篇主要介绍页面导航 上一篇文章给 ...
- Mac OS X 常用快捷键一览
- Open Source Software List: The Ultimate List
http://www.datamation.com/open-source/ Accessibility 1. The Accessibility Project The Business Value ...
- oracle总是使用索引的第一个列
如果索引是建立在多个列上, 只有在它的第一个列(leading column)被where子句引用时,优化器才会选择使用该索引. 译者按: 这也是一条简单而重要的规则. 见以下实例. SQL> ...
- JS获取手机型号和系统
废话不多说,直接上源码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&q ...
- javascript 元素的大小
1.偏移量 元素的可见大小由其高度.宽度决定,包括所有内边距.滚动条和边框大小(不包含外边距).通过下列4个属性可以获取元素的偏移量: offsetHeight: offsetWidth: offse ...
- 【CSS3】使用CSS3制作全屏切换效果
在线演示: DEMO DEMO中及以下代码并没有写兼容代码,请使用高级浏览器打开,IE版本对CSS3支持并不太友好,IE11打开没有滚屏效果. 兼容代码前缀: -webkit- -moz- -o- - ...
- Python--day67--内容回顾