leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法
- 题目解析
- 代码如下
题目解析
这一题是一道会员的题目,题目介绍如下:
Given a string, find the length of the longest substring T that contains at most 2 distinct characters.For example, Given s = “eceba”,T is "ece" which its length is 3.
意思的大概情况是找出一个字符串中不相同字符不超过两个的最长子串
根据我们之前介绍的滑动窗口法的解法:
滑动窗口法详解
leetcode 438. Find All Anagrams in a String 滑动窗口法
leetcode 3. Longest Substring Without Repeating Characters 最长非重复子串的长度 滑动窗口法
此题与leetcode第三题,相似非常高,可以先做第三题再做这一题:
代码如下
from collections import defaultdictclass Solution:def lengthOfLongestSubstringTwoDistinct(self, s):begin, end, counter, length = 0, 0, 0, 0map_dict = defaultdict(int)while end < len(s):c = s[end]map_dict[c] += 1# counter 表示新的字符if map_dict == 1:counter += 1end += 1# 如果新字符的数量大于2,就开始移动窗口while counter > 2:char_tmp = s[begin]if map_dict[char_tmp] == 1:counter -= 1map_dict[char_tmp] -= 1begin += 1length = max(length, end - begin)return length
leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法相关推荐
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串...
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串...
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- LeetCode 159. Longest Substring with At Most Two Distinct Characters --Java,C++,Python解法
题目地址:Longest Substring with At Most Two Distinct Characters - LeetCode Given a string s , find the l ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串...
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- Longest Substring With At Most K Distinct Characters
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- LeetCode Longest Substring with At Least K Repeating Characters(递归)
问题:给出一个字符串s,要求求出最长的子串,每个字符出现至少k次 思路: 方法一,先统计每个字符出现的次数,然后分别从头和尾找出出现资料最小为k的位置,然后再此区间遍历,当出现小于k次时,在两个子区间 ...
- 【算法】159题 Longest Substring with at Most Two Distinct Characters 最大的子串
1.概述 给定一个字符串.查找一个最长子序列,这个子序列中重复的字符最多包含2个. 此问题和 [算法]剑指 Offer 48. 最长不含重复字符的子字符串 问题很相似. 2.方案 2.1 双指针 pu ...
- 386 · Longest Substring with At Most K Distinct Characters最多有k个不同字符的最长子字符串
链接:https://www.lintcode.com/problem/386/description https://mp.weixin.qq.com/s?__biz=MzU2OTUyNzk1NQ= ...
最新文章
- 不降低scipy版本解决AttributeError: module ‘scipy.misc‘ has no attribute ‘imresize‘问题
- numpy逻辑运算符
- 编译原理词/语法分析
- ajax 返回数组某个属性值,js中sort方法根据数组中对象的某一个属性值进行排序...
- java对cpu线程的要求_java-CPU份额对线程有什么影响
- linux 下删除文件夹(文件夹不为空时)
- 节点式光端机与点对点式光端机的区别
- 【iCore3 双核心板_FPGA】例程十二:Modelsim仿真实验
- 开放,从容不凡,携手迈进容器应用时代 ——2020容器云职业技能大赛正式开赛
- Google Earth的十个常用技巧应用
- 使用mysqldump 导出sql数据
- 万字长文:解读区块链7类共识算法
- iptables转发基础
- java web初始化实例_Struts2中实现web应用的初始化实例详解
- [转载]Mysql导出表结构及表数据 mysqldump用法
- 类和对象之Scala程序
- Asp.Net MVC部暑托管服务器iis7提示403错误解决方法
- python对原数组遍历_Python数组遍历的简单实现方法小结
- android sdk环境配置_Mac 配置adb环境的方法
- 数据分析引擎之Kylin学习笔记
热门文章
- Go 知识点(17)— go 工具链 go test 使用
- Redis 笔记(03)— string类型(设置key、获取key、设置过期时间、批量设置获取key、对key进行加减、对key值进行追加、获取value子串)
- tornado 异步两种实现形式 通过回调可以利用
- CPU/GPU/TPU/NPU...XPU都是什么意思?
- 适用于Windows和Linux的Yolo-v3和Yolo-v2(上)
- 计算机视觉系列最新论文(附简介)
- 2021年大数据Flink(十四):流批一体API Connectors JDBC
- HarmonyOS Java工程目录结构
- Web Service 安全性解决方案(SOAP篇)
- C语言解析pcap文件得到HTTP信息实例(原创,附源码)