思路:滑动窗口的思想

方法一:滑动窗口

    int lengthOfLongestSubstring(string s) {
/*
控制一个滑动窗口,窗口内的字符都是不重复的,通过set可以做到判断字符是否重复
*/
unordered_set<char> set;
size_t maxL=;
for(int l=,r=;r<s.size();++r)
{
if (!set.count(s[r])) // 当前判断的元素不存在于滑动窗口[l,r-1]中
set.insert(s[r]); // 将元素放入滑动窗口(即记录不重复字符)
else // 当前判断的元素已经存在于滑动窗口[l,r-1]中
{
while(set.count(s[r])) // 从左缩短窗口,直到剔除当前判断的元素为止
set.erase(s[l++]);
set.insert(s[r]); // 将当前判断元素放入到滑动窗口中
}
maxL=max(maxL,set.size()); // 更新无重复字符的最长子串
}
return maxL;
}

【leetcode 3. 无重复字符的最长子串】解题报告的更多相关文章

  1. Leetcode(三)无重复字符的最长子串

    3. 无重复字符的最长子串 题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最 ...

  2. 【LeetCode】无重复字符的最长子串【滑动窗口法】

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc&quo ...

  3. [LeetCode] 3. 无重复字符的最长子串

    题目链接:(https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/) 题目描述: 给定一个字符 ...

  4. LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters

    题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...

  5. Leetcode——3. 无重复字符的最长子串

    难度: 中等 题目 Given a string, find the length of the longest substring without repeating characters. 给定一 ...

  6. 力扣Leetcode 3. 无重复字符的最长子串

    无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串 ...

  7. [LeetCode]3. 无重复字符的最长子串(滑动窗口)

    题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc ...

  8. [LeetCode]3.无重复字符的最长子串(Java)

    原题地址: longest-substring-without-repeating-characters/submissions 题目描述: 示例 1: 输入: s = "pwwkew&qu ...

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

    题目描述 给定一个字符串,找出不含有重复字符的最长子串的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. ...

随机推荐

  1. 第一篇:Git操作详解

    最近由于项目的需要,我需要负责整个项目的托管,其中涉及到很多Git相关的命令,所以就将之前用到的git相关的命令做了一个总结和归纳.由于开发环境是Linux,所以我接下来的操作命令均针对Linux环境 ...

  2. java:安装Runtime Environment,设置Tomcat Server 的方法

    Eclipse 中开发Webapp, 一般需要配置Tomcat Server, 以便在Eclipse 中进行Debug.具体的步骤如下: 1. Windows ==>Preference ==& ...

  3. django 使用内建过滤器实现文章摘要效果

    django 使用内建过滤器实现文章摘要效果 前端html代码 <div class="list-group"> {% if articles %} {% for ar ...

  4. struts2 框架 的环境搭建 与配置

    一,Struts2简介: 1,来由:Struts(金属支架),在程序中表示起支撑作用的通用程序代码,Struts2是在Struts1框架的基础上融合了WebWork优秀框架升级得到的. 2,解释:St ...

  5. 【leetcode刷题笔记】Plus One

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  6. 【leetcode刷题笔记】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  7. POJ2891Strange Way to Express Integers (线性同余方程组)

    Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...

  8. Operating System-Thread(2) Multi-Process-Parallel vs Multi-Thread-Parallel

    本文主要介绍线程的模型 一.Multi-Process-Parallel vs Multi-Thread-Parallel 多进程的并行:CPU在多个进程之间进行切换,系统给人一种多个进程在并行执行的 ...

  9. JAVA的推荐书目

    本文是摘自别人的网站,自己读的书少,谨以此作为自己要读的书的一个书目列表吧. 原文地址:http://blog.sina.com.cn/s/blog_6aa1784101011hl5.html 正文: ...

  10. CentOS6.5中的vsftpd安装配置

    安装ftp 1.使用chkconfig 来查看是否装有vsftpd服务: 2.使用yum命令直接安装:yum -y install vsftpd 3.然后为它创建日志文件:touch /var/log ...