最长无重复子串问题 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 ...
随机推荐
- 类的重载case2
/*功能:类的成员属性和成员方法*/ public class Person{ public static void main(String args[]){ TruePerson ...
- 2019-1-9-WPF-最小的代码使用-DynamicRenderer-书写
title author date CreateTime categories WPF 最小的代码使用 DynamicRenderer 书写 lindexi 2019-1-9 14:7:26 +080 ...
- Node.js MVC模式+MongoDB实现学员管理系统
目录结构: 项目入口文件 /* Author:张波 */ /* 文件说明: 此文件是本项目的入口文件 启动这个项目,会先执行本文件中的代码 */ // 1. 引入模块 const http = req ...
- 自定义View系列教程06--详解View的Touch事件处理
深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...
- 阿里云PolarDB发布重大更新 支持Oracle等数据库一键迁移上云
5月21日,阿里云PolarDB发布重大更新,提供传统数据库一键迁移上云能力,可以帮助企业将线下的MySQL.PostgreSQL和Oracle等数据库轻松上云,最快数小时内迁移完成.据估算,云上成本 ...
- PHP怎么调用其他类的方法
2个PHP,这个PHP中的类调用另一个PHP中的类,如何调用.Java中是import ,php中是什么?还是用其他什么方法? 1.引用类:比如类名为product,则:include('...路径/ ...
- spark.read.csv读取CSV文件 ArrayIndexOutOfBoundsException报错
通过 spark.read.csv读取CSV文件时,遇到 到 ArrayIndexOutOfBoundsException报错,初步判断是缺少参数导致,放百度看看,没找引起问题相关的参数. 第一个看到 ...
- QQ 第三方登录
- Redis正确使用的十个技巧
http://www.jb51.net/article/73376.htm Redis 在当前的技术社区里是非常热门的.从来自 Antirez 一个小小的个人项目到成为内存数据存储行业的标准,Redi ...
- 在CentOS7上安装ftp服务器用于保存服务端上传的图片。
1.CentOS卸载vsftpd的方法 如果服务器上已经安装了vsftpd服务,配置出错需要卸载vsftpd服务. 1.1 查找vsftpd服务 [root@localhost /]# rpm -aq ...