当前位置:网站首页>剑指 Offer 48. 最长不含重复字符的子字符串
剑指 Offer 48. 最长不含重复字符的子字符串
2022-07-16 03:22:00 【愈努力俞幸运】
长度为 N的字符串共有 (1 + N)N/2个子字符串(复杂度为 O(N^2),判断长度为 N的字符串是否有重复字符的复杂度为 O(N) ,因此本题使用暴力法解决的复杂度为 O(N^3)
考虑使用动态规划降低时间复杂度。

'''
#动态规划+字典
class Solution:
def lengthOfLongestSubstring(self, s):
if not s:return 0
dic={}
dp=[0]*len(s)
dp[0]=1
dic[s[0]]=0
ans=1
for j in range(1,len(s)):
i=dic.get(s[j],-1)
if i==-1:
dp[j]=dp[j-1]+1
ans=max(ans,dp[j])
dic[s[j]]=j
elif dp[j-1]<j-i:
dp[j] = dp[j - 1] + 1
ans = max(ans, dp[j])
dic[s[j]] = j
else:
dp[j] =j-i
ans = max(ans, dp[j])
dic[s[j]] = j
return ans
'''
'''
简化代码
class Solution:
def lengthOfLongestSubstring(self, s):
if not s: return 0
dic = {}
dp = [0] * len(s)
dp[0] = 1
dic[s[0]] = 0
ans = 1
for j in range(1, len(s)):
i = dic.get(s[j], -1)#查找字典中有没有这个键
if dp[j - 1] < j - i:
dp[j] = dp[j - 1] + 1
ans = max(ans, dp[j])
dic[s[j]] = j
else:
dp[j] = j - i
ans = max(ans, dp[j])
dic[s[j]] = j
return ans
'''
#动态规划+线性遍历
class Solution:
def lengthOfLongestSubstring(self, s):
if not s: return 0
dp = [0] * len(s)
dp[0] = 1
ans = 1
for j in range(1, len(s)):
i=j-1
while i>=0 and s[i]!=s[j]:
i-=1
if dp[j - 1] < j - i:
dp[j] = dp[j - 1] + 1
ans = max(ans, dp[j])
else:
dp[j] = j - i
ans = max(ans, dp[j])
return ans
a=Solution()
nums="abcabcbb"
print(a.lengthOfLongestSubstring(nums))
边栏推荐
- STL tips
- IO Language Guide
- Mysql5.7创建用户错误:ERROR 1364 (HY000): Field ‘ssl_cipher‘ doesn‘t have a default value解决方法
- This article enables you to understand IIC, SPI and UART protocols
- 群晖7.1使用SHR添加硬盘
- Object conversion problems
- 金仓数据库 KingbaseES SQL 语言参考手册 (3.1.1.8. 几何类型)
- 【HBZ分享】TCP协议讲解
- golang 空接口
- Jincang database kingbasees SQL language reference manual (3.1.1.6. boolean type, 3.1.1.7. bit string type)
猜你喜欢

【RT-Thread】nxp rt10xx SFUD和FAL组件搭建与使用
![[programming compulsory training 9] the number of schemes in the grid (recursion) + alternative addition (bit operation)](/img/84/362c8fb54f2d3b0fe6fb7f915c0b46.png)
[programming compulsory training 9] the number of schemes in the grid (recursion) + alternative addition (bit operation)

Reduce debugging costs by 50%. Xiaojiang IOT pushes a remote serial port debugging assistant

mysql触发器和存储过程
![[golang | GRC] GRC server streaming service end stream practice](/img/c6/b7a81894be1bb60d19311abfc07800.png)
[golang | GRC] GRC server streaming service end stream practice

03_案例搭建【RestTemplate 调用微服务】

iptables 端口转发

Iptables port forwarding

Port forwarding tool rinetd

Talk about data overflow
随机推荐
批流融合升级:Apache Flink 1.15.0 发布 Pulsar Flink Sink Connector
Runtimeerror: the size of tensor a (4) must match the size of tensor B (3) at non singleton
Google Earth engine app (GEE) - load a searchable Spector
Source insight 4 what about Chinese garbled code?
IIC read / write EEPROM
简述memcached的工作原理
台式万用表究竟如何选型?
【机器学习】决策树 – Decision Tree
Kingbasees SQL language reference manual of Jincang database (3.1.1.8. geometric type)
【随记】从入门到入土的密码学 | DES
[swoole series 2.4] websocket service
【RT-Thread】nxp rt10xx 设备驱动框架之--uart搭建和使用
(笔记)ideavim和ide冲突解决方法
Hcip day 4 notes
JdbcTemplate 快速使用
一文速学-PySpark数据分析基础:Spark本地环境部署搭建
吉时利万用表DMM6500
【RT-Thread】nxp rt10xx SFUD和FAL组件搭建与使用
安装g2opy框架
Jincang database kingbasees SQL language reference manual (3.1.1.13. JSON type)