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 无重复字符的最长子串的更多相关文章

  1. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  2. 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)

    这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...

  3. leetcode 3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    一.题目大意 https://leetcode.cn/problems/longest-substring-without-repeating-characters/ 给定一个字符串 s ,请你找出其 ...

  4. [LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  5. 3. Longest Substring Without Repeating Characters无重复字符的最长子串

    网址:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 显然采用sliding window滑 ...

  6. Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串

    给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...

  7. [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  8. [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  9. LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters

    题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...

随机推荐

  1. 使用笔记:TF辅助工具--tensorflow slim(TF-Slim)

    如果抛开Keras,TensorLayer,tfLearn,tensroflow 能否写出简介的代码? 可以!slim这个模块是在16年新推出的,其主要目的是来做所谓的“代码瘦身” 一.简介 slim ...

  2. Android平台5+ API提前生效,支持在plusready事件前调用

    ios上plus是一直存在的,不涉及等ready事件.但安卓上还是需要等plus ready.在安卓环境中,通常情况下需要html页面解析完成后才会让5+ API生效,安卓的执行的顺序为: 加载htm ...

  3. HDFS写机制

    HDFS写机制: 1.client客户端调用分布式文件系统对象DistributedFileSystem对象的create方法,创建一个文件输出流FSDataOutputStream对象. 2.Dis ...

  4. MySQL 是怎么保证数据一致性的(转载)

    在<写数据库同时发mq消息事务一致性的一种解决方案>一文的方案中把分布式事务巧妙转成了数据库事务.我们都知道关系型数据库事务能保证数据一致性,那数据库到底是怎么设计事务这一特性的呢? 一. ...

  5. PHP命令行执行文件或代码

    Linux环境 1.执行代码 php -r "echo 'hello';" (注意加分号,与PHP文件一样) 2.执行文件 php -f  文件所在路径(/var/www/xxx. ...

  6. BZOJ 4332: JSOI2012 分零食 FFT+分治

    好题好题~ #include <bits/stdc++.h> #define N 50020 #define ll long long #define setIO(s) freopen(s ...

  7. learning java AWT EventQs

    import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.Ac ...

  8. mysql 函数表

    Name Description ABS() Return the absolute value ACOS() Return the arc cosine ADDDATE() Add time val ...

  9. noi.ac #42 模拟

    \(des\) 二维平面上存在 \(m\) 个点,每个点会对该点的 \(8\) 个方向上的最近的点产生影响 问每个点会被影响多少次 \(sol\) 过每个点会产生 \(4\) 条线段 保存每条线段的斜 ...

  10. Luogu P1903 BZOJ 2120 数颜色 带修改的莫队

    https://www.luogu.org/problemnew/show/P1903 之前切过这道题,复习莫队再切一遍,不过我之前写的是主席树和树状数组,也不知道我当时怎么想的…… 这个题卡常我没写 ...