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

74hc595驱动模块的Verilog设计(线性状态机) module hc595_driver( input clk, //50Mhz input rst_n, //复位信号 input [15:0] data, //数码管待显示数据,8位段选数据,8位位选数据 input en, //使能 o... 阅读详情

原题链接:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/

这也是一道带锁题,已经解锁的朋友可以选择做一做。这道题其实就是经典的窗口移动题,感觉难度其实是Medium。题目的要求是对于输入的字符串,要求返回只包含两个字母的子串最长的长度。

解法就是我们维护一个窗口的2个指针,一个指向窗口左端,一个指向窗口右端。然后右边的指针不断向右移动,如果窗口里的子串包含的字符种类数大于2种,则向右移动窗口的左指针,直到窗口重新恢复到包含字符种类数为2为止。时间复杂度是O(n)

代码如下:

public int lengthOfLongestSubstringTwoDistinct(String s) {
        if(s.length() == 0)
            return 0;
        int count = 0;
        HashMap<Character,Integer> map = new HashMap<>();
        int start = 0;
        int len = 0;
        for(int i = 0;i<s.length();i++)
        {
            char c = s.charAt(i);
            if(map.containsKey(c))
            {
                map.put(c,map.get(c)+1);
            }
            else
            {
                map.put(c,1);
            }
            if(map.get(c)==1)
                count++;
            while(count>2)
            {
                char c2 = s.charAt(start);
                map.put(c2,map.get(c2)-1);
                if(map.get(c2)<=0)
                {
                    count--;
                }
                start++;
            }
            len = Math.max(i-start,len);
        }
        return len+1;
    }
类似的窗口移动题目还有很多,如 Longest Substring Without Repeating Characters 这道题,Code Ganker 已经给出了非常好的解决方案了。有关窗口移动题目思路都是一个模式,朋友们自己去总结哈。

如何解决Windows系统中缺失oem*.inf文件导致的软件运行问题 本文详细解析了Windows系统中因oem*.inf文件缺失导致软件无法运行的常见问题。文章阐述了oem*.inf文件作为安装信息文件的重要作用,并提供了从精准定位文件路径、安全获取与替换文件的手动修复步骤,到利用专业工具进行一站式修复的完整解决方案。最后,还给出了从软件安装来源、卸载习惯到系统维护等方面的预防措施,帮助用户彻底避免此类问题。 阅读详情

相关推荐

利用python语言进行相机标定

利用python语言进行相机标定 目录利用python语言进行相机标定一、相机标定相关原理1.相机标定简介2.坐标映射2.1图像坐标系1)图像坐标系(Pixel coordinate system)2)成像平面坐标系(Retinal coordinate system)3)摄像机坐标系(Camera coordinate system)4)世界坐标系(World coordinate system)二、相机标定算法流程1.镜头畸变1.1径向畸变1.2切向畸变2.相机标定算法求解外参内参和单应矩阵关系求解(张

qq_44818951的博客 4707

【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

在Matlab中开发AutoSAR模型的流程(自下而上)

前面介绍了AutoSAR软件组件开发的两种方式,其中自下向上的方式,是从模型出发,流程如下。

小胡同学的工程师学习之路 5373

【LeetCode - 159】至多包含两个不同字符的最长子串

【中等】哈希、双指针、字符串、滑动窗口

学哥斌的博客 3577

论文理解【LLM-OR】—— 【PaMOP】Guiding Large Language Models in Modeling Optimization Problem

针对 LLM 求解运筹优化任务,PaMOP 通过约束划分树分离子问题,实现复杂优化问题的分块建模,并结合求解器反馈与反向翻译一致性检查迭代纠错,降低子任务复杂度,提升可执行率与建模准确性。

佚失的诗篇 1016

让 AI 学会“慢思考”:MCP Sequential Thinking 深度部署与使用指南

摘要: MCP Sequential Thinking 是一种让AI具备"慢思考"能力的技术,通过动态调整思维路径解决复杂问题。本文详细介绍了在Windows、macOS和Linux系统下的部署方法,包括环境准备、配置文件修改等步骤。该技术通过"产生想法-评估需求-修正路径"的循环机制,显著提升AI在代码设计、数学证明等复杂任务中的表现。文章还提供了验证安装方法、使用示例和常见问题解答,帮助开发者解锁AI的深层思考能力。(149字)

weixin_43236925的博客 1927

leetcode 第 159 题: 至多包含两个不同字符的最长子串 (C++)

int findSubstring(string s) { unordered_map<char, int> map; int len = 0; int left = 0, right = 0; while (right < s.size()){ auto c = s[right]; ++right; ++map[c]; while (map.size() > 2){ auto d = s[left]; ++left; if (map[d] > 1) --map[d]; els

zj 1004

goahead(嵌入式Web服务器)之文件传输篇

goahead文件上传原理: 使用html form即表单提交文件上传请求,web服务器核心处理接收客户端Post过来的文件数据(注意post的是二进制数据),最后,web服务器把接收到文件数据以二进制格式写到服务器本端存储系统。 goahead文件上传步骤: 1、创建upload.html文件,内容如下: <html> <head> <meta charset="utf-8"/> <title>Upload</title> </head&g

weixin_40732273的博客 3499

LeetCode 159. 至多包含两个不同字符的最长子串

满足题目要求的子串是 "aabbb" ,长度为 5。满足题目要求的子串是 "ece" ,长度为 3。的最长子串,并返回该子串的长度。

m0_56090828的博客 643

Codex 自定义模型操作指南:CLI 与桌面客户端双端实操

检查版本 codex --version # 预期输出类似:codex-cli 0.133.0-alpha.1 # 如未安装 npm install -g @openai/codex假设你要接入DeepSeek,在# 临时设置(当前终端会话有效)export DEEPSEEK_API_KEY="sk-你的 DeepSeek API Key"# 永久设置(推荐,写入 shell 配置)

篮球界最会写bug的人;程序猿中打球最菜的人。 476

OPTEE v4.4.0 qemu_v8环境搭建(支持Hafnium)

一、前提条件 二、拉取代码 三、下载工具链 四、修改代码 五、编译 六、运行

AI-安全-功耗 896

LeetCode 1590. Make Sum Divisible by P【前缀和,哈希表,数论】中等

Given an array of positive integers nums , remove the smallest subarray (possibly empty) such that the sum of the remaining elements is divisible by p . It is not allowed to remove the whole array. Return the length of the smallest subarray that you need t

memcpy0的博客 884

YOLOv11改进 ,YOLOv11改进主干网络为MobileNetV3,助力涨点

MobileNetV3通过结合硬件感知网络架构搜索(NAS)和NetAdapt算法,通过新颖的架构改进进一步提升了性能。本文开始探讨了自动化搜索算法与网络设计如何协同工作,以利用互补方法来提升整体技术水平。通过这一过程,创建了两个新的MobileNet模型:MobileNetV3-Large和MobileNetV3-Small,分别针对高资源和低资源使用场景。这些模型随后被适配并应用于目标检测和语义分割任务。

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

Leetcode 159 Longest Substring with At Most Two Distinct Characters

思路: sliding window。这道题又是最长substring,所以我想到了昨天刷题有人说这类题目可以用sliding window algorithm来解决。今天尝试了一下,应该也算是用这个算法做出来。sliding window主要就是,有两个指针,左右指针。每次操作有且只有一个指针会移动。换句话说,每次操作要么左指针移动右指针不动,要么左指针不动右指针移动。开始的时候左右指针都在0位置,一般来说都是先移动右指针来先找到一个满足条件的substring。然后这时候右指针不动了,左指针开始移动,.

GoodJobJasper的博客 280

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

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

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

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

LeetCode159传送门 解法: 1. Brute Force方法。构建字符串S的所有可能子串,为每个子串,构建一个set,set的大小可以表征子串中字符的类别数。假设字符串长度为n,则子串个数为,时间复杂度为,空间复杂度为。 2. 使用三个指针。一个指针k指向遍历字符串的主循环中的字符位置,一个指针j指向子串的上一个不同种类字符位置,一个指针i指向当前合法子串的起始位置。当k位置字符与...

weixin_42344158的博客 247

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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 2 distinct characters. Example 1: Input: "eceba" Output: 3 Explanation: t is "ece" which its length is 3. Example 2: Input: "ccaabbb...

叶现一的专栏 225

LeetCode159:Longest Substring with At Most Two Distinct Characters

Given a strings, find the length of the longest substringtthat containsat most2 distinct characters. Example 1: Input: "eceba" Output: 3 Explanation: tis "ece" which its length is 3. Examp...

卷卷萌的博客 251

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

找到最多含有两个不同字符的子串的最长长度。例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置。初始p1为0. p2为-1.start初始化为0,表示两种字符串的开头。 只要遍历一次string就可以得到结果了。 首先我们要确定p2的值,那么i要一直到不等于s[p1]的值为止,那么位置就是p2了。 然后继续往后如果来一...

117

LeetCode 159. Longest Substring with At Most Two Distinct Characters

#include #include #include #include using namespace std; /* Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example: Given s = "e

ActiveCoder的博客 418

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

Leetcode-159.Longest Substring with At Most Two Distinct Characters

Problem DescriptionGiven 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. Analysis: We

Tigers Den 482

Leetcode 159. Longest Substring with At Most Two Distinct Characters

Longest Substring with At Most K Distinct Characters 把count初始设为K就可以 public int lengthOfLongestSubstringTwoDistinct(String s) { Map<Character, Integer> map = new HashMap<>(); ...

Belle_Chou的博客 188

[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

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 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
foolnote
博客等级 码龄11年 0粉丝 10原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值