[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 characters.
For example, Given s = “eceba”
and k = 2,
T is "ece" which its length is 3.
这道题是之前那道Longest Substring with At Most Two Distinct Characters的拓展,而且那道题中的解法一和解法二直接将2换成k就行了,具体讲解请参考之前那篇博客:
解法一:
class Solution { public:int lengthOfLongestSubstringKDistinct(string s, int k) {int res = 0, left = 0;unordered_map<char, int> m;for (int i = 0; i < s.size(); ++i) {++m[s[i]];while (m.size() > k) {if (--m[s[left]] == 0) m.erase(s[left]);++left;}res = max(res, i - left + 1);}return res;} };
具体讲解请参考之前那篇博客Longest Substring with At Most Two Distinct Characters,参见代码如下:
解法二:
class Solution { public:int lengthOfLongestSubstringKDistinct(string s, int k) {int res = 0, left = 0;unordered_map<char, int> m;for (int i = 0; i < s.size(); ++i) {m[s[i]] = i;while (m.size() > k) {if (m[s[left]] == left) m.erase(s[left]);++left;}res = max(res, i - left + 1);}return res;} };
类似题目:
Longest Substring with At Most Two Distinct Characters
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串...相关推荐
- [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] 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 ...
- leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法
题目解析 代码如下 题目解析 这一题是一道会员的题目,题目介绍如下: Given a string, find the length of the longest substring T that c ...
- 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= ...
- 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 ...
- 【算法】159题 Longest Substring with at Most Two Distinct Characters 最大的子串
1.概述 给定一个字符串.查找一个最长子序列,这个子序列中重复的字符最多包含2个. 此问题和 [算法]剑指 Offer 48. 最长不含重复字符的子字符串 问题很相似. 2.方案 2.1 双指针 pu ...
- Leetcode每日必刷题库第3题,如何实现无重复字符的最长子串?
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例: 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 &qu ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- 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 ...
最新文章
- 赠书 | 手把手教你自己动手打造一个智能恒温器
- 小型自动化运维--expect脚本之自动同步
- Hadoop应用实战100讲(二)-Hadoop常用命令汇总
- android引用另一个项目作为library错误
- oracle导入报错39002,oracle impdp ORA-39002
- 一起学习C语言:数组(二)
- 学习云计算学哪种编程语言_您应该学习哪种编程语言?
- python中split拆分数组_Python 数组分割
- Spring Boot ES 实战,直接拿来用!
- 精进 Quartz—Quartz大致介绍(一)
- 算法第四版 高清完整中文版(非纸质版)
- 2000~2022年Java学习笔记
- 分享两款雪花特效代码
- Android Update Engine 分析(十七)10 类 InstallOperation 数据的生成和应用
- poj1125 Stockbroker Grapevine Floyd算法
- 解决ios下的微信页面背景音乐无法自动播放问题
- 信息安全系统设计基础期中总结
- 无盘服务器网线接法,无盘经验:解析网吧千兆网线的标准做法
- win10快捷方式变成空白图标怎么办?
- WP Smush Pro网站图片优化插件最新版免费下载
热门文章
- IE6,7,8,FF兼容总结
- 防止IIS文件被下载方法
- 死锁问题分析的利器——valgrind的DRD和Helgrind
- Google Test(GTest)使用方法和源码解析——预处理技术分析和应用
- Python3中参数*args和**kwargs介绍
- Ubuntu14.04 64位上配置终端显示git分支名称
- linux哪个版本支持多线程,关于Linux操作系统的叙述错误的是()A、Linux是多用户、多任务、支持多线程的操作系统B、Linux的源...
- qt发布后 mysql数据库_qt发布后 mysql数据库
- php多线程查表,php curl_multi 多线程查询的例子
- 哪个工具编译java_用什么工具来编译JAVA啊?