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. For "bbbbb" the longest substring is "b", with the length of 1.

问题:找到字符串中最长的无重复字符的子字符串。

这题是求最长连续子字符串,可以使用 滑动窗口算法(Slide Window Algorithm)求解,和 Minimum Size Subarray Sum 相似。

设下标 l 和 r, 把左开右闭 [l, r) 想象成一个窗口。

  • 当 s[r] 和窗口内字符重复时, 则 l 向右滑动,缩小窗口。
  • 当s[r] 和窗口内字符不重复时,则 r 向右滑动,扩大窗口,此时窗口内的字符串一个无重复子字符串。

在所有的无重复子字符串中,找到长度最大的,即为原问题解。

int lengthOfLongestSubstring(string s) {

    int l = ;
int r = ; unordered_set<int> setv;
setv.insert(s[l]); int longest = ; while (r < s.size()) { if (setv.count(s[r]) != ) {
setv.erase(s[l]);
l++;
}else{
setv.insert(s[r]);
r++;
longest = max(longest, (int)setv.size());
}
} return longest;
}

[LeetCode] 3. Longest Substring Without Repeating Characters 解题思路的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

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

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

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

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  4. Leetcode:Longest Substring Without Repeating Characters 解题报告

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  5. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  6. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

  7. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  8. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. Java [leetcode 3] Longest Substring Without Repeating Characters

    问题描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

随机推荐

  1. spark-shell 执行脚本并传入参数

    使用方法: ./spark-script.sh your_file.scala first_arg second_arg third_arg 脚本: scala_file=$ arguments=$@ ...

  2. mysql导入导出.sql文件 备份还原数据库

    从数据库导出数据库文件:   进入你的MySQL的安装目录的bin目录或者在C盘的根目录都行,我选的是在bin目录下,下面的例子出第一个外将以在C盘的根目录来讲解   我的mysql安装在了C盘,C: ...

  3. ASP.NET MVC5 easyui 之 treegrid 初用记录

    菜鸟初次使用,参考论坛中介绍的方法仍走了一些弯路,把自己遇到的问题记录下来. 1.必须定义根节点: 2.根节点一个或多个均可: 4.根节点的父节点属性不必定义,或者定义为0: 5.各级子节点的父节点属 ...

  4. 深入了解line-height

    1.定义   行高:两行文字baseline(基线)之间的距离 示意图: 2.为何line-height可以让单行文本垂直居中       其实并没有垂直居中,除非将font-size:0; 3.li ...

  5. python 中对list做减法操作

    问题描述:假设我有这样两个list,          一个是list1,list1 = [1, 2, 3, 4, 5]          一个是list2,list2 = [1, 4, 5]     ...

  6. osg学习笔记2, 命令行参数解析器ArgumentParser

    ArgumentParser主要负责命令行参数的读取 #include <osgDB/ReadFile> #include <osgViewer/Viewer> int mai ...

  7. 在ubuntu下利用minicom实现串口通信

    windos有串口调试助手,linux下也有这样的工具——minicom.不过,minicom和linux下的许多工具都一样,也是命令行模式,没有图形化界面供我们享受.作为一款串口调试工具,虽然难看但 ...

  8. Computer Vision Applied to Super Resolution

    Capel, David, and Andrew Zisserman. "Computer vision applied to super resolution." Signal ...

  9. IntelliJ IDEA配置缓存地址

    idea的配置缓存路径是保存配置文件和插件的本地目录,对idea上的所有个人修改全都储存在那里 相比于eclipse的免安装.解压即用来说,一旦系统重装或者idea重装又没有进行备份的情况下,个人对i ...

  10. CSS中IE8和chrom像素百分比计算差异

    IE8中和chrome在计算像素百分比上,IE8舍一位取元素像素大小,chrome则使用四舍五入取元素像素大小: 比如:<body><div stype=“width:30%”> ...