[Leetcode] 159. Longest Substring with At Most Two Distinct Characters 解题报告

小白水平理解面试经典题目LeetCode 159. Longest Substring with At Most Two Distinct Characters【Prime】 原题:Given a string s, find the length of the longest substring t that contains at most 2 distinct characters.给定一个字符串 s ,找到最多包含 2 个不同字符的最长子字符串 t 的长度。 阅读详情

题目:

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.

思路:

这道题目也是典型的two pointers:我们定义一个哈希表来表示不同字符的个数,并设定两个索引点start和end,然后从前往后扫描:一旦发现一个新字符,就把它加入哈希表中,并且判断此时哈希表的大小是否超过2,如果超过则更新start的值,直到哈希表的大小不超过2。每次加入一个字符后,都需要更新一下最大长度,最后返回最大长度即可。虽然算法中出现了两重while循环,但我们说时间复杂度依然是O(n)(请思考是为什么?)。关于空间复杂度,由于哈希表的大小最大的时候为3,所以该算法的空间复杂度实际是O(1)。理论上,这里的时间复杂度和空间复杂度肯定已经是最优的了。

代码:

class Solution {
public:
    int lengthOfLongestSubstringTwoDistinct(string s) {
        unordered_map<char, int> hash;
        int max_length = 0;
        int start = 0, end = 0;
        while (end < s.length()) {
            hash[s[end]]++;
            while (hash.size() > 2) {
                hash[s[start]]--;
                if (hash[s[start]] == 0) {
                    hash.erase(s[start]);
                }
                ++start;
            }
            max_length = max(max_length, end - start + 1);
            ++end;
        }
        return max_length;
    }
};
IKM 测试题 - Java SE 8 评估测试题目 阅读详情

相关推荐

模拟IC设计笔记:拆解一个经典的时钟占空比校正(DCC)电路模块,从理论到SPICE仿真

本文深入解析了时钟占空比校正(DCC)电路在模拟IC设计中的关键作用,特别是在高速ADC应用中。通过晶体管级拆解,详细介绍了基于电荷泵和积分器的模拟反馈型DCC电路设计,包括占空比检测、误差放大与校正执行单元的实现。文章还提供了SPICE仿真验证方法,确保电路在100MHz时钟下的稳定性和性能。

weixin_33725722的博客 443

【LeetCode】Longest Substring with At Most Two Distinct Characters

给定一个字符串 s ,找出至多包含两个不同字符的最长子串 t 。 示例 1: 输入: "eceba" 输出: 3 解释: t 是 "ece",长度为3。 示例 2: 输入: "ccaabbb" 输出: 5 解释: t 是 "aabbb",长度为5。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/longest-substrin...

天行健,君子以自强不息 375

人工智能学习之机器人路径规划优化

A* AStar 人工智能 机器人路径规划优化

RongLin02的博客 7082

【Leetcode】159. Longest Substring with At Most Two Distinct Characters

题目地址: https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 给定一个字符串sss,求其最长的只含最多222种字母的子串的长度。 思路是双指针。开两个指针jjj和iii始终保持j≤ij\le ij≤i,并开一个哈希表存s[j:i]s[j:i]s[j:i]之间每个字母出现了多少次。如果当前区间满足要求,则更新答案,继续右移iii,否则说明当前区间含有了超过222种字母,此时jjj及其左边作

数学、算法爱好者的博客 316

AppleAccount注册之生成sign

根据日志发现:签名来自于AuthKit的-[AKAppleIDServerResourceLoadDelegate signRequest:withCompletionHandler:]的一个block,这个库是放在共享缓存库里的,找到提前准备好的IDA文件,先去找主方法,根据分析IDA,最后找到了实际生成签名的方法:t1。,接受五个参数,context(之前讲的获取到的客户端证书),bytes(待加密的body字节),length(字节长度),以及两个接受结果的参数,直接进这个方法看,

2503_90751938的博客 1017

Shopee 虾皮API接口:商品详情采集的实践

随着电商行业的蓬勃发展,数据采集和分析在商业决策中扮演着越来越重要的角色。Shopee作为东南亚领先的电商平台,拥有大量的商品数据。通过Shopee的API接口,我们可以方便地获取商品详情,为数据分析和市场研究提供有力支持。本文将介绍如何使用Shopee API接口采集商品详情,并分享一些实践技巧。提供了丰富的数据接口,包括商品详情、店铺信息、订单数据等。通过API接口,开发者可以轻松地获取Shopee平台上的数据,进行进一步的处理和分析。Shopee API接口:商品详情采集的实践与技巧。

An4480的博客 1903

159. 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.思路分析这题依旧是滑动窗口的思想 要求找到最长的只

J_Shine的博客 379

RK3588开发板DDR频率上不去?手把手教你用ddrbin_tool降频调优(附完整配置流程)

本文详细介绍了RK3588开发板DDR频率调优的完整解决方案,重点使用ddrbin_tool工具进行降频配置。从硬件原理分析到实战操作,包括参数导出、频率调整、二进制生成及烧录验证,帮助开发者解决DDR高频不稳定问题,提升系统可靠性。适用于嵌入式开发、工业控制等场景。

