[Leetcode] 3. Longest Substring without Repeating Characters 解题报告

【力扣Leetcode题解系列之0003. Longest Substring Without Repeating Characters 解题全析】 本文详细解析了力扣第3题“无重复字符的最长子串”的两种高效解法:滑动窗口法和一次遍历字典法。滑动窗口法通过双指针维护无重复字符区间,时间复杂度O(n);字典法记录字符位置实现单次遍历,同样O(n)复杂度。文章提供了Python、C++和Go三种语言的实现代码,并分析了算法的时间与空间复杂度。该题考察字符串处理与数据结构运用能力,是算法学习的经典范例。 阅读详情

题目:

 

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.

 

思路:

 

典型的字符串问题中的“Two Pointers”的应用。可以借助一个hash表,维护一个不包含重复字符串的窗口,使用左右指针left,right滑动窗口大小。左右指针的滑动规则如下:

1)首先右指针right向右移动,直到遇到一个和窗口中有重复的字符。右指针在滑动过程中更新最大不重复子串的长度。

2)此时左指针向右移动到重复字符之后去,同时更新hash表中的字符。

该算法的时间复杂度为O(n),空间复杂度依然为O(n)。

在面试中需要注意英文中“subsequence”和“substring”的区别。前者中的邻接字符在原串中也必须是邻接的,而后者中的邻接字符在原串中未必需要邻接。

 

代码:

C++:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if(s.size() == 0) 
            return 0;  
        int left = 0, right = 0, max_length = 0;  
        unordered_map<char, bool> hash;
        for(right = 0; right < s.size(); ++right)
        {
            while(hash.count(s[right]))
                hash.erase(s[left++]);
            hash[s[right]] = true;
            max_length = max(max_length, (int)hash.size());
        }
        return max_length;
    }
};

Python3:

class Solution:
    def lengthOfLongestSubstring(self, s: 'str') -> 'int':
        if len(s) == len(set(s)):   # this means every charchater is unique
            return len(s)
        d = {}                      # map from values to indices
        start = 0
        tmp = 0
        ans = 0
        for index, val in enumerate(s):
            if val in d and d[val]>= start:
                start = d[val] + 1
            d[val] = index
            tmp = index - start + 1
            ans = max(ans,tmp)
        return ans

 

 

 

 

C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告 Leetcode 3. Longest Substring Without Repeating Characters   提交网址: https://leetcode.com/problems/longest-substring-without-repeating-characters/ Total Accepted: 149135 Total Submissions: 674678 阅读详情

相关推荐

使用html和JavaScript实现一个简易的物业管理系统

码实现了一个简易的物业管理系统,主要使用了以下技术和功能: 1.主要技术 使用的技术: HTML: 用于构建网页的基本结构。 包括表单、表格、按钮等元素。 CSS: 用于美化网页的外观和布局。 设置字体、颜色、边距、对齐方式等样式。 JavaScript: 用于处理用户交互和动态更新页面内容。 实现了新增住户、查询住户、编辑住户、删除住户等功能。 处理表单提交事件,验证输入数据,并更新表格显示。 DOM操作: 使用 JavaScript 直接操作 HTML DOM 元素。 动态

weixin_41312759的博客 1777

【LeetCode】345. 反转字符串中的元音字母(Java)

