LeetCode 76.最小覆盖字串

Leetcode 题解 76.最小覆盖字串 Python实现 分析:这个程序需要使用滑动窗口和双指针的方法,因为它需要在字符串s中找到包含字符串t中所有字符的最短子串。通过使用滑动窗口,我们可以在遍历字符串s的过程中,动态地调整窗口的大小,以找到满足条件的子串。具体来说,滑动窗口和双指针的方法可以帮助我们在O(n)的时间复杂度内解决这个问题,因为我们可以通过遍历一次字符串s,不断调整窗口的大小,来找到包含t中所有字符的最短子串。整体上,这个程序的设计思路是使用滑动窗口和双指针的方法,通过不断调整窗口的大小,来找到包含t中所有字符的最短子串。所有字符的最小子串。 阅读详情

1、题目描述

给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。

注意:

  • 对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。
  • 如果 s 中存在这样的子串,我们保证它是唯一的答案。

2、算法思路

        使用滑动窗口+哈希表(数组代替)

        可以学习这题用于计算总类的思路,剩下就是滑动窗口。

        LeetCode 904.水果成篮-优快云博客

3、算法代码

class Solution {
       public String minWindow(String S, String T) {
        char[] s = S.toCharArray();
        char[] t = T.toCharArray();

        int[] hash1 = new int[128];  //hash1是记录t中的字符,128根据ASCII表
        int[] hash2 = new int[128];  //hash2是记录s中的字符
        int kind = 0;
        for (char ch : t) {
            if (hash1[ch]++ == 0) {
                kind++;//统计字符的种类
            }
        }
        int min_length = Integer.MAX_VALUE, begin = -1;
        for (int left = 0, right = 0, count = 0; right < s.length; right++) {
            char into = s[right];
            if (++hash2[into] == hash1[into]) count++;//进窗口,达到和t中的一样字符的个数,count才可以++
            while (count == kind) {
                if (right - left + 1 < min_length) {//更新结果
                    min_length = right - left + 1;
                    begin = left;
                }
                char out = s[left++];
                if (hash2[out]-- == hash1[out]) count--;//出窗口
            }
        }
        if(begin==-1)return new String();
        else return S.substring(begin,begin+min_length);
    }
}

4、运行结果

Nature风格的北半球投影图,几行代码就能绘制... 在子刊***Nat. Clim. Chang.***中,通过北极俯视投影表示NAO和AO的EOF分解。仅在定义图层的时候设置为该投影,其余设置content,绘制contourf都是PlateCaree。如子刊Nature Geosci中表示了海冰的漂移,还添加了DEM底图,非常美观。以海温数据为例,ERA5月温度平均,是一张721*1440的0.25degTIF。现在的结果看起来不错,但太大了,我们如果只需要北半球的数据,则需要指定经纬度。绘制完结果也是正方形,和ArcGIS差不多,见之前的推文。 阅读详情

相关推荐

【主力资金密码】副图指标升级版使用技术图文案例分解,通达信软件炒股指标公式代码

(1)主力资金线在低位时,对应的副图指标显示柱线是绿色,这个阶段主力资金参与小,行情处于下跌趋势或弱势震荡。当然,如果结合主图指标【翻倍密码系统】操作,主图的两个买卖点以及K线均线变色跟踪功能,更多的技术支撑,多空更加清晰,共振做多或多头更加确定和安全。如上图标记,中档虚线是参考安全操作的一个区间划分,粉色阶段做多或持股为主,为操作增加一道安全保护。如上图,副图指标【主力资金密码】更新叠加操作区间背景,绿色阶段持币或做空,粉色阶段做多或持股。如上图,副图指标,标记的两个区间,绿色区间时做空或持币。

weixin_69760830的博客 2563

双指针算法之滑动窗口 | 力扣76.最小覆盖字串

本文将通过解决 力扣76.最小覆盖字串 问题,讲解滑动窗口的原理与实现 76.最小覆盖字串 题目:给定字符串 S 以及字符串 T ,求 S 种 包含 T 的最短连续子字符串的长度 要求 时间复杂度 ≤ O(n)

比特的一天 457

【Python&RS】哨兵2号(Sentinel2)轨道条带说明+图幅统计(表格+矢量)

今天给大家带来哨兵2号的图幅编号介绍以及全国省、市所需图幅数的统计。网上教程极少,而且结果也不公开。因为每次下载的时候都不知道需要几幅,所以研究后给大家分享一下。

