LeetCode-Longest Substring Without Repeating Characters

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

This question belong to the same category as those such as "longest substring without repeating characters", "minimum window substring", and "substring with concatenation of all words". To solve this kind of question we can use two pointers and a hash table. When the key of the hash table is char, we can simply use an array as the hash table. The most important idea in solving this kind of questions is "how to update the "start" pointer" and the solution to these questions seem usually differ only in this respect.

这段话写的很好, 都是用hash 有的题用map(longest substring at most 2 distinct char)有的题用hashset就可以(这个题)然后记录一个start指针

检查新碰到的这个char之前遇到过没有,假如遇到过就将start移动到这个char第一次出现过的地方的后面 就是重新开始一个substring。

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        if ( s == null || s.length() == 0 )
            return 0;
        HashSet<Character>set = new HashSet<Character>();
        int len = 0;
        int start = 0;
        int longest = 0;
        for ( int i = 0; i < s.length(); i ++ ){
            if ( !set.contains(s.charAt(i))) {
                set.add(s.charAt(i));
                len ++;
            }
            else{
                while ( s.charAt(start) != s.charAt(i)){
                    set.remove(s.charAt(start));
                    start ++;
                }
                start ++;
                len = i - start +1;
            }
            if ( len > longest)
                longest = len;
        }
        return longest;
    }
}

下面这个人写的更简洁一点 用了hashmap value存这个char出现过的所有位置 省去了我一个一个移动start的这步,直接移动到max(这个key随影的values)就是上一次出现的地方

   public int lengthOfLongestSubstring(String s) {
        if (s.length()==0) return 0;
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        int max=0;
        for (int i=0, j=0; i<s.length(); ++i){
            if (map.containsKey(s.charAt(i))){
                j = Math.max(j,map.get(s.charAt(i))+1);
            }
            map.put(s.charAt(i),i);
            max = Math.max(max,i-j+1);
        }
        return max;
    }
才意识到 这就是dp了 晕



