当前位置:网站首页>Leetcode 159 Longest substring containing at most two different characters (2022.06.08)
Leetcode 159 Longest substring containing at most two different characters (2022.06.08)
2022-06-09 11:27:00 【ChaoYue_ miku】
Given a string s , find at most The longest substring containing two different characters t , And returns the length of the substring .
Example 1:
Input : “eceba”
Output : 3
explain : t yes “ece”, The length is 3.
Example 2:
Input : “ccaabbb”
Output : 5
explain : t yes “aabbb”, The length is 5.
Pass times 23,666 Submit the number 42,557
source : Power button (LeetCode)
link :https://leetcode.cn/problems/longest-substring-with-at-most-two-distinct-characters
Method 1 : The sliding window
C++ Submission :
class Solution {
public:
int lengthOfLongestSubstringTwoDistinct(string s) {
int len = s.size();
int l = 0, r = 0, ch = 0, ans = 0;
unordered_map<char, int> window;
while(r < len){
char ch1 = s[r];
if(window[ch1] == 0){
ch++;
}
window[ch1]++;
while(ch > 2){
char ch2 = s[l++];
window[ch2]--;
if(window[ch2] == 0){
ch--;
}
}
ans = max(ans, r - l + 1);
r++;
}
return ans;
}
};
边栏推荐
猜你喜欢

web开发重点,简单开发web

基于任务调度的企业级分布式批处理方案

What are the application advantages of 3D digital sand table display

Simple example of C # picture verification code

环糊精-金属有机骨架负载低分子肝素及阿霉素(MOF金属有机骨架负载生物大分子药物)

你知道多少,深度解析,值得收藏

Perfdog releases new indicators, tailored to the game

对象的实例化和访问

终于有人把大数定律讲明白了

Daily question -1232 Dotted line
随机推荐
Data asset management: how to manage the data assets of an enterprise?
MOFs, metal organic framework materials of folate ligands, are loaded with 5-fluorouracil, sidabelamine, taxol, doxorubicin, daunorubicin and other drugs
NFT market has entered the era of aggregation, and okaleido has become the first aggregation platform on BNB chain
Introduction to automatic partition management of gaussdb (DWS)
[email protected]负载5-氟尿嘧啶|[email protected]装载5
More than observation | Alibaba cloud observable Technology Summit officially launched
Baidu post-90s programmers were sentenced for deleting and modifying databases, saying they were dissatisfied with their leaders
中金财富开户安全吗
Document writing specification
处理链加载数据出错的可能原因-process chain loading error
[basic knowledge] ~ zener diode, triode, amplification circuit, number of logic gate transistors, FPGA device junction temperature range, FPGA loading mode, Schmitt trigger, C language structured prog
MySQL learning notes - Chapter 5 - data backup and recovery, MySQL logs
基于任务调度的企业级分布式批处理方案
Cyclodextrin metal organic framework( β- Cd-mof) loaded with dimercaptosuccinic acid( β- CD-MOF/DMSA) β- Drug loading mechanism of cyclodextrin metal organic framework
In modern society, people are more and more dependent on semiconductor products
环糊精-金属有机骨架负载低分子肝素及阿霉素(MOF金属有机骨架负载生物大分子药物)
使用五数概括法来确定数据集中的孤立点
計網 | OSI模型中各層單比特
idea取消数据简化
Network planning | units of each layer in OSI model