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. Linux文件权限学习总结

    一.用户对文件或目录都有哪些权限? 四种:读.写.执行.没有权限 二.如何表示这四种权限? 如果用十进制数字表示,分别为:4.2.1.0:如果用字符表示,分别为:r.w.x.-.个人觉得,使用chmo ...

  2. js传带参数的函数

    字符串: setTimeout('pageScroll(4)',100);

  3. 获取本机IP、mac地址、计算机名

    python获取本机IP.mac地址.计算机名 在python中获取ip地址和在php中有很大不同,我们先来看一下python 获得本机MAC地址: >>> import uuid ...

  4. python基础教程笔记—画幅好画(详解)

    今天写一下基础教程里面的第二个项目,主要使用python来做一个pdf的图,比较简单. 首先我们需要安装用到的模块pip install reportlab即可. 书上是用urlopen从往上下了一个 ...

  5. C#中获得汉字的首拼音(加强版)

    /// <summary> /// 汉字拼音首字母列表 /// 包含了20901个汉字,收录的字符的Unicode编码范围为19968至40869 /// </summary> ...

  6. POJ 2886 Who Gets the Most Candies? 线段树

    题目: http://poj.org/problem?id=2886 左右转的果断晕,题目不难,关键是准确的转啊转.因为题目要求输出约数个数最多的数,所以预处理[1,500000]的约数的个数就行了. ...

  7. Entity Framework Code First 数据迁移

    需要在[工具 --> NuGet 程序包管理器 --> 程序包管理器控制台]中输入三个命令: Enable-Migrations (初次迁移时使用) Add-Migration [为本次迁 ...

  8. Cocoapod安装 - 管理第三方库

    在我们开发移动应用的时候,一般都会使用到第三方工具,而由于第三方类库的种类繁多,我们在项目中进行管理也会相对麻烦,所以此时我们就需要一个包管理工具,在iOS开发中,我们使用最多的就是Cocoapods ...

  9. Docker的安装及简单使用

    1.  Docker的安装 (这里的“安装docker”其实就是安装Docker Engine) $ sudo apt-get intasll docker.io note: apt-get是ubun ...

  10. 几MB的大图片变成几百KB

    使用windows自带的“画图”工具就可以. 1.用“画图”打开图片. 2.点击“重新调整大小” 弹出如下窗口 修改这里的“水平”和“垂直”,如都从100改为30.改完之后,点击确定,最后再“保存”或 ...