3. Longest Substring Without Repeating Characters 无重复字符的最长子串
1. 原始题目
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是"abc",所以其长度为 3。
示例 2:
输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是"b",所以其长度为 1。
示例 3:
输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是"wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke"是一个子序列,不是子串。
2. 思路
双指针法。[i,j]左闭又闭区间为当前子串,如果j+1位置的元素没有重复则继续加入,否则i+1直到没有重复元素。那么如何确定是否重复呢,有两种方法,第一种是循环判断在字串中是否仍有重复的元素。第二种是构建一个字典,来判断当前元素是否已经出现过。下面给出了两种解法。
3. 解题
方法1.循环判断是否有重复
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if not s:return 0
i,j=0,0 # 单元素肯定是目前的最长子串
res = 1 # 此时长度为1
temp = s[0] # 子串
while(i<len(s) and j+1<len(s)):
if s[j+1] in temp: # 如果下一个元素有重复
while(s[j+1] in temp): # 则i循环往前直到不包含该重复元素
i+=1
temp = temp[1:]
else:
j+=1 # 没有重复元素就继续往前
temp+=s[j]
res = max(len(temp),res) # 返回最长的子串长度 return res
方法2.利用字典判断当前元素是否有重复
from collections import defaultdict
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if not s:return 0
d = defaultdict(int)
i,j=0,-1 # [i,j] 左闭又闭区间为初始子串
res = 0
while(i<len(s)):
if j+1<len(s) and d[s[j+1]]==0: # 如果j+1个元素没有出现过,则j继续+1
j+=1
d[s[j]]+=1
else: # 出现过则i位置字典对应的次数-1,i右移
d[s[i]]-=1
i+=1
res = max(res,j-i+1) return res
3. Longest Substring Without Repeating Characters 无重复字符的最长子串的更多相关文章
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)
这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...
- leetcode 3. Longest Substring Without Repeating Characters 无重复字符的最长子串
一.题目大意 https://leetcode.cn/problems/longest-substring-without-repeating-characters/ 给定一个字符串 s ,请你找出其 ...
- [LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- 3. Longest Substring Without Repeating Characters无重复字符的最长子串
网址:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 显然采用sliding window滑 ...
- Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
- [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...
随机推荐
- Visual Studio 各版本对应关系
Known Name Version Latest KB / Revision Visual Studio 6 6 Service Pack 6; 6.0.3790.0; VB6.0-KB290887 ...
- SQL 语句中关于 NULL 的那些坑
问题描述 今天在跟进公司内部测试平台线上问题的时候,发现一个忽略已久的问题. 为了简化问题描述,将其进行了抽象. 有一张数据表qms_branch,里面包含了一批形式如下所示的数据: id name ...
- LightOJ - 1369 - Answering Queries(规律)
链接: https://vjudge.net/problem/LightOJ-1369 题意: The problem you need to solve here is pretty simple. ...
- CF827D Best Edge Weight 题解
题意: 给定一个点数为 n,边数为 m,权值不超过 \(10^9\) 的带权连通图,没有自环与重边. 现在要求对于每一条边求出,这条边的边权最大为多少时,它还能出现在所有可能的最小生成树上,如果对于任 ...
- 孤独 & 失望
哪有人喜欢孤独,不过是害怕失望.
- jdango 2.x的url配置的改变
新版本的url.py文件中,不在使用1.x的正则表达式,强制使用在程序启动的时候会提示: WARNINGS: ?: (2_0.W001) Your URL pattern '^*article/' h ...
- 【微信小程序】如何获取用户绑定手机号
用户调用wx.login()方法,获取登录用户凭证code wx.login({ success: function(res) { console.log('loginCode', res.code) ...
- UOJ272. 【清华集训2016】石家庄的工人阶级队伍比较坚强 [FWT]
UOJ 思路 很容易想到\(O(3^{3m}\log T)\)的暴力大矩乘,显然过不了. 我们分析一下每次转移的性质.题目给的转移方程是填表法,我们试着改成刷表法看看-- 发现好像没啥用. 注意到游戏 ...
- shell脚本编程基础介绍
Linux系统——shell脚本编程基础介绍 1.什么是shell 它是一个命令解释器,在linux/unix操作系统的最外层,负责直接与用户对话,把用户的输入解释给操作系统,并处理各种操作输出的结果 ...
- CSS工作记录
1:行内元素 设置背景图片(假设 给span) /*span 标签加背景图片 需要设置快级元素 定义高度宽度,当高度宽度很小的时候 需要设置背景图片大小*/ .filex { display: inl ...