无重复字符的最长子串【动态规划解法】

【leetcode】3 无重复字符的最长字串(动态规划) 题目链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 示例 2: 输入: "bb... 阅读详情

题目描述

给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。

样例

> 输入: "abcabcbb" 
> 输出: 3 
>  解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
     请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

思路

动态规划
设字符串为 s s s,定义 d p [ i ] dp[i] dp[i]为以 s [ i ] s[i] s[i]为结尾的字符串的无重复字符的最长子串,定义映射map <char ,int>lastindex记录在状态转移过程中,某字符出现的最末位置。
不难推导出状态转移存在三种情况:

1° s [ i ] s[i] s[i]未在之前状态出现过,即 l a s t i n d e x lastindex lastindex中找不到 s [ i ] s[i] s[i],则 d p [ i ] = d p [ i − 1 ] + 1 dp[i]=dp[i-1]+1 dp[i]=dp[i−1]+1;

2° s [ i ] s[i] s[i]出现过,但是不包含在dp[i-1]对应的最大的子串中,数学表达为 d p [ i − 1 ] < ( i − 1 ) − l a s t i n d e x [ i ] + 1 dp[i-1]<(i-1)-lastindex[i]+1 dp[i−

最长无重复字符的子串--动态规划 Longest Substring Without Repeating Characters: Given a string, find the length of the longest substring without repeating characters. Examples: Given “abcabcbb”, the answer is “abc”, which 阅读详情

相关推荐

寻找最长不含重复的子字符串(动态规划)

描述  从字符串中找出一个最长的不包含重复字符的子字符串,返回该子字符串的长度。 动态规划 分析:  长度为N的字符串,有(1+N)*N/2 个子字符串, 状态定义:dp[j],代表以字符s[j]为结尾的最长不重复子字符串的长度。 初始状态: 转移方程:固定右边界j,设在s[j]左侧,且与s[j]距离最近的相同字符为s[i]; 若左侧无相同字符,则dp[j]=dp[j-1]+1; 若dp[j–1]<j-i 则 dp[j]=dp[j-1]+1; dp[j-1]>=j-i; 返回值:最长的子字符

CCC_B的博客 995

XS5018A TX ISP芯片同轴高清模拟输出图像处理芯片用户指南

当看门狗开启且系统异常,看门狗模块得不到喂狗信号时,自动产生看门狗复位信号,复位​​​​​​​。分量上与人眼对光谱的响应通常是有偏差的,通常通过一个色彩校正矩阵校正光谱响应的交叉效。但实际上由于电路的作用,复位模块对整个芯片及各个功能模块的复位进行统一管理,负责产生各个模块的软复位信。每个区域前景色可以在 16 个色表中查找,单个区域的前景色统一,各个区域的前。较弱的坏点被放大成刺眼的白点和明显的黑点,强烈推荐开启动态坏点校正功能。四种制式,通过寄存器配置,可以很方便的实现四种制式的切换、且可以对截止。

2403_88922712的博客 1968

Windows游戏玩家必看:用Process Lasso优化CPU优先级和核心绑定,告别卡顿

本文为Windows游戏玩家详细介绍了如何使用Process Lasso优化系统性能。通过设置CPU优先级和核心绑定,可以智能管理进程资源,确保游戏获得最高调度权限,有效减少因后台程序干扰或Windows调度机制不完善导致的卡顿与帧数波动,从而提升游戏流畅度与稳定性。

weixin_29299807的博客 2266

Verilator+VSCode实战:5分钟搞定Verilog语法检查与波形调试

本文详细介绍了如何将Verilator仿真器与VSCode编辑器高效集成,打造极速的Verilog开发工作流。通过环境配置、实时语法检查、一键仿真与波形生成等实战技巧,帮助IC设计工程师在几分钟内完成从代码编写到波形调试的全过程,大幅提升中小规模模块的验证效率。

weixin_28454475的博客 473

IAP板卡远程升级(串口网口都可)+Ymodem+AES对bin文件加密+pc软件+板卡相互烧录

还是决定记录一下,毕竟这个东西我也搞了挺久的,遇到一些棘手的,或者是因为我很粗心遇到的问题也和大家分享一下,。 板卡核心是 STM32F103RCT6,256kflash 前言 开始看这个远程升级的时候,我是一头雾水的,原子哥的教程里面也有说到过iap,有了一个大概的了解,我想不明白的是,通过串口下载的方式把hex文件下载到flash,和把bin文件下载到flash难道不是拉一根线,下载一个文件嘛,(初入职场的我并没有考虑到安全这个问题)如果直接将hex文件给到客户那边,风险是非常大...

那可真是太开心了的博客 5379

弱网、高热、只有 32MB 内存:恶劣环境下嵌入式 SoC 音视频开发的“不死”法则

永远不要假设。你申请一个 1920 宽的图像,硬件为了对齐(比如 16 字节对齐或 256 字节对齐),实际分配的一行可能有 1936 甚至是 2048 字节。那个多出来的部分叫Padding。如果你写代码时用来计算数据量,或者用Width来做指针偏移:恭喜你,你的画面会像被风吹歪的百叶窗一样,产生斜切畸变。永远使用VB_CONF结构体里返回的u32Stride字段。这是区分新手和老手的试金石。第六章 ISP 调优(Tuning)实战:为什么你调出的画面像个“鬼片”?

大模型大数据攻城狮的专栏 1779

人形机器人新产品导入(NPI)工艺工程师的角色与技术解析

本文系统解析人形机器人NPI工程师的核心职责与能力要求。主要内容包括:1)NPI工程师需主导装配工艺设计、样品试做及问题闭环,涉及公差分析(如尺寸链计算)、工艺评审与FMEA应用;2)要求输出完整工艺文档体系,并跟踪试产至量产全过程;3)任职资格强调3年以上机器人/智能制造行业经验,掌握SPC、公差计算等质量工具,熟悉点胶/焊接等工艺及特殊材料特性;4)通过实际案例展示关节精度优化、新物料验证等典型工作场景;5)提供基于职责的面试问题及技术性解答方案。文章聚焦技术细节,为从业者提供实用参考框架。

郑伟强dev的专栏 385

锐捷 MPLS VPN 配置

ISP内部配置 , PE设备和CE设备之间的接口不开启 ( 只在物理接口上配置 , loopback 口不配置 )最后一步关闭 ipv4 单播 , 如果你发现路由奇奇怪怪的 , 可以尝试一下。both --> 同时配置 import export 值。绑定后接口会重置 , 且接口的路由会转到VRF路由表中。这里基本上绑定了 vrf 实例的接口都需要宣告进去。vpnv 4 通道是用来传递 mpls 路由的。先建立邻居管理,建立VPNV4通道。这里要做双向引入 , 才会有路由。export 导出值。

dekangzou的博客 1717

深度学习工具包 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")

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

上一篇: ubuntu安装cuda教程详解
下一篇: 翻转字符串里的单词【stringstream解法】
RealWeakCoder
博客等级 码龄7年 236粉丝 68原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址