当前位置:网站首页>剑指 Offer 48. 最长不含重复字符的子字符串
剑指 Offer 48. 最长不含重复字符的子字符串
2022-07-21 10:23:00 【nsq1101】
题目
请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
示例 1:
输入: "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
示例 2:
输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。
示例 3:
输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。
请注意,你的答案必须是 子串 的长度,“pwke” 是一个子序列,不是子串。
提示:
s.length <= 40000
思路
- 遍历一下找到重复字符
- 采用字典方式寻找重复字符的位置
- 动态规划,判断情况
程序
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
dp = [0] * (len(s)+1)
dic = {
}
for j in range(len(s)):
i = dic.get(s[j],-1)
dic[s[j]] = j
if dp[j] < j - i :
dp[j+1] = dp[j] + 1
else:
dp[j+1] = j - i
return max(dp)
边栏推荐
- Jenkins historical version download
- C语言的宏总结
- 2022 global developer salary exposure: China ranks 19th, with an average annual salary of $23790
- Educational codeforces round 70 a question you are given two binary strings
- 2022全球开发者薪资曝光:中国排第19名,平均年薪23,790美元
- bootloader系列一——Arm处理器启动流程解析
- Image segmentation using kmean and post-processing using CRFs
- 2019 Niuke summer multi school training camp (the sixth session) d-move [violence enumeration]
- MySQL45讲笔记-字符串前缀索引&MySQL刷脏页分析
- Qt creator 常用的17个快捷键
猜你喜欢
Codeforces Round #578 (Div. 2) C - Round Corridor 【数论+规律】
CGI的介绍及简单应用
ECCV2022 中小型矩阵的批量高效(batch-efficient)特征分解
This price is fragrant enough! Lingyao 142022 shadow cyan glaze spike: 12th generation core +2.8k OLED screen
A Beginner guide to Deep Learning based Semantic Segmentation using Keras
Web Monitoring - mjpg streamer migration
收藏版|史上最全机器学习优化器Optimizer汇总
Depthwise Separable Convolution详解
2019杭电多校 第五场 6630(原1007) permutation 2 (斐波那契数列)
OpenCV:如何去除票据上的印章
随机推荐
Mutex和智能指针替代读写锁
Mutex and smart pointer replace read-write lock
pytorch入门二 使用pyplot动态展示函数拟合过程
C语言的宏总结
MySQL存储引擎大全
Educational codeforces round 70 a question you are given two binary strings
Matplotlib drawing sin function image
Don't know a little statistics, "Star Wars" for nothing
Codeforces round 578 (Div. 2) B - block adventure [greed]
Web Monitoring - mjpg streamer migration
Macro summary of C language
Codeforces round 579 (Div. 3) C - common divisors [number theory]
正则化的基本思路
Codeforces Round #579 (Div. 3) A 、B、E
Transplant tslib-1.4 to mini2440 development board
[performance optimization] MySQL common slow query analysis tools
Tensorflow Serving部署tensorflow、keras模型详解
工作流引擎在vivo营销自动化中的应用实践 | 引擎篇03
函数之递归[通俗易懂]
Introduction to pytorch II use pyplot to dynamically show the function fitting process