weixin_42713608的博客 557

159. Longest Substring with At Most Two Distinct Characters [Medium]

plus会员题,和904. Fruit Into Baskets一模一样 /** * 滑动窗口法 * 认为'-'不会出现在s中 * 5 ms, 72.82% * 36.8 MB, 94.42% */ class Solution { public int lengthOfLongestSubstringTwoDistinct(String s) { int max = 0, curr = 0, countC2 = 0; // countC2代表两个字符中,最近出现的字符

汋灼的博客 232

热力学仿真软件:Fluent_(8).材料属性设置

在 Fluent 中,材料属性主要包括密度、比热容、导热系数、黏度等物理性质。这些属性在不同的仿真场景中有着不同的作用,例如在流体仿真中,密度和黏度是必不可少的;在传热仿真中,导热系数和比热容是主要考虑的因素。如果所需的材料不在材料库中,或者需要对现有材料进行修改,用户可以自定义材料属性。在主菜单中选择Materials。点击打开材料编辑器。在材料编辑器中,点击Create按钮,输入新的材料名称。选择材料类型(如FluidSolid等)。在属性栏中填写所需的材料属性。

kkchenjj的博客 1506

基于YOLO26/11/v8算法的Web目标检测系统,人脸表情识别系统,Django+Vue3 的前后端分离,实现摄像头实时识别,YOLO26/YOLO11/v8 + LLM大模型智能分析,科研必备

在人工智能迈向通用化(AGI)的今天,“视觉感知 + 语言理解”的多模态联合是未来的趋势。单纯的检测画框已经无法满足复杂的业务需求,如何让系统“看懂”画面并进行“思考”,是当前视觉项目的重点。我们知道目标检测是计算机视觉中的重要任务,广泛应用于安防监控、自动驾驶、智能家居等领域。因此本项目将 YOLO 目标检测模型(兼容 YOLO26/v11/v8 系列)与 DeepSeek 等顶流大语言模型(LLM) 进行联合,使用 Django (后端) + Vue3 (前端) 技术栈

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 2874

[leetcode] 159. Longest Substring with At Most Two Distinct Characters 解题报告

题目链接:https://leetcode.com/problems/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.

小榕流光的专栏 956

【python科研数据处理】将Endnote自动搜索的所有pdf文件,全都移动指定文件夹中

将Endnote自动搜索到的全部pdf文件,全都移动指定文件夹中1 背景2 问题解决 手动反爬虫,禁止转载:原博地址 https://blog.csdn.net/lys_828/article/details/122370892(CSDN博主:Be_melting) 知识梳理不易,请尊重劳动成果,文章仅发布在CSDN网站上,在其他网站看到该博文均属于未经作者授权的恶意爬取信息 1 背景 在进行期末考试周后,面对最后一门功课,需要提交至少80篇SCI参考文献,因此最快下载文献的方式就是通过Web of S

lys_828的博客 1830

leetcode 159. Longest Substring with At Most Two Distinct Characters

Given a string s , find the length of the longest substringtthat contains at most 2 distinct characters. Example 1: Input: "eceba" Output: 3 Explanation: t is "ece" which its length is 3. Exam...

cmy203的博客 190

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

[LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串...

Given a string, find the length of the longest substring T that contains at mostkdistinct characters. For example, Given s =“eceba”and k = 2, T is "ece" which its length is 3. 159. Longest Subs...

weixin_30835923的博客 332

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

[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. 这题的线性解法是维护一个sliding w

┅☆心随风飞 1万+

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

[leetcode]159. Longest Substring with At Most Two Distinct Characters

159. Longest Substring with At Most Two Distinct Characters. 1 暴力法 Time Limit Exceeded class Solution(object): def lengthOfLongestSubstringTwoDistinct(self, s): """ :type s: str ...

CY_TEC的博客 394

leetcode 159: Longest Substring with At Most Two Distinct Characters

Total Accepted: 1167 Total Submissions: 3961 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

西施豆腐渣 2980

[leetcode] 340. Longest Substring with At Most K Distinct Characters 解题报告

题目链接: https://leetcode.com/problems/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 characters.

小榕流光的专栏 6697

Longest Substring with At Most Two Distinct Characters

https://oj.leetcode.com/problems/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

chaochen1407的专栏 487

LeetCode 159. Longest Substring with At Most Two Distinct Characters(最长字串)

原题网址:https://leetcode.com/problems/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.

James Pan 673

LeetCode 340. Longest Substring with At Most K Distinct Characters(最长字串)

原题网址:https://leetcode.com/problems/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 characters.

James Pan 1481

LeetCode 159. Longest Substring with At Most Two Distinct Characters --Java,C++,Python解法

LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大部分题目Java和Python的解法都有。 题目地址:Longest Substring with At Most Two Distinct Characters - LeetCode Given a string s , find the length of the longest sub...

个人博客 1409
上一篇: [Leetcode] 158. Read N Characters Given Read4 II - Call multiple times 解题报告
下一篇: [Leetcode] 160. Intersection of Two Linked Lists 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值