Remote Sensing迷途小书童 1686

【Leetcode hot 100】76.最小覆盖字串

文章摘要: 本文详细解析了LeetCode 76题“最小覆盖子串”的滑动窗口解法。通过哈希表(或数组)统计字符需求,用双指针动态维护窗口,当窗口包含目标串所有字符时缩小窗口寻找最短子串。关键点包括:用valid标记已满足的字符种类、数组优化哈希表访问、窗口缩小时机判断。Java代码实现了O(n)时间复杂度的解法,适用于英文字符串场景。练习建议尝试用HashMap对比性能差异,深入理解滑动窗口的优化逻辑。

专注Java和MySQL领域知识分享 1101

白盒测试逻辑覆盖之谓词覆盖、子句覆盖、三类有效字句覆盖等知识点

本篇笔记会简化书本上的概念,用容易理解的方式来说明,有些例子打字麻烦,我会直接放图片! 目录 基础知识: 谓词覆盖predicate coverage 子句覆盖 clause coverage 组合覆盖 combinatorial coverage 有效字句覆盖Active Clause Coverage 广义有效子句覆盖(General Active Clause Coverage) 相关性有效子句覆盖(Correlated Active Clause Coverage) 限制性有效

weixin_46019681的博客 2668

三种 绘制奈奎斯特曲线 的方法

如何绘制Nyquist曲线 提供三种绘制方法,其中使用最广泛的是概略图绘制方法 ①解析法绘制 ②概略图法绘制 ③MATLAB绘制

weixin_46664967的博客 6万+

力扣76题,最小覆盖字串

力扣76题,最小覆盖字串,滑动窗口,hash表

sxycylq的博客 186

Linux系统之部署经典魔塔小游戏

Linux系统之部署经典魔塔小游戏

jks212454的博客 1825

LeetCode 76 最小覆盖字串

最小窗口子字符串问题,使用C++的滑动窗口方法解决方案。这个算法有效地在给定字符串中找到包含另一个字符串所有字符的最小子字符串。滑动窗口技术能够在线性时间复杂度内解决这个问题,是实际应用的有效解决方案。最小窗口子字符串问题是一个经典的算法问题,我们的任务是在给定字符串中找到包含另一个字符串所有字符的最小子字符串。在本文中,我们将探讨使用C++的滑动窗口方法解决这个问题。我们将使用滑动窗口技术来高效地解决这个问题。,我们需要找到字符串。所有字符的最小窗口。

宁子希的博客 401

AHB2APB_bridge 设计

此处研究AHB Slave到 APB master的桥接器,实现接口转化,例如下图之后是参数描述

Starry__的博客 2199

基于STM32单片机的智能分拣系统

基于STM32单片机的智能分拣系统是一种高效、精准的自动化分拣解决方案,适用于物流、仓储、制造业等领域。该系统利用STM32单片机作为核心控制器,结合传感器技术、电机控制算法和通信模块,实现对物品的自动识别、分类和分拣。通过智能化的设计,该系统能够大幅提高分拣效率,降低人工成本,并减少错误率。基于STM32单片机的智能分拣系统是一种高效、精准、低成本的自动化解决方案,能够广泛应用于物流、制造、环保等领域。

qq_62634009的博客 1940

LeetCode第76题----最小覆盖字串

本题是LeetCode第76题,官方定义的难度系数为Hard,但是题目很有意思,这是我第一次遇到我自己的暴力解法和官方的优秀解法相差不大的题目。

F & F的博客 1814

【AI大模型应用开发】教你用AI轻松将你的知识库变为知识图谱(附完整代码,直接运行)

本文我们来介绍如何使用AI,将自己的知识库自动转换为知识图谱,并进行可视化展示。

同学小张的博客 3930

leetcode 76最小覆盖字串

【代码】leetcode 76最小覆盖字串。

m0_58182797的博客 301

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

刷题Leetcode76————最小覆盖字串

Leetcode最小覆盖字串 在学习labuladong算法以后尝试手写出该题的题解,但是却出现了如下的情况,最后一个测试用例没有通过,其实这是一个很简单的一个错误。代码书写不规范导致的 Ps:首先我对HashMap进行初始化,key为character,value为Integer,问题就出来在了Integer上面。两个引用数据类型要用equals(),一个Integer,一个int的话可以用==,因为会自动拆箱. public String minWindow(String s, String t)

