LeetCode 第 3 题:无重复字符的最长子串(滑动窗口)
LeetCode 第 3 题:无重复字符的最长子串 (滑动窗口)
方法:滑动窗口
滑动窗口模板问题:右指针先走,满足了一定条件以后,左指针向前走,直到不满足条件。
特点:左右指针的方向是一致的,并且是不回头的。
C++ 代码:
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int size = s.size();
if (size < 2) {
return size;
}
int left = 0;
int right = 0;
int res = 0;
int count = 0;
int freq[128] = {0};
while (right < size) {
if (freq[s[right]] == 1) {
count++;
}
freq[s[right]]++;
right++;
while (count > 0) {
if (freq[s[left]] == 2) {
count--;
}
freq[s[left]]--;
left++;
}
res = max(res, right - left);
}
return res;
}
};
Java 代码:
/**
* @author liweiwei1419
* @date 2019/9/23 11:34 下午
*/
public class Solution5 {
public int lengthOfLongestSubstring(String s) {
int len = s.length();
if (len < 2) {
return len;
}
int res = 0;
// 表示边界条件,重复次数
int count = 0;
int[] hash = new int[128];
int left = 0;
int right = 0;
while (right < len) {
if (hash[s.charAt(right)] == 1) {
// 当前看到的 right 多于 1 个,说明此时的滑动窗口有重复元素
count++;
}
hash[s.charAt(right)]++;
right++;
while (count > 0) {
// 如果正好遇到重复的那个字符,就可以退出循环了
if (hash[s.charAt(left)] == 2) {
count--;
}
hash[s.charAt(left)]--;
left++;
}
// 此时 (left, right] 这个区间内没有重复元素
// (3, 5],[4,5]
res = Math.max(res, right - left);
}
return res;
}
}
Python 代码:
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
size = len(s)
if size < 2:
return size
left = 0
right = 0
hash = [0] * 128
res = 0
count = 0
while right < size:
if hash[ord(s[right])] == 1:
count += 1
hash[ord(s[right])] += 1
right += 1
while count == 1:
if hash[ord(s[left])] == 2:
count -= 1
hash[ord(s[left])] -= 1
left += 1
res = max(res, right - left)
return res
LeetCode 第 3 题:无重复字符的最长子串(滑动窗口)的更多相关文章
- LeetCode 第三题--无重复字符的最长子串
1. 题目 2.题目分析与思路 3.思路 1. 题目 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3 ...
- [LeetCode]3. 无重复字符的最长子串(滑动窗口)
题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc ...
- leetcode的Hot100系列--3. 无重复字符的最长子串--滑动窗口
可以先想下这两个问题: 1.怎样使用滑动窗口? 2.如何快速的解决字符查重问题? 滑动窗口 可以想象一下有两个指针,一个叫begin,一个叫now 这两个指针就指定了当前正在比较无重复的字符串,当再往 ...
- Leetcode(3)无重复字符的最长子串
Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...
- LeetCode刷题--无重复字符的最长子串(中等)
题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 " ...
- 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)
目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...
- leetcode题解#3:无重复字符的最长子串
leetcode题解:无重复字符的最长子串 题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: s = "abcabcbb"输出: 3 解释 ...
- Leetcode(3)-无重复字符的最长子串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. 给定 &q ...
- leetcode刷题第三天<无重复字符的最长子串>
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 : 输入: "abcabcbb" 输出: 解释: 因为无重复字符的最长子串是 . 示例 : 输入: &quo ...
随机推荐
- 三维数点的CDQ分治板子
int n, k, tot; struct _ {int x,r,f;} a[N]; struct __ { int type; ll x,y; bool operator < (const _ ...
- Dapper多表查询时子表字段为空
最近在学习使用Dapper时百度了一篇详细使用教程,在做到多表查询的时候,出现如下情况. 使用的SQL如下, SELECT * FROM [Student] AS A INNER JOIN [Juni ...
- python之BeautifulSoup4
阅读目录 1.Beautiful Soup4的安装配置 2.BeautifulSoup的基本用法 (1)节点选择器(tag) (2)方法选择器 (3)CSS选择器 (4)tag修改方法 Beautif ...
- js之运算符(逻辑运算符)
逻辑运算符通常用于布尔型(逻辑)值.这种情况下,它们返回一个布尔值.它经常和关系运算符一起配合使用.“&&” .“!”和“ ||” 运算符会返回一个指定操作数的值,因此,这些运算符也用 ...
- pip 报错找不到pip问题
具体报错如下 解决办法: wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate 使用当前python3运行
- CentOS 7系统时间与实际时间差8个小时
1.查看系统时间: [root@localhost sysconfig]# timedatectl Local time: 一 2017-11-06 21:13:19 CST Universal ti ...
- 4.(基础)tornado应用安全与认证
这一节我们介绍应用安全与认证,其实中间省略了一个数据库.对于tornado来说,读取数据库的数据,性能的瓶颈还是在数据库上面.关于数据库,我在<>中介绍了sqlalchemy,这是一个工业 ...
- python中的else语句
python语言和其它语言一样在支持else语句,通常else语句和if语句合用,完成程序的分支选择功能. 例如如下打印学成成绩代码: score = int(input("请输入成绩:&q ...
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
D. Arpa and a list of numbers time limit per test 2 seconds memory limit per test 256 megabyte ...
- string::empty
bool empty() const noexcept;注:判断string对象是否为空,为空返回true #include <iostream>#include <string&g ...