[leetcode] 3. Longest Substring Without Repeating Characters @ python

3 Longest Substring Without Repeating Characters(无重复字符的最长子串)Python实现 Longest Substring Without Repeating Characters(无重复字符的最长子串),其内容如下: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最... 阅读详情

原题

https://leetcode.com/problems/longest-substring-without-repeating-characters/
Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.
Example 2:

Input: “bbbbb”
Output: 1
Explanation: The answer is “b”, with the length of 1.
Example 3:

Input: “pwwkew”
Output: 3
Explanation: The answer is “wke”, with the length of 3.
Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

解题思路

遍历字符串, 用字典来储存字符和索引, 如果字符重复了, 更新subtring的起点start, 每次遍历后, 更新result的值. 这里start应该只能增加不能减少, 否则特殊情况时, 例如’abba’ 会出错.

时间复杂度: O(n)
空间复杂度: O(1)

代码

class Solution:
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        # use start to record the start index of substring
        start, res = 0, 0
        seen = {}
        for i, ch in enumerate(s):
            if ch in seen:
                # start cannot decrease
                start = max(start, seen[ch] + 1)
            seen[ch] = i
            res = max(res, i-start+1)
        return res
【力扣Leetcode题解系列之0003. Longest Substring Without Repeating Characters 解题全析】 本文详细解析了力扣第3题“无重复字符的最长子串”的两种高效解法:滑动窗口法和一次遍历字典法。滑动窗口法通过双指针维护无重复字符区间,时间复杂度O(n);字典法记录字符位置实现单次遍历,同样O(n)复杂度。文章提供了Python、C++和Go三种语言的实现代码,并分析了算法的时间与空间复杂度。该题考察字符串处理与数据结构运用能力,是算法学习的经典范例。 阅读详情

相关推荐

LeetCode3 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1. Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. Java AC版本

[python 刷题] 3 Longest Substring Without Repeating Characters

解题思路是用双指针+哈希表,左右指针指向子字符串的开始和结束的位置,哈希表存储每个字符串最后出现的下标+1,每次更新右侧指针时,如果当前字符是已经出现的字符,则将左指针移向最后出现的位置。这里存储的所有位置都是下标+1,主要是为了针对只出现 1 个字符的案例,如。,所以针对这个情况,所有存储的位置都是下标+1,计算字符串长度时也用。之前所在的下标位置,依旧会取到包含重复字符的字符串,因此需要取当前。开局的时候 dict 是空的,左右指针同时指向。这到提要求找的是最长的,没有重复符号的子字符串。

GoldenaArcher的博客 596

COMSOL实现裂缝性储层油水两相流模拟的关键技术

多孔介质中的流体流动模拟是油气藏开发的核心技术,其基本原理基于达西定律和质量守恒方程。在裂缝性储层中,离散裂缝网络与基质系统的耦合流动带来了特殊的数值挑战。通过COMSOL Multiphysics平台实现的多物理场耦合技术,可以精确模拟油水两相在复杂裂缝系统中的渗流行为。该技术采用Level Set方法表征裂缝几何形态,结合自适应网格生成技术解决狭窄裂缝的网格剖分难题,并创新性地引入Bang-Larsen毛管压力滞后模型,显著提高了采收率预测精度。这些方法在致密油藏开发、水驱优化等工程实践中具有重要应用价

weixin_34005090的博客 237

【Leetcode-python】3.Longest Substring Without Repeating Characters(最长不重复连续子字符串)

题目:给定一个字符串,输出没有重复字符的最长子字符串的长度。 例:Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Input: "bbbbb" Output: 1 Explanation: The answer is "b", wi...

vita2dolce的博客 402

基于PCIE8311+FPGA+TMS320F28335 PCIE应用项目开发板PROTEL99设计硬件(原理图+PCB)文件

基于PCIE8311+FPGA+TMS320F28335 PCIE X1应用项目开发板PROTEL99设计硬件(原理图+PCB)文件,Protel 99se 设计的项目工程文件,包括原理图及PCB印制板图,可用Protel或 Altium Designer(AD)软件打开或修改,都已经制板在实际项目中使用,可作为你产品设计的参考。

GJZGRB的博客 581

Jupyter Notebook 命名出现“error”且运行后没反应