[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 the le 阅读详情

相关推荐

从拆箱到跑通:Intel RealSense D435i开箱实测+ROS驱动配置全流程

本文详细记录了Intel RealSense D435i深度相机的开箱体验,并提供了在Ubuntu 22.04与ROS2 Humble环境下,从安装librealsense2 SDK到配置ROS2驱动的完整流程。重点解决了驱动安装、数据流发布、点云对齐以及IMU数据融合等关键步骤,帮助研究者和开发者快速实现D435i与机器人系统的集成。

weixin_29168393的博客 313

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

博学 笃志 切问 近思 162

大模型KV Cache节省神器MLA学习笔记(包含推理时的矩阵吸收分析)

这篇文章主要是对Deepseek2提出的优化KV Cache的MLA方法做个人理解,特别是关于MLA的矩阵吸收部分,这部分Paper以及官方开源实现没有给出。然后,开源社区大佬确实出手很快,在知乎的《如何看待 DeepSeek 发布的 MoE 大模型 DeepSeek-V2?

I good vegetable a! 1万+

[LeetCode]——Longest Substring Without Repeating Characters

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

vashzx的专栏 458

ctfshow---sql注入(171-213)

这里提一句,为什么不直接替换成acb这种,因为,flag原本就有字母,还原的时候会全部都还原成数字的,数字替换成符号应该也是可以的,这边测试了下除了&#好像别的符号都可以。插入查询后查询语句就会变成这样,由于id=11111这是一个不存在的id,而后面1=1又为真,所以会查询到表内所有的内容,--+是注释后面的字符。因为接受username的时候没有用引号,所以接受单纯的数字,而数字和字符比较会转换类型,字符串会变成数字0进行比较,所以相等。

fainpo的博客 1332

ANSYS HFSS从入门到放弃?这些隐藏技巧让3D电磁仿真效率翻倍

本文针对已掌握ANSYS HFSS基础操作但受困于仿真效率的用户,深入剖析了提升3D电磁仿真速度的核心技巧。文章重点介绍了如何精准控制自适应网格剖分、合理配置GPU加速与分布式计算以释放硬件潜能,并分享了针对PCB/封装仿真的HFSS 3D Layout优化工作流。掌握这些方法,能有效将仿真时间从“天”级缩短至“小时”级,大幅提升天线、高速连接器等复杂电磁结构的设计迭代效率。

weixin_29046611的博客 435

leetcode--3. Longest Substring Without Repeating Characters

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

林伏案的博客 463

IGV实战指南:从数据导入到Peak可视化的全流程解析

本文提供了一份IGV实战指南,详细解析了从数据导入到Peak可视化的全流程。内容包括软件安装、参考基因组加载、测序数据处理、信号调整以及如何精准定位和可视化Peak区域,并分享了导出出版级矢量图的高级技巧,帮助用户高效完成基因组数据可视化分析。

weixin_29212867的博客 155

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

leetcode第三题

方亮的专栏 1342

特斯拉Model 3 BMS主控板拆解:从硬件设计到热管理策略的实战解析

本文深度拆解特斯拉Model 3的BMS主控板,解析其硬件架构与热管理协同策略。文章聚焦于主控板的分区布局、双核MCU的智慧分工,以及从被动监控到主动干预的热管理范式转变,揭示了特斯拉在高压安全、实时控制与系统级工程思维上的设计哲学。

zz67890的博客 606

汇川AM系列ethercat控制伺服的使用说明(含转矩模式)

本文以一个实际例子,介绍ethercat伺服驱动器的工作原理,以及在汇川AM系列PLC中,如何点动、定位、速度、转矩控制ethercat伺服。

qq_34047402的博客 5134

[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

uniapp [全端兼容] - 实现连接蓝牙电子秤+监听数据转换并实时返回示例代码,uniApp实现BLE蓝牙连接多个电子秤设备、蓝牙通讯协议对接详细教程(全平台兼容H5、微信小程序、苹果安卓App)

uniapp连接蓝牙电子秤,uniapp如何连接电子秤蓝牙,蓝牙称重,实现监听数据转换并实时返回,多蓝牙,uni-app电子秤16进制蓝牙通讯协议对接,uniapp微信小程序蓝牙获取电子秤的值,uniapp蓝牙称重电子秤,蓝牙电子秤硬件,APP端连接蓝牙秤获取重量实时显示在页面上蓝牙秤,插件组件,蓝牙连接失败,uniapp安卓苹果ios连接蓝牙电子秤没反应,uniapp微信小程序 蓝牙低功耗连接发送数据监听接收,uni-app小程序蓝牙连接、读写数据全过程,uniapp连接蓝牙体脂秤,蓝牙搜索不到怎么办,如

🏆 前端领域优质创作者 1万+

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

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

sultry_yi的博客 524

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

LeetCode题解 C++ Longest Substring Without Repeating Characters

LeetCode题解 Longest Substring Without Repeating Characters

Caffey 443

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

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

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

【Leetcode】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. Examples: Give

业精于勤,荒于嬉 3185

【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向后遍......

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

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 145

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

小川的博客 212

LeetCode 3. Longest Substring Without Repeating Characters

LeetCode 3. Longest Substring Without Repeating Characters (Medium) 主要知识点:sliding window;优先级:4

温迪今天刷题了吗 275

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/ 1771

LeetCode Longest Substring Without Repeating Characters

原题链接在这里:https://leetcode.com/problems/longest-substring-without-repeating-characters/ String 题目,基本思路就是维护一个窗口[walker,runner]. walker,runner都是向右移,同时维护一个HashSet hs,用来存储这一段中的所有不同character。首先右侧窗口runner在前跑

Dylan_Java_NYC的博客 583

Leetcode之Longest 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

漫漫学习路 305

leetcode Longest Substring Without Repeating Characters

var lengthOfLongestSubstring = function(s) { let curr = 0; let valuePos = new Map(); const len = s.length; if(len==0) { return 0; } for(let i=0,j=0;i&l...

weixin_42424269的博客 489

【LeetCode】3. Longest Substring Without Repeating Characters

题目: 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. Exampl...

希望的专栏 447
上一篇: *LeetCode-Anagrams
下一篇: LeetCode-Valid Sudoku
bsbcarter
博客等级 码龄16年 0粉丝 226原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值