题目链接

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

from collections import defaultdictclass Solution:def lengthOfLongestSubstring(self, s):begin, end , counter, d = 0, 0, 0, 0map_dict = defaultdict(int)while end < len(s):c = s[end]map_dict[c] += 1# counter表示重复字符的个数if map_dict[c] > 1:counter += 1end += 1# 将begin一直移到第一个重复字符的位置while counter > 0:char_tmp = s[begin]if map_dict[char_tmp] > 1:counter -= 1map_dict[char_tmp] -= 1begin += 1# 每次计算下当前子串长度d = max(d, end - begin)return d

提供一种更短的解法:

class Solution:def lengthOfLongestSubstring(self, s):dic, res, start, = {}, 0, 0for i, ch in enumerate(s):if ch in dic:start = max(start, dic[ch]+1)res = max(res, i-start+1)dic[ch] = ireturn res

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

  1. Leetcode 3:Longest Substring Without Repeating Characters(最长不重复子串)

    Description Given a string, find the length of the longest substring without repeating characters. 给 ...

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

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

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

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

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

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

  5. LeetCode:3. Longest Substring Without Repeating Characters

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

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

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

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

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

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

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

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

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

最新文章

  1. 【转】秒杀系统架构分析与实战
  2. linux命令dmesg查看进程被杀死原因
  3. 以太网Ethernet解码概述
  4. 机器学习 感知机算法_0(Matlab实现)
  5. java jsch shell_如何在字符串中获取jsch shell命令输出
  6. c#中的一些容易混淆的概念
  7. 对Javascript异步执行的理解
  8. 总结过去10年的程序员生涯
  9. C语言基础练习题初学者可参考
  10. 电脑里面英文系统的中文简介
  11. 电脑端如何截屏网页全部内容|网页截长图
  12. markdown中打勾,对号和打叉,表格内换行
  13. 永洪BI配置GIS地图的方法
  14. 微信公众号开发 (1) 微信接入认证成为开发者
  15. iOS 开发中的 Flux 架构模式
  16. 智慧工厂数字孪生 数字孪生工厂 工厂数字孪生
  17. 用计算机进行图片处理课后反思,使用曲线工具画图教学反思
  18. layui.layer 弹出层使用
  19. 欧特克开发者训练营(Autodesk DevCamp 2013)的视频教程可以下载
  20. linux ftp cmd被动模式,如何在Windows命令提示符下使用被动FTP模式?

热门文章

  1. 如何在团队中做好Code Review
  2. 2022-2028年中国电动牙刷行业深度调研及投资前景预测报告(全卷)
  3. 2022-2028年中国复合软管行业市场行情动态及发展趋向分析报告
  4. 2022-2028年中国废旧塑料回收产业研究及前瞻分析报告
  5. Python 将字符串转为字典
  6. scheduled sampling_seq2seq
  7. AMD–7nm “Rome”芯片SOC体系结构,支持64核
  8. 分布式深度学习DDL解析
  9. 2021年大数据Hadoop(一):​​​​​​​Hadoop介绍
  10. Django 视图URLconf3.1