Jupyter Notebook 命名出现“error”且运行后没反应提前声明错误截图(方便大家看看是不是自己的错误)解决方法 提前声明 我的问题是在看到答主 @开始King的回答后才解决的,因为答主大大的文章后面有些地方不是很详细,我在这里再在King大大的基础上完善一下子 错误截图(方便大家看看是不是自己的错误) 1.命名失败,上面出现一个“error”字样 2.运行失败,无论是点击上方的运行按钮还是按快捷键shift+enter 都只会向下再增加一个代码块,而不会运行程序。 (也不是很多人出现的[*

qq_52770743的博客 1万+

【LeetCode】修炼之路-0003-longest-substring-without-repeating-characters(无重复字符的最长子串)【python】【中等】

leetcode 0003 最长无重复字串 python题解

crazyjinks的博客 1394

Transformer 面试题及详细答案120道(1-10)-- 基础概念

本文总结了Transformer模型的10个常见面试问题及答案,涵盖了其核心结构、创新点、工作机制等关键知识点。主要内容包括:Transformer的自注意力机制和多头注意力机制创新点,相比RNN/CNN的优势,模型在《Attention Is All You Need》论文中的提出背景,以及Encoder-Decoder协同工作流程等。重点解析了位置编码的作用、长距离依赖处理能力、与传统注意力机制的区别等核心问题。每个问题都给出了简明扼要的专业解答,适合作为Transformer相关面试准备的参考资料。

大剑师兰特的GIS世界 855

无重复字符的最长子串

给定一个字符串 s,请你找出其中不含有重复字符的的长度。= "因为无重复字符的最长子串是 ",所以其长度为。= "因为无重复字符的最长子串是 "",所以其长度为。= "因为无重复字符的最长子串是 "",所以其长度为。请注意,你的答案必须是的长度,""是一个子序列,不是子串。

qq_51753728的博客 2734

【ROS】Nav2源码之nav2_collision_monitor(碰撞监测)详解

nav2_collision_monitor碰撞监视器是一个节点,为机器人提供了一个额外的安全级别。它使用来自传感器的数据,执行若干与避免碰撞相关的任务,绕过成本图和轨迹规划器,监测是否需要紧急停止或者减速。

郭老二 1466

ADB加密实例

主要介绍如何给adb命令增加加密访问

四儿家的小祖宗的博客 2917

LeetCode 3 — Longest Substring Without Repeating Characters (C++ Java Python)

题目:http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/] Given a string, find the length of the longest substring without repeating characters. For example, the longest sub

龙之梦 5811

全覆盖路径规划开源项目Clean-robot-turtlebot3关键函数解析(上)

本系列文章主要介绍全覆盖路径规划开源项目Clean-robot-turtlebot3的相关内容,包含如下四篇文章,分别介绍了开源项目Clean-robot-turtlebot3流程的概括总结、ROS坐标系常用坐标及其变换、Clean-robot-turtlebot3关键函数解析等内容。

慕羽★的博客 2783

[LeetCode] 3. Longest Substring Without Repeating Characters 最长不重复子串

[LeetCode] 3. Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Expla...

weixin_30795127的博客 111

optee系统启动流程

BL2 - Trusted Boot Firmware,一般为Trusted Bootloader, 也称第一个启动镜像。BL31 - EL3 Runtime Firmware,负责双系统切换,运行在secure monitor 或ATF中。BL33 - Non-Trusted Firmware,一般为uboot、linux kernel。BL32 - Secure-EL1 Payload, 一般为TEE OS Image。BL1 - Trusted ROM,一般为BootRom。

baron-周贺贺-代码改变世界ctw 283

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

【LeetCode】3. Longest Substring Without Repeating Characters

Introduce Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Examp...

roguesir的博客 1600

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

LeetCode之3. Longest Substring Without Repeating Characters

LeetCode上给的解决办法尝试 #1 暴力方法思考 一个个查看所有的子字符串看看是否没有相同的字符 算法 ——假设我们有一个函数,boolean allUnique(String substring),这个函数在String的每个子字符串中的字符都是两两不同的时候返回True。我们可以迭代字符串S中的所有子字符串并调用allUnique这个函数。如果返回True的话,我们就更新不

梦想,就现在! 487

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【LeetCode】3.Longest Substring Without Repeating Characters(Python)

Problem 给定一个字符串,找到最长子字符串的长度而不重复字符。 例1: 输入:“abcabcbb” 输出:3 说明:答案是"abc",长度为3。 例2: 输入:“BBBBB” 输出:1 说明:牛逼,他的回答是"b",与1的长度。 例3: 输入:“pwwkew” 输出:3 说明:答案是&a

E.W的博客 829

3. Longest Substring Without Repeating Characters

3. Longest Substring Without Repeating Characters 题目描述 Given a string, find the length of the longest substring without repeating characters. Example 1: Input: “abcabcbb” Output: 3 Explanation: The an...

fruitful1989的博客 538

[LeetCode] 3. Longest Substring Without Repeating Characters(Python)

[LeetCode] 3. Longest Substring Without Repeating Characters(Python)1. 题目2. 题目理解3. 代码实现 1. 题目 Given a string s, find the length of the longest substring without repeating characters. Example 1: Input: s = “abcabcbb” Output: 3 Explanation: The answer is “a

qq_35029666的博客 709

[leetcode][python] 3. Longest Substring Without Repeating Characters

3. Longest Substring Without Repeating Characters 知识点:two pointers 1. 原题 Given a string, find the length of the longest substring without repeating characters. Example 1: Input: “abcabcbb” Output: 3 ...

qq_40670635的博客 406

Longest Substring Without Repeating Characters

对leetcode第三题Longest Substring Without Repeating Characters的解法分析标签(空格分隔): 编程 程序参考了http://blog.sina.com.cn/s/blog_60b5450101017xqh.html的思路并且修改了错误 题目分析给出一个字符串,找出这个字符串中最长的没有重复字符的子串,需要注意的是题目中子串的概念,子串必须是连续

dongchang's blog 711

leetcode 3. Longest Substring Without Repeating Characters Python3

leetcode 3. Longest Substring Without Repeating Characters Python Description 点击查看题目 Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcab...

与你的博客 253

leetcode python - Longest Substring Without Repeating Characters

# Given a string, find the length of the longest substring without repeating characters. # # Examples: # # Given "abcabcbb", the answer is "abc", which the length is 3. # # Given "bbbbb", the answer i

人工智能 7271
上一篇: [leetcode] 2. Add Two Numbers @ python
下一篇: [leetcode] 204. Count Primes @ python
闲庭信步的空间
博客等级 码龄9年 86粉丝 416原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值