日常分享学习的轨迹 186

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

力扣76.最小覆盖字串

题目中允许字串中的字符频率>=目标串t的字符频率,在向右扩展的同时为尝试缩短字符串长度,判断窗口左端的字符频率是否大于目标串t的所需频率,大于则弹出,(在字串第一次与目标串t匹配后)该步骤始终保证窗口内字串与目标串t匹配。思路:使用滑动窗口+字符频率数组计算curr_count(当前满足频率需求的字符个数),当curr_count==required__count时,窗口内字串与目标串t匹配;最小覆盖子串 "BANC" 包含来自字符串 t 的 'A'、'B' 和 'C'。如果没有这样的子串,返回空字符串。

fpfpanda的博客 44

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

leetcode76:最小覆盖字串

给定两个字符串。

D2510466299的博客 606

leetcode.76:最小覆盖字串

给你一个字符串 S、一个字符串 T,请在字符串 S 里面找出:包含 T 所有字符的最小子串。 示例: 输入: S = “ADOBECODEBANC”, T = “ABC” 输出: “BANC” 说明: 如果 S 中不存这样的子串,则返回空字符串 “”。 如果 S 中存在这样的子串,我们保证它是唯一的答案。 通过次数57,799提交次数152,286 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/minimum-window-substring 著作权

janyoo的博客 257

LeetCode系列76—最小覆盖字串

题意 76. 最小覆盖子串 题解 方法一:滑动窗口 在滑动窗口类型的问题中都会有两个指针,一个用于「延伸」现有窗口的 r 指针,和一个用于「收缩」窗口的 l 指针。在任意时刻,只有一个指针运动,而另一个保持静止。我们在s上滑动窗口,通过移动 r 指针不断扩张窗口。当窗口包含 t 全部所需的字符后,如果能收缩,我们就收缩窗口直到得到最小窗口。 时间复杂度:O(|S|+|T|),这里|S|表示字符串S的长度,|T|表示字符串T的长度 空间复杂度:O(|S|+|T|) 相同类型的题还有LeetCode中的3、20

fkuner的博客 179

Leetcode 76 最小覆盖字串

给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。 注意: 对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。 如果 s 中存在这样的子串,我们保证它是唯一的答案。 示例 1: 输入:s = "ADOBECODEBANC", t = "ABC" 输出:"BANC" 示例 2: 输入:s = "a", t = "a" 输出:"a" 示例 3: 输入: s = .

qq_37637818的博客 119

最小覆盖字串—leetcode76

难度困难441 给你一个字符串 S、一个字符串 T,请在字符串 S 里面找出:包含 T 所有字母的最小子串。 示例: 输入: S = "ADOBECODEBANC", T = "ABC" 输出: "BANC" 说明: 如果 S 中不存这样的子串,则返回空字符串""。 如果 S 中存在这样的子串,我们保证它是唯一的答案。 思路: 1.首先遍历一遍t,用map记录每个字母出现的次数。...

独行侠 258

Leetcode 76.最小覆盖字串

所有字符的子串,则返回空字符串。所有字符的最小子串。

技术记录 296

LeetCode76. 最小覆盖字串

LeetCode76. 最小覆盖字串 给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 “” 。 注意: 对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。 如果 s 中存在这样的子串,我们保证它是唯一的答案。 // 滑动窗口双指针 // 先移动右指针,直到窗口内包含所有目标字符;当窗口已包含所有目标字符时,移动左指针,寻找窗口的最短长度; #include<string>

m0_56077202的博客 180

leetcode76 最小覆盖字串

详细解答见:https://leetcode-cn.com/problems/minimum-window-substring/solution/hua-dong-chuang-kou-suan-fa-tong-yong-si-xiang-by-/

weixin_42809993的博客 199

LeetCode第七天之最小覆盖字串76

滑动窗口之加法 public static String minWindow(String s, String t) { int sLen=s.length(); int tLen=t.length(); //处理特殊情况 if (sLen==0||tLen==0||sLen<tLen) return ""; char[] charArrayS=s.toCharArray(); .

fanzhe的博客 277

每日一题:Leetcode-76 最小覆盖字串

力扣题目解题思路java代码。

LIUCHANGSHUO的博客 415
上一篇: LeetCode 30.串联所有单词的字串
下一篇: LeetCode 704.二分查找
我是黄骨鱼
博客等级 码龄4年 180粉丝 38原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值