https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

内容描述:

Given a string, find the length of the longest substring without repeating characters.Example 1:Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

这个问题的特点就是字符序列不能出现重复,一旦出现重复,比如说w字母出现了两次,就要比较w出现第一次和第二次这段区间之内的字符串和原来的字符串之间哪个字符串更长,并更新最长字符串。

我们可以定义一个集合,设置一个start指针,一个end指针。res表示的最长字符串的长度。end指针每次都往后移动一个字符,然后判断这个指针是否在集合里面,如果不在集合里面,就把end指针指向的字符添加到集合里面,并且更新最长字符标识res。

如果end指针中的值出现在集合之中了,就说明出现了重复字符。这时候就应该把start指针往后移动到该字符第一个出现的位置,比较res中的值和新移动的值哪个更大一些,然后更新res。

这里存在一个问题,start指针,一个一个往后移动效率有点低,可不可以一次就定位到指定的位置?答案是可以的,这时候就不能用集合了,可以使用字典的键值对的办法来进行一次定位。但是这样也有一个很难操作的问题,比如说,对于abcabc而言,当end指针移动到第二次出现a的时候,这个时候,start指针如果也指向end指针一样的位置的话,当下一次end指针移动到b的时候,会面临字典中第一次出现的b无法删除的情况。

事实上,没有这么麻烦,我们的想法很简单,我们其实是想,比如说字母c当第二次出现的时候,那么我们start指针指向字母c第一次出现下一个位置就好了。明白这样的思路,代码写起来就很简单了。

class Solution:def lengthOfLongestSubstring(self, s):""":type s: str:rtype: int"""# start 指针指向的是当前子串首字符在 input 中对应的indexres, start, n = 0, 0, len(s) maps = {}for i in range(n):start = max(start, maps.get(s[i], -1) + 1) # get方法返回第一次重复字符出现的位置,这样我们向后+1就是希望start的位置了res = max(res, i - start + 1) # 当前子串满足条件了,更新结果maps[s[i]] = i # 将当前字符与其在 input 中的 index 记录下来return res

LeetCode:3. Longest Substring Without Repeating Characters相关推荐

  1. 【贪心】LeetCode 3. Longest Substring Without Repeating Characters

    LeetCode 3. Longest Substring Without Repeating Characters Solution1:我的答案 该方法中哈希表记录的是字符出现的次数.标准的贪心算法 ...

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

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

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

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  4. leetcode 3. Longest Substring Without Repeating Characters 最长非重复子串的长度 滑动窗口法

    题目链接 根据我们之前介绍的滑动窗口法的解法: 滑动窗口法详解 leetcode 438. Find All Anagrams in a String 滑动窗口法 这题,我们不难解决,使用之前的模板. ...

  5. python刷leetcode_零基础python刷leetcode -- 3. Longest Substring Without Repeating Characters

    算法很重要,但是每天也需要学学python,于是就想用python刷leetcode 的算法题,和我一起开始零基础python刷leetcode之旅吧.如有不对的地方,希望指正,万分感谢~~ 题目 最 ...

  6. leetcode 3. Longest Substring Without Repeating Characters | 3. 无重复字符的最长子串(双指针+滑窗)

    题目 https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题解 双指针+滑窗,维护一个 set, ...

  7. LeetCode 3 Longest Substring Without Repeating Characters 区间,想法 难度:1

    https://leetcode.com/problems/longest-substring-without-repeating-characters/ 思路:从某点结束所能取到的最早开头是到目前出 ...

  8. leetcode 3. Longest Substring Without Repeating Characters

    记得这要+1,j = std::max(j,container[s[i]]+1); class Solution { public:int lengthOfLongestSubstring(strin ...

  9. [LeetCode] 3. Longest Substring Without Repeating Characters 题解

    问题描述 输入一个字符串,找到其中最长的不重复子串 例1: 输入:"abcabcbb" 输出:3 解释:最长非重复子串为"abc" 复制代码 例2: 输入:&q ...

最新文章

  1. 我热爱计算机作文500字,电脑吸引了我
  2. HDU_1253 胜利大逃亡(BFS)
  3. Arrays.asList方法总结
  4. 修改ssh默认端口号
  5. 从理念到实践跳跃式演进! 云的原生“免疫系统”如何有机作战?
  6. 数据状态更新时的差异 diff 及 patch 机制
  7. OAuth 2.0介绍
  8. express+mongodb制作简单的文章发布系统(一)之环境的搭建
  9. Linux Mint---开启桌面三维特效
  10. 计算机网络知识点整理
  11. 疯狂程序员网址。。。
  12. js 图片上传时加水印
  13. 412.FizzBuzz
  14. Python实战1-9例:变量、运算、字符串等综合训练
  15. ehviewer_ehviewer1.7.7
  16. 给你推荐一个文件阅读器:Neat Reader
  17. Beta阶段测试报告
  18. “大脑营养剂”给你好睡眠
  19. ffmpeg音视频文件音视频流抽取,初步尝试人声分离
  20. error:LNK2005 “*******”@@3PANA 已经在****.obj中定义的一种情况及解决办法

热门文章

  1. 回溯法基本思想_数据结构之简单的回溯算法
  2. .net源代码已经可以调试
  3. 已经入门了C++,后面的路怎么走?
  4. 21 week4 submit buidAndRun() node-rest-client
  5. 使用Git 管理heroku的项目(windows)
  6. Linux系统状态检测及进程控制--2
  7. STM32的can现场总线实验心得
  8. postfix+postfixadmin+extmail邮件服务器架设过程
  9. 虚拟化部署之Hyper-V虚拟网络配置
  10. 【Python学习系列五】Python网络爬虫框架Scrapy环境搭建