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 ...
随机推荐
- 本地仓库推送到远程仓库:fatal: refusing to merge unrelated histories
最近,在操作git的时候,遇到各种问题,下面总结一下. 最开始,我不是先把远程仓库拉取到本地 ,而是直接在本地先创建一个仓库,再git remote add添加远程仓库. 当然,gitee官方还是有操 ...
- LINUX部署TOMCAT服务器
转载声明: http://www.cnblogs.com/xdp-gacl/p/4097608.html 解压tomcat服务器压缩包 配置环境变量 tomcat服务器运行时是需要JDK支持的,所以必 ...
- Backpack IV
Description Given an integer array nums[] which contains n unique positive numbers, num[i] indicate ...
- php自定义函数之变量作用域
我们通过前面的章节函数定义部份的学习我们知道了几个不同的规矩: 函数定义时后括号里面接的变量是形式上的参数(形参),与函数体外的变量没有任何关系.仅仅是在函数内部执行大理石量具哪家好 函数内声明的变量 ...
- 洛谷 P2279 [HNOI2003]消防局的设立 题解
每日一题 day34 打卡 Analysis 这道题的正解本来是树形dp,但要设5个状态,太麻烦了.于是我就用贪心试图做出此题,没想到还真做出来了. 考虑当前深度最大的叶子结点,你肯定要有一个消防局去 ...
- Phoenix 简单介绍
转载自:https://blog.csdn.net/carolzhang8406/article/details/79455684 1. Phoenix定义 Phoenix最早是saleforce的一 ...
- COGS 2687 讨厌整除的小明
二次联通门 : COGS 2687 讨厌整除的小明 /* cogs 2687 讨厌整除的小明 打表出奇迹.. 考场时看了一下样例就感觉有非常鬼畜的做法.. 手搞几组数据做法就出来了... 2333 * ...
- Mathtype新版本
新版本Mathtype在MS Word 2007不能直接调用.今天发现,可以直接从Windows程序菜单中启动.在独立的窗口下编辑,编辑后粘贴到Word或Tex中. 早年使用Word时,嫌Mathty ...
- Java 线程之间的通讯,等待唤醒机制
public class ThreadNotifySample { public static void main(String[] args) { // Res res = new Res(); / ...
- 安装 sqoop
简介 Sqoop是一个用来将Hadoop(Hive.HBase)和关系型数据库中的数据相互转移的工具,可以将一个关系型数据库(例如:MySQL ,Oracle ,Postgres等)中的数据导入到Ha ...