LeetCode Longest Substring Without Repeating Characters

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

原题链接在这里:https://leetcode.com/problems/longest-substring-without-repeating-characters/

String 题目,基本思路就是维护一个窗口[walker,runner]. walker,runner都是向右移,同时维护一个HashSet hs,用来存储这一段中的所有不同character。首先右侧窗口runner在前跑,每当遇到hs中没有的character,加入hs,知道遇到相同元素,说明以不符合要求,runner停下,准备移动左侧窗口walker。

移动左侧窗口walker前,先更新最大长度max(res, runner - walker)。然后开始移动walker,一直找到与此时runner指向的相同元素,之前的元素都需从hs中删除。

本应删掉此时walker指向的元素,但由于runner还要再次添加这个元素,并且那一行在else statement 里不方便改写,所以没有删掉walker此时指向的元素。

Time Complexity is O(n), Space Complexity is O(n).

Note: 无论hs中是否含有runner指向的元素,runner都要向后移,否则下一循环时就会错误减掉元素。

AC Java:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s == null || s.length() == 0)
            return 0;
            
        int res = 0;
        int walker = 0;
        int runner = 0;
        HashSet hs = new HashSet();
        
        while(runner < s.length()){
            if(hs.contains(s.charAt(runner))){
                res = Math.max(res,runner - walker);
                while(s.charAt(walker) != s.charAt(runner)){
                    hs.remove(s.charAt(walker));
                    walker++;
                }
                walker++;
                
            }else{
                hs.add(s.charAt(runner));
            }
            runner++;
        }
        
        return Math.max(res,runner - walker);
        
    }
}


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

相关推荐

【力扣Leetcode题解系列之0003. Longest Substring Without Repeating Characters 解题全析】

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

youngerwang的博客 947

LeetCodeLongest Substring Without Repeating Characters(应该全是比较全的,有思路分析)

有思路分析,有图示,相当于带你debug一遍,但是有图更清晰。方法应该也是比较全的

Blackoutdragon的博客 675

Leetcode回文串拼接-leetcode_note:用于记录leetcode题目的解析

Leetcode回文串拼接 leetcode_node 题解 该项目主要用于基于Leetcode的刷题记录,与日常学习,对Leetcode上的题目按照解题方法进行分明别类的整理。 题目列表 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring 6.ZigZag Conversion 7.Reverse Integer 8.String To Integer 9.Palindrome Number 10.String To Integer 11.Container With Most Water 12.Integer To Roman 13.Roman To Integer 289 347 380 442 457 Circular Array Loop 535 Encode and Decode TinyURL 560 565 566 Maximum Binary T

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的博客 396

LeetCode:3.无重复字符的最长字串(longest-substring-without-repeating-characters)思路与题解

LeetCode:3.无重复字符的最长字串(longest-substring-without-repeating-characters) 题目描述 思路 这是典型的滑动窗口问题,即通过滑动窗口来找到最长的字串,我们来看一下具体过程: 首先字符串abcabcbb如下: 然后在字符串第一个位置展开窗口 此时,窗口的起始位置start=0,终止位置end=0(注意字符串也是从0开始的),窗口的长度为length=end-start+1=1 此时窗口内还没有重复元素,所以end向右移动一位,得到: 此时

野犬17的博客 597

Leetcode】3. Longest Substring Without Repeating Characters

题目地址: https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目大意是,给定一个字符串,求其中的一个子串不含重复字母,返回其长度。可以用双指针来做,用一个map保存快指针遍历到jjj的时候s[0,...,j]s[0,...,j]s[0,...,j]所有字母最后出现的位置,快指针从000向后遍......

数学、算法爱好者的博客 553

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

博学 笃志 切问 近思 158

[LeetCode]——Longest Substring Without Repeating Characters

作为模式识别的图像狗,越发觉得毕业找工作还是得coding! 痛定思痛,入了leetcode的深坑! 在提交了无数次以后,Accepted终于向俺招手了~ 虽说复杂度高了点,但是作为一个学渣,能看到Accepted这辈子也值了!class Solution { public: int lengthOfLongestSubstring(string s) { str

vashzx的专栏 433

leetcode--3. Longest Substring Without Repeating Characters

尺取法 首先设置头尾两个指针,都指向头,然后尾指针开始遍历,在遍历过程中记录每个字母出现个数,每次遍历若当前字母没有出现过,则更新答案长度。若出现过,则将头指针移到重复字母的后面一位。然后继续重复上述操作。最后返回答案即可。 class Solution { public: int lengthOfLongestSubstring(string s) { if(s.le

林伏案的博客 438

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

leetcode第三题

方亮的专栏 1307

[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的博客 103

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

leetcode中该题网址:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 ...

sultry_yi的博客 516

LeetCode题解 C++ Longest Substring Without Repeating Characters

LeetCode题解 Longest Substring Without Repeating Characters

Caffey 439

Leetcode第三题《Longest Substring Without Repeating Characters

Leetcode第三题《Longest Substring Without Repeating Characters》 题目: Given a string, find the length of the longest substring without repeating characters. For example, the lo...

weixin_30361641的博客 92

LeetcodeLongest 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. Examples: Give

业精于勤,荒于嬉 3182

LeetCode力扣之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 l...

LeeWei 144

LeetCode -- Longest Substring Without Repeating Characters

题目描述:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3.

我的英文站点:https://iorilan.medium.com/ 1761

LeetcodeLongest Substring Without Repeating Characters 问题

问题描述: Given a string, find the length of the longest substring without repeating characters. 示例: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", wit

漫漫学习路 303

LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)

这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Characters(顺位题号是3)。给定一个字符串,找到最长无重复字符子字符串的长度。例如: 输入:“abcabcbb” 输出:3 说明:答案是“abc”,长度为3。 输入:“bbbbb” 输出:1 说...

小川的博客 205
上一篇: LeetCode Single Number
下一篇: LeetCode Substring with Concatenation of All Words
Dylan_Java_NYC
博客等级 码龄11年 1粉丝 134原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值