leetcode 3. Longest Substring Without Repeating Characters 最长非重复子串的长度 滑动窗口法
题目链接
根据我们之前介绍的滑动窗口法的解法:
滑动窗口法详解
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 最长非重复子串的长度 滑动窗口法相关推荐
- Leetcode 3:Longest Substring Without Repeating Characters(最长不重复子串)
Description Given a string, find the length of the longest substring without repeating characters. 给 ...
- Longest Substring Without Repeating Characters(最长不重复子序列求解)
问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...
- 【贪心】LeetCode 3. Longest Substring Without Repeating Characters
LeetCode 3. Longest Substring Without Repeating Characters Solution1:我的答案 该方法中哈希表记录的是字符出现的次数.标准的贪心算法 ...
- [LeetCode]3.Longest Substring Without Repeating Characters
[题目] Given a string, find the length of the longest substring without repeating characters. For exam ...
- LeetCode:3. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...
- [LeetCode]--3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- python刷leetcode_零基础python刷leetcode -- 3. Longest Substring Without Repeating Characters
算法很重要,但是每天也需要学学python,于是就想用python刷leetcode 的算法题,和我一起开始零基础python刷leetcode之旅吧.如有不对的地方,希望指正,万分感谢~~ 题目 最 ...
- leetcode 3. Longest Substring Without Repeating Characters | 3. 无重复字符的最长子串(双指针+滑窗)
题目 https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题解 双指针+滑窗,维护一个 set, ...
- LeetCode 3 Longest Substring Without Repeating Characters 区间,想法 难度:1
https://leetcode.com/problems/longest-substring-without-repeating-characters/ 思路:从某点结束所能取到的最早开头是到目前出 ...
最新文章
- 【转】秒杀系统架构分析与实战
- linux命令dmesg查看进程被杀死原因
- 以太网Ethernet解码概述
- 机器学习 感知机算法_0(Matlab实现)
- java jsch shell_如何在字符串中获取jsch shell命令输出
- c#中的一些容易混淆的概念
- 对Javascript异步执行的理解
- 总结过去10年的程序员生涯
- C语言基础练习题初学者可参考
- 电脑里面英文系统的中文简介
- 电脑端如何截屏网页全部内容|网页截长图
- markdown中打勾,对号和打叉,表格内换行
- 永洪BI配置GIS地图的方法
- 微信公众号开发 (1) 微信接入认证成为开发者
- iOS 开发中的 Flux 架构模式
- 智慧工厂数字孪生 数字孪生工厂 工厂数字孪生
- 用计算机进行图片处理课后反思,使用曲线工具画图教学反思
- layui.layer 弹出层使用
- 欧特克开发者训练营(Autodesk DevCamp 2013)的视频教程可以下载
- linux ftp cmd被动模式,如何在Windows命令提示符下使用被动FTP模式?
热门文章
- 如何在团队中做好Code Review
- 2022-2028年中国电动牙刷行业深度调研及投资前景预测报告(全卷)
- 2022-2028年中国复合软管行业市场行情动态及发展趋向分析报告
- 2022-2028年中国废旧塑料回收产业研究及前瞻分析报告
- Python 将字符串转为字典
- scheduled sampling_seq2seq
- AMD–7nm “Rome”芯片SOC体系结构,支持64核
- 分布式深度学习DDL解析
- 2021年大数据Hadoop(一):​​​​​​​Hadoop介绍
- Django 视图URLconf3.1