原题链接:传送门

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.

题目大意:

给定一个字符串,找到没有重复字符的最长子串,然后返回这个子串的长度。比如“abcabcbb”中没有重复字符的最长子串是“abc”,返回3;“bbbbbbbb”中没有重复字符的最长子串是“b”,返回1。

这个题目的解决思路主要有两个,一个是哈希表,一个是双游标遍历。

由于要找没有重复字符的子串,所以我们要记住当前子串中都出现了什么字符。这里用unordered_set来存储出现的字符。下面是unordered_set的简单介绍,以后再详细了解下。

unordered表示它是无序的,也就是说,容器中的元素没有特别的次序关系,set说明这是一个set容器,也就是说元素的值可以标示对应的元素。然后我们需要两个游标来对字符串进行遍历,一个begin一个end,end游标主动向前移动,每移动一步,就在哈希表中检查end游标对应的字符是否存在,如果不存在,说明当前begin到end之间的子串满足没有重复字符的要求,end可以继续移动;如果end对应的字符在哈希表中已经存在,说明在begin到end之间有一个字符和end对应的字符相同,接下来就需要找到这个字符,由于这个字符在end的左面,这时需要用begin游标来找到这个字符。

begin游标的任务是找到begin到end这一子串中和end对应的字符相等的字符,需要begin逐一检查,如果相同说明找到,可以退出循环;如果不同,说明begin到end之间的子串还不符合条件,因此可以将begin对应的字符从哈希表中删除。

在遍历的过程中,依次更新最大长度,直到遍历完,就可以得到结果。对应的代码如下:

class Solution {
public:int lengthOfLongestSubstring(string s) {int n=s.size();if(n==0)return 0;//哈希表,用来存储当前子串中已经出现的字符unordered_set<char> is_shown;int begin=0;//左游标int end=0;//右游标int ret=0;//返回结果//对整个字符串进行遍历while(end<n){unordered_set<char>::iterator it;//查找end对应的字符是否出现过it=is_shown.find(s[end]);//出现过if(it!=is_shown.end()){//更新ret的值if(ret<end-begin)ret=end-begin;//end对应的字符在当前子串中出现过,说明在begin到end之间有一个和s[end]相同,找到它while(s[begin]!=s[end]){//不同的时候,需要把begin对应的字符从哈希表中删除,因为它已经不再当前子串中了is_shown.erase(s[begin]);begin++;}//循环结束,此时s[begin]和s[end]相同,向右移一位begin++;}//没出现过,说明当前子串仍然满足条件,并将s[end]添加到哈希表中else{is_shown.insert(s[end]);}//end右移end++;}//更新retret=max(ret,end-begin);return ret;}
};

LeetCode3:Longest Substring Without Repeating Characters相关推荐

  1. 滑动窗口解决最小子串问题 leetcode3. Longest Substring Without Repeating Characters

    问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...

  2. leetcode3. Longest Substring Without Repeating Characters

    解法一: 1.维护三个变量: max用于记录当前最大值, next表示当前维护的正确子串, pre表示之前出现的重复字符再上一次出现的最靠前位置 A1:当前重复字符当前位置 A2:当前重复字符上一次位 ...

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

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

  4. 3.longest substring without repeating characters

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

  5. [LeetCode]3.Longest Substring Without Repeating Characters

    [题目] Given a string, find the length of the longest substring without repeating characters. For exam ...

  6. Longest Substring Without Repeating Characters(最长不重复子序列求解)

    问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...

  7. [LeetCode] Longest Substring Without Repeating Characters

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

  8. LeetCode——Longest Substring Without Repeating Characters

    原问题 Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  9. LeetCode:3. Longest Substring Without Repeating Characters

    https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...

最新文章

  1. windows10安装docker
  2. SAP S/4 Hana On-premise Edition 1511做了哪些简化
  3. 最简单易懂的对拍讲解
  4. dtmf信号系统的matlab仿真,dtmf信号系统的matlab仿真毕业设计
  5. python自动化办公 51cto_利用python实现批量自动化运维脚本案例
  6. 如何胜任一个小型公司的技术总监?
  7. CKEditor设置背景图片及宽高
  8. 55. Attribute name 属性
  9. linux调度器(十)——调度器/proc信息解读
  10. centos中多台主机免密登录_关于单点登录(SSO)数据共享(session和redis)的那点事?...
  11. Go黑魔法之导出私有函数与私有变量
  12. 惯导系统误差分析(一) 惯性导航系统的误差与误差源
  13. H5游戏忆童年—承载梦想的纸飞机回来了吗?
  14. 前端使用vux中md5加密方法
  15. thinkadmin关联查询
  16. vim 编辑器常用操作
  17. tex常用函数 上下行对齐_tex常用函数 上下行对齐_【学术写作】如何优雅地(用TeX)写AI论文...
  18. JZ17 树的子结构
  19. 利用opencv+QT打开并显示图片
  20. sigmoid的通俗理解

热门文章

  1. 使用CodeDom生成程序集
  2. 可以在xml中靠增加属性来实现分组
  3. android git上传出现错误,热更新上传patch包时提示上传失败,文件不合法
  4. 计算机c语言二级题型,计算机二级C语言题型和评分标准
  5. webviewjavascriptbridge android ios,js与ios交互使用WebViewJavascriptBridge如何写多个函数
  6. php异步请求$.post,如何用PHP实现异步请求、忽略返回值
  7. android commit apply,关于SharedPreference的commit和apply和ANR
  8. linux中hadoop命令大全,hadoop常用命令
  9. VMware14.0 安装 CentOS7.2
  10. C++ 内存基本构件 placement new