-
问题描述
- 给定一个字符串
s,请你找出其中不含有重复字符的 最长子串 的长度。
- 给定一个字符串
-
测试用例
示例1
- 输入: s = “abcabcbb”
- 输出: 3
- 解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
示例2
- 输入: s = “bbbbb”
- 输出: 1
- 解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。
示例 3:
- 输入: s = “pwwkew”
- 输出: 3
- 解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。
- 请注意,你的答案必须是 子串 的长度,“pwke” 是一个子序列,不是子串。
示例 4:
- 输入: s = “”
- 输出: 0
示例5
- 输入:s=“ ”
- 输出:1
示例6
- 输入:s=“ ”
- 输出:1
-
补充说明
-
0 <= s.length <= 5 * 104
-
s由英文字母、数字、符号和空格组成
-
-
解题报告
主要思路是使用滑动窗口,分别使用start、end 指针,前提是保证两个指针单调递增,剩余逻辑就是处理指针移动的逻辑。
java代码如下:
public int lengthOfLongestSubstring(String s) { int start = 0; int end = 0; int res = 0; //记录出现的位置 int[] array = new int[128]; while (end < s.length()) { int index = s.charAt(end); //移动start指针 start = Math.max(start, array[index]); //移动end指针 end += 1; //计算最大窗口 res = Math.max(res, end - start); //记录出现字符位置,重置start指针准备 array[index] = end; } return res; }
相关推荐
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
LeetCode第3题解析
给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。 示例1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。 示例 3: 输入: "pwwkew" 输出: 3 解释: 因为无重复字符的最长子串是"wke",所以其长度为 3。 请注意,你的答案必须是 子串 的长度,"pwke"是一个子序列...
【力扣Leetcode题解系列之0003. Longest Substring Without Repeating Characters 解题全析】
本文详细解析了力扣第3题“无重复字符的最长子串”的两种高效解法:滑动窗口法和一次遍历字典法。滑动窗口法通过双指针维护无重复字符区间,时间复杂度O(n);字典法记录字符位置实现单次遍历,同样O(n)复杂度。文章提供了Python、C++和Go三种语言的实现代码,并分析了算法的时间与空间复杂度。该题考察字符串处理与数据结构运用能力,是算法学习的经典范例。
给定一个字符串s,请你找出其中不含有重复字符的最长子串的长度。
目录 一、代码 二、代码分析 一、代码 public class Demo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符串"); String s = sc.next(); int max = 0; // 标识最大长度 String s..
3 Longest Substring Without Repeating Characters(无重复字符的最长子串)Python实现
Longest Substring Without Repeating Characters(无重复字符的最长子串),其内容如下: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最...
HEVC/VVC中R-Lambda码率控制算法原理
文章目录1. 比特分配1.1 GOP级比特分配1.2 帧级比特分配1.3 LCU级比特分配2. 码率控制2.1 λ的计算和更新2.2 参数QP的确定 VVC中码率控制算法沿用了HEVC中经典的R-λ码率控制模型,R-λ由中科大的李斌博士提出(详情参考JCTVC-K0103)。R-λ模型的基础为视频编码中 R(比特 bpp)和 D(失真 MSE)之间的双曲线模型,即 D(R)=CR−KD(R) = ...
leetcode3--Longest Substring Without Repeating Characters(无重复字符的最长子串)【滑动窗口】
正如标题所述,该题会用到滑动窗口的思想,那么什么是滑动窗口以及什么时候使用呢?顾名思义,滑动窗口就是可滑动的窗口,这里的窗口即代表所包含的子串,滑动代表所包含的子串可以依次滑动(一般都是从左到右滑动),我们只需要创建两个指针(广义指针,并非术语指针)来表示窗口的左右边框,就可以表示该滑动窗口。那创建完窗口后怎么操作捏?让右指针先右移1,判断现在的子串是否符合题目要求。如果符合右指针就继续右移;如果不符合左指针右移1,并判断现在是否符合,不符合则继续移动。//以下是该模板伪代码。
SAP PP模块MRP控制参数实战指南:从NETCH到NEUPL的深度解析
本文深入解析SAP PP模块中MRP控制参数的配置与应用,涵盖NETCH、NETPL和NEUPL三种运行模式的优化策略。通过实战案例和参数对比,帮助用户提升物料需求计划的效率和准确性,优化库存周转率和生产交付能力。
LeetCode第3题 无重复字符的最长子串(Longest Substring Without Repeating Characters)
文章目录题目地址题目描述代码实现 题目地址 题目地址https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其...
RenderDoc实战:5分钟搞定安卓Vulkan应用抓帧(含常见连接失败排查)
本文详细介绍了如何使用RenderDoc工具对安卓Vulkan应用进行抓帧分析,包括环境配置、完整抓帧流程及常见连接问题的解决方案。通过实战案例和高级技巧,帮助开发者快速定位渲染问题和性能瓶颈,提升移动图形开发效率。
晶振(晶体振荡器)测试方法详解
晶振测试方法分为无源和有源两种类型。无源晶振(两脚)需用示波器测试波形,重点关注正弦波形状、200mV-1.5Vpp幅值和频率精度;有源晶振(四脚)通电后直接输出方波。测试要点包括:示波器使用10X探头、AC耦合,带宽需5倍于晶振频率;万用表可初筛短路/开路;替换法是工程最有效方法。常见故障排查顺序:供电→复位→焊接→型号→负载电容→波形观察→更换晶振→MCU配置。负载电容需按公式计算匹配,示波器观察是核心测试手段。
[LeetCode]3. 无重复字符的最长子串(Longest Substring Without Repeating Characters)题解
在leetcode中该题网址:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 ...
[Cocos2d塔防游戏开发]Cocos2dx-3.X完成塔防游戏《王国保卫战》--地图(一)
上面3章图是完成后的效果图 游戏已完成,除了英雄外,基本还原了90%的游戏内容,一共13关,20种防御塔,30+种敌人,如上图,以假乱真吧 下面从地图模块起介绍我的方法,如有更好的方法,请留言一起讨论,游戏资源下载原版游戏数据包,解压即可 推荐一款软件,TextureUnpackerRelease1.04可以分割plist形式的图片,提高效率 经过一个月学习也发现之前有很多化简为繁的错误
LeetCode 3.Longest Substring Without Repeating Characters 无重复字符的最长子串
题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: “abcabcbb” 输出: 3 解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。 示例 2: 输入: “bbbbb” 输出: 1 解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。 示例 3: 输入: “pwwkew” 输出: 3 解释: 因为无重复字符的最长子串是 “w...
新手必看|HDevelop核心算子快速上手,为C#移植打基础
在上一篇文章中,我们完成了C#+HALCON的开发环境搭建。但在实际工业开发中,绝不会直接在C#中编写视觉算法HDevelop可视化调试算法 → 验证效果 → 导出C#代码 → 集成到上位机。HDevelop是HALCON专属的算法调试工具,支持实时预览图像、参数动态调节、零编译运行,是新手入门和工业算法调试的核心工具。本文聚焦工业场景高频使用的核心算子,从基础操作到实战案例,手把手教你使用HDevelop;同时重点讲解算法导出C#的规范,让你调试完成的算法,能无缝移植到WinForm上位机中。
Python图像处理之光斑分析
光斑是工程中经常出现的图像数据,其特点是目标明确,分布清晰。对光斑图像的分析,主要包括质心定位、目标截取以及半径拟合等。
LeetCode 面试必备100题:无重复字符的最长子串 Longest Substring Without Repeating Characters
???? 作者:Linux猿 ???? 简介:优快云博客专家????,C/C++、面试、刷题、算法尽管咨询我,关注我,有问题私聊! ???? 关注专栏:LeetCode面试必备100题(优质好文持续更新中……)???? 目录 一、题目描述 二、测试样例 三、解题思路 四、代码实现 五、算法复杂度 六、题目链接 一、题目描述 给定一个字符串 s ,请你找出其中不含有重复字符的最长子串的长度。 提示: 1. 0 <= s.length <= 5 * 10^4 2. s
036、算子映射:如何将ONNX算子映射到NPU硬件指令?
《算子映射:ONNX到NPU的转换艺术》 本文深入探讨了将ONNX算子映射到NPU硬件指令的技术挑战与实践经验。作者通过真实案例揭示了映射过程中的常见陷阱,如Reshape算子被错误映射导致分类错误。文章详细分析了三种映射策略(直接映射、拆分映射和融合映射),并强调映射表在编译器中的核心作用。特别针对动态形状、精度对齐等难题提供了解决方案,分享了编写映射表时的实用铁律,包括先优化后映射、严格单元测试等。最后指出,成功的算子映射需要在数学语义、硬件约束和性能优化之间找到精确平衡,这种看似枯燥的工作实则决定着N
【leetcode】第3题 无重复字符的最长子串(Longest Substring Without Repeating Characters)做题记录 C++实现
leetcode第3题 #3. 无重复字符的最长子串(Longest Substring Without Repeating Characters) 今天把第三题做完了,各个方法都尝试了一下,思考与学习的时间有点长,说难也不难,其中有一些坑,趁还记着记录一下。 题目描述 题目里没有限定字符取值范围,根据做题结果来看,测试集数据中的字符范围是128个ASCII码,没有用到扩展ASCII码。 关于解...
C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片
基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正
深度学习工具包 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');```[](https://bitdeli.com/free "Bitdeli Badge")
LeetCode 3. Longest Substring Without Repeating Characters 无重复字符的最长子串 C#
前言 本文介绍了 LeetCode 第 3 题 , “Longest Substring Without Repeating Characters”, 也就是 “无重复字符的最长子串” 的问题. 本文使用 C# 语言完成题目,用到了 C# 的哈希表 HashSet 和 Dictionary ( 不用 HashTable 而是使用 Dictionary,官方推荐使用 Dictionary,详情见 h...
温度PID控制PLC程序
温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值
LeetCode 3.Longest Substring Without Repeating Characters 无重复字符的最长子串 Python3 三种解法
LeetCode 3.Longest Substring Without Repeating Characters 无重复字符的最长子串 Python3 三种解法LeetCode 3.Longest Substring Without Repeating Characters 无重复字符的最长子串 Python3 两种解法暴力匹配滑动窗方法一滑动窗方法二 无重复字符的最长子串 Python3...
NTscan扫描工具
ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。
LeetCode(3):无重复字符的最长子串 Longest Substring Without Repeating Characters(Java)
2019.4.18 依旧,字符串问题优先考虑动态规划~ 这道题可以采用滑动窗口策略。设立begin和end指针标志窗口的两端,建立一个HashSet来保存窗口内的元素。当end对应元素不存在HashSet中时,end前移;当end对应元素存在HashSet中时,将元素从set中删除并将begin前移,直到删去end为止。全程取最大的窗口长度即可。 传送门:无重复字符的最长子串 Given a s...
【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)
这道题是LeetCode里的第3道题。 题目描述: 给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。 示例1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。 示例 3: 输入:...
LeetCode(003)- Longest Substring Without Repeating Characters
题目: Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. Example 1: Input: “abcabcbb” Output: 3 Explanation: The answer...
[LeetCode] 3. Longest Substring Without Repeating Characters 最长不重复子串
[LeetCode] 3. Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Expla...
LeetCode3 无重复字符的最长子串 Longest Substring Without Repeating Characters
LeetCode3 无重复字符的最长子串 Longest Substring Without Repeating Characters 题目描述 题目地址:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 示例1:...
1158



被折叠的 条评论
为什么被折叠?



