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.Examples:Given “abcabcbb”, the answer is “abc”, which the length is 3.Given “bbbbb”, the answer is “b”, with the le 阅读详情

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


题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

题目大意:找到给定字符串中的最大字串,要求字串中字母不重复。

解题思路:纪录初始起点下标 st  = 0,遍历字符串,用 map 标记从起始位置 st 到当前位置的字串中每个字母的出现次数,如果访问到重复字母,则纪录字串最大长度并更新起始位置 st 及更新map 标记的值。

代码:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int len = s.length();
        if (len == 0)
            return 0;
        else if(len == 1)
            return 1;
        int ans = 1,st = 0;
        map <char,int> mp;
        mp[s[0]] = 1;
        for(int i = 1;i < len;i ++){
            if(mp[s[i]]){
                ans = max(ans,i - st);
                while(mp[s[i]] && st < i){
                    mp[s[st]] -- ;
                    st ++ ;
                }
                mp[s[i]] ++ ;
            }else{
                mp[s[i]] ++;
            }
        }
        ans = max(ans,len - st);
        return ans;
    }
};


LeetCode 题解 3. Longest Substring Without Repeating Characters(最长不含重复字符的子字符串) 阅读详情

相关推荐

零基础用AHK操作Excel:从数据读取到自动填写(附完整脚本解析)

本文为零基础用户详细介绍了如何使用AutoHotkey(AHK)通过COM接口高效操作Excel,实现从数据自动读取到填写的完整自动化流程。文章包含完整的脚本解析、环境搭建指南、核心代码逐行讲解以及实际应用案例,帮助办公人员大幅提升数据处理效率,告别重复劳动。

weixin_29202213的博客 569

[LeetCode]3.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

SmartSi 1908

开源智能相机- Xilinx Zynq-7000高清图像处理模块

Zynq-7000图像处理模块设计资料保存:开源智能相机- Xilinx Zynq-7000高清图像处理模块 一、产品概述 Z-turn Board是一款以Xilinx Zynq-7010/7020全可编程系统级芯片的嵌入式开发板。ARM处理器和FPGA架构紧密集成。拥有双核ARM Cortex-A9 MPCore的高性能,低功耗特性,在设计中能更好的满足各种工业需要。L...

4046

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

简简单单的生活... 449

MoveIt避障规划实战:从点云到OctoMap的环境感知全流程

本文详细解析了MoveIt避障规划中从点云到OctoMap的环境感知全流程,重点介绍了深度相机选型、点云数据处理、OctoMap生成及避障规划参数调优等关键技术。通过实战案例展示了如何利用MoveIt和OctoMap实现机械臂在复杂环境中的高效避障,为工业自动化和服务机器人开发提供实用解决方案。

weixin_30417487的博客 574

警惕AI依赖:如何避免数字水蛭侵蚀你的认知能力

在人工智能技术日益普及的今天,人机协作已成为现代工作流的核心范式。其原理在于通过算法模型辅助人类完成信息处理、内容生成等任务,从而提升效率。然而,过度依赖AI可能导致认知卸载,引发思考能力退化等潜在风险。从技术价值看,AI的核心在于增强而非替代人类智能,关键在于建立健康的协作模式。在应用场景上,无论是编程开发、内容创作还是决策分析,用户都需警惕工具依赖对深度工作、创造性思维和决策责任的侵蚀。本文聚焦于识别AI依赖的早期信号,并提供构建分阶段使用流程、将AI作为质疑者等具体策略,旨在帮助读者守护核心认知能力,

weixin_30240349的博客 646

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

NX二次开发点通过云配准获取相同体

先找到体的参考方向(这个参考方向对于相同体重合之后是相同的),这个时候我们的思路是三个不共线的点确定一个坐标系,然后和绝对方向求转换矩阵。然后获取体的所有边的几何中心,把这些点通过转换矩阵转换之后存起来,再和另外一个体转换之后的点数据作对比,在误差范围内配对点的数量等于体获取点的数量就是相同体

qq_39541524的博客 1663

leetcode--3. Longest Substring Without Repeating Characters

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

林伏案的博客 463

YOLOv11改进 | 网络结构代码逐行解析(一) | 项目目录构造分析

Hello,大家好这次给大家带来的不是改进,是整个YOLOv11项目的分析整个系列大概会更新5-7篇左右的文章从项目的目录到每一个功能代码的都会进行详细的讲解,下面开始进行YOLOv11逐行解析的第一篇——项目目录构造分析开头之前顺便给大家推荐一下我的专栏,本专栏更新上百余篇YOLOv11改进机制手把手教你添加到网络结构中,同时针对拿到模型不知道如何修改,不知道如何发表论文的读者进行针对性的文章介绍,本专栏质量分平均分96分,内容质量完全有所保证。

Snu77的博客 1万+

HTML满屏跳动的爱心(完整代码)

HTML语言实现满屏跳动爱心的完整代码。

AIGC 12万+

[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

AutoSAR系列讲解(实践篇)12.1-Diagnostics简介

主要概括性的讲解了诊断协议UDS和AutoSAR的诊断架构

雪云飞星的博客 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

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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

leetcode第三题

方亮的专栏 1343

IEM模型,地表粗糙度模拟

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

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万+

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

【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 3. Longest Substring Without Repeating Characters

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

温迪今天刷题了吗 275

【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之3. Longest Substring Without Repeating Characters

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

梦想,就现在! 488

[LeetCode] 3. Longest Substring Without Repeating Characters

题目链接: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ DescriptionGiven a string, find the length of the longest substring without repeating characters.Examples

从入门到扑街 425

【leetcode】3. Longest Substring Without Repeating Characters

思路:begin记录longest substring开始的位置;longest当前substring的长度;map存放字符的index。实质上是维护了一个substring区间,使得substring内的所有字符都是独一无二的。 class Solution { public: int lengthOfLongestSubstring(string s) { if (...

zxc120389574的博客 185

LeetCode 3:Longest Substring Without Repeating Characters

滑动窗口方法滑动窗口例题 滑动窗口 滑动窗口是数组/字符串问题中常用的抽象概念。 窗口通常是在数组/字符串中由开始和结束索引定义的一系列元素的集合。 例题 LeetCode 3:无重复字符的最长子串 使用 HashSet 作为滑动窗口, 将字符存储在当前窗口 [i, j)最初 j = i)中。 然后我们向右侧滑动索引 j,如果它不在 HashSet 中,也就是当前窗口没有重复字符时,继续滑动 ,也...

qq_24367757的博客 236

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

leetcode【3】Longest Substring Without Repeating Characters

leetcode【3】Longest Substring Without Repeating CharacterstagsquestionExample:myAnswer 滑动窗口:尾部处理更为重要 tags **hash-table | two-pointers | string | sliding-window ** question Given a string, find the le...

qq_43305329的博客 131
上一篇: Leetcode 21. Merge Two Sorted Lists
下一篇: Leetcode 29. Divide Two Integers
雨狮子
博客等级 码龄12年 24粉丝 185原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址