编写一个函数,以字符串作为输入,反转该字符串中的元音字母。(包括大写) class Solution { public String reverseVowels(String s) { //双指针 char[] ch = s.toCharArray(); int left = 0, right = ch.length - 1; while (left < right) { //调整位置

小楊同学的笔记本 316

PCA9548实战:从硬件连接到Linux驱动的I2C扩展全解析

本文详细解析了PCA9548 I2C多路复用器从硬件连接到Linux驱动配置的全过程。涵盖硬件引脚连接、电源去耦、地址选择等关键设计要点,以及Linux驱动加载、设备树配置和应用层编程技巧,帮助开发者高效实现I2C总线扩展。

weixin_28726391的博客 406

[leetcode] 3. Longest Substring Without Repeating Characters 解题报告

题目链接:https://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

小榕流光的专栏 436

LabView实现modbus通讯

labview实现modbus通讯

Lrj的博客 7661

Python数据清洗实战:用RapidFuzz搞定模糊匹配(附完整代码示例)

本文详细介绍了在Python数据清洗中,如何利用RapidFuzz库高效解决字符模糊匹配难题。通过实战代码示例,演示了从处理拼写错误、格式不一致的脏数据,到构建实体对齐与记录去重的完整清洗管道,帮助开发者提升数据质量与处理效率。

pear55的博客 529

leetcode解题报告3. Longest Substring Without Repeating Characters

leetcode解题报告3. Longest Substring Without Repeating Characters题目地址 难度是medium题目描述给定一个字符串,找出里面最长的连续子串,其中要求子串不能有重复的字符。题目只要求求出这个最长子串的长度而已,不需要返回具体的子串。我的思路首先容易想到暴力算法,两层循环,可以遍历所有子串。但是这时间复杂度高,而且明显没必要,有很多优化的地方。

sysu_xiaoming的博客 453

同花顺Supermind量化交易 技术面分析专题(一)-- 相似k线的形态验证与选股

W底”,“多头上涨”,“趋势回踩”,”上涨中继“这些都是常见的K线形态,以”上涨中继“为例,当股价快速上涨后,出现盘整形态而非顶部形态,预示股价还会进一步上涨,那么本节主要讲述”上涨中继”形态是否能够预测未来股价上涨,以及如何应用“上涨中继”形态,来快速选股。"相似K线"技术相比于传统的K线组合而言更具技术性,其还有较多的运用空间,比如:运用顶部形态来监控个股潜在的下跌风险,运用相似度来监控个股是否脱离大盘走势,研究各类形态的上涨预测能力,并应用于选股等等。第一步:选取一段标准的“上涨中继”形态K线图。

qq_22808537的博客 4544

【LeetCode】3. Longest Substring Without Repeating Characters 解题报告(Python)

题目描述: Given a string, find the length of thelongest substringwithout repeating characters. Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", which the length is 3. Input: "bb...

行云 552

【图像去噪】论文复现:降维打击!图像对输入变成像素对输入!Pytorch实现Noise2Void(N2V),基于U-Net模型训练,简洁明了理解N2V核心思想!附训练好的灰度图和RGB图的模型文件!

论文题目:Noise2Void-Learning Denoising from Single Noisy Images —— Noise2Void -从单个噪声图像中学习去噪【图像去噪】论文精读:Noise2Void-Learning Denoising from Single Noisy Images(N2V)由于源码是tensorflow版本,本文将基于Pytorch复现。

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 1355

UNet 改进(19):与ASPP结合的图像分割模型

这个代码实现了一个改进版的UNet模型,在传统的UNet架构基础上,引入了ASPP模块来增强模型捕获多尺度上下文信息的能力。​​UNet主干网络​​:经典的编码器-解码器结构,带有跳跃连接​​ASPP模块​​:位于瓶颈层,用于提取多尺度特征。

2401_82355416的博客 877

Leetcode (3) Longest Substring Without Repeating Characters 解题报告

题目:Longest Substring Without Repeating Characters 找到没有重复字符的最长子串.

晨星的博客 260

YOLO + SubspaceAD:一张良品图,检出所有未知缺陷

当YOLO遇上子空间建模,工业质检正迎来从“见多识广”到“举一反三”的质变时刻。

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 494

LeetCode 3. Longest Substring Without Repeating Characters(Java)解题报告

Requirement Given a string, find the length of the longest substring without repeating characters. Example 1: Input: “abcabcbb” Output: 3 Explanation: The answer is “abc”, which the length is ...

qq_33302567的博客 226

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

【LeetCode】3. Longest Substring Without Repeating Characters解题报告(Python)

参考链接,记得看本文最后的备注 测试代码: class Solution(object): def lengthOfLongestSubstring(self, s): &amp;amp;amp;quot;&amp;amp;amp;quot;&amp;amp;amp;quot; :type s: str :rtype: int &amp;amp;amp;quot;&amp;amp;amp;quot

L141210113的专栏 270

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

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

3. Longest Substring Without Repeating Characters - Medium - Leetcode解题报告

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

weixin_34161064的博客 123

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

LeetCode 3 Longest Substring Without Repeating Characters 解题报告

LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring withou...

weixin_30437481的博客 96

【LeetCode】3.Longest Substring Without Repeating Characters解题报告

【LeetCode】3.Longest Substring Without Repeating Characters解题报告tags: Two Pointers HashTable String题目地址:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 题目描述:

郝春雨的博客 229

LeetCode 3. Longest Substring Without Repeating Characters 解题报告

LeetCode 3. Longest Substring Without Repeating Characters ,最大没有重复字符的子串长度

rjx博客 241

LeetCode(3) Longest Substring Without Repeating Characters解题报告

LeetCode(3) Longest Substring Without Repeating CharactersProblem Given a string, find the length of the longest substring without repeating characters. 大意:求字符串最长没有重复字符的子串的长度。 Example Given “abcabc

jackllvv的博客 205

leetcode 3. 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...

weixin_30871293的博客 138

闯关leetcode——3.Longest Substring Without Repeating Characters

leetcode第三题

方亮的专栏 1340

LeetCode(3) Longest Substring Without Repeating Characters

LeetCode的第3题,给定一个字符串,找到其中的一个最长的字串,使得这个子串不包含重复的字符。 // // Solution.h // LeetCodeOJ_003_LongestUniqueSubstr // // Created by feliciafay on 11/21/13. // Copyright (c) 2013 feliciafay. All rights rese

BUILD 1万+

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 Explanation: Th...

博学 笃志 切问 近思 159
上一篇: [Leetcode] 2. Add Two Numbers 解题报告
下一篇: [Leetcode] 4. Median of Two Sorted Arrays 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值