链接:https://www.lintcode.com/problem/386/description

https://mp.weixin.qq.com/s?__biz=MzU2OTUyNzk1NQ==&mid=2247491103&idx=1&sn=8d9a2ed68a7dd31bb2aeb50ffe8fa4c8&source=41#wechat_redirect

题解:

class Solution {
public:/*** @param s: A string* @param k: An integer* @return: An integer*/int lengthOfLongestSubstringKDistinct(string &s, int k) {// write your code hereif (s.size() <= 0) {return 0;}int right = 0;int left = 0;int max_len = 0;std::unordered_map<char, int> table;while (right < s.size()) {++table[s[right]];if (table.size() > k) {while (left <s.size() && table.size() > k) {if (--table[s[left]] == 0) {table.erase(s[left]);}++left;}}max_len = max(max_len, right-left+1);++right;}return max_len;}
};

386 · Longest Substring with At Most K Distinct Characters最多有k个不同字符的最长子字符串相关推荐

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法

    题目解析 代码如下 题目解析 这一题是一道会员的题目,题目介绍如下: Given a string, find the length of the longest substring T that c ...

  6. 【算法】159题 Longest Substring with at Most Two Distinct Characters 最大的子串

    1.概述 给定一个字符串.查找一个最长子序列,这个子序列中重复的字符最多包含2个. 此问题和 [算法]剑指 Offer 48. 最长不含重复字符的子字符串 问题很相似. 2.方案 2.1 双指针 pu ...

  7. LeetCode 340. Longest Substring with At Most K Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...

  8. 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 ...

  9. 【Lintcode】1375. Substring With At Least K Distinct Characters

    题目地址: https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description 给定一 ...

最新文章

  1. SAP PM 初级系列10 - 维护通知单相关的配置
  2. java23中设计模式——结构模式——Composite(组合)
  3. mysql8.0导出带数据的库表_MySQL8.0数据库导出与备份
  4. python实现简单的api接口-使用Python编写API接口和使用API接口
  5. 出错页面webpar的t删除
  6. mysql主从复制深入研究_mysql主从复制原理,深入探讨
  7. wince linux 性能,wince remote call profiler(性能分析)
  8. 五一高铁票难抢?用RPA机器人试试!
  9. 微软发布关于 Surface Pro 3 TPM 绕过漏洞的安全公告
  10. 关于vue中next和Tick(nextTick)的一点理解
  11. 男子造出山寨ATM机 盗信息做假卡取钱13万
  12. ligerui+json_002_Grid用法、属性总结
  13. 丰收互联蓝牙key怎么开机_蓝牙UKEY使用说明
  14. iphonex蓝牙打不开转圈_多人反应iPhone X蓝牙连接时遇到问题
  15. Photoshop安装方法
  16. 实现Pomodoro计时器的Vue组件
  17. android ios 微信 备份通讯录备份通讯录,苹果手机号码怎么备份 微信导入联系人号码...
  18. LeetCode-1225. 报告系统状态的连续日期(困难)
  19. 计算机工程应用是什么期刊,计算机工程类期刊汇总
  20. 安卓文件传输工具 Android File Transfer mac

热门文章

  1. nginx的基本使用和反向代理,地址重写问题
  2. Ubuntu on Windows,在Windows上的Ubuntu
  3. windows下mysql+apache+php配置
  4. (译)如何优化cocos2d程序的内存使用和程序大小:第一部分
  5. #include #import @class 讲解
  6. HAProxy+Hive构建高可用数据挖掘集群
  7. Linux下安装amsn
  8. Source Insight 快捷键大全
  9. 30种应该知道的sql调优方法
  10. 【Java】@ResponseBody 返回JsonObject