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 ...
随机推荐
- ProGuard 最全混淆规则说明
Input/Output Options 输入输出选项 -include filename 递归引入目录的配置文件 -basedirectory directoryname -injars class ...
- Two strings CodeForces - 762C (字符串,双指针)
大意: 给定字符串$a$,$b$, $b$可以任选一段连续的区间删除, 要求最后$b$是$a$的子序列, 求最少删除区间长度. 删除一段连续区间, 那么剩余的一定是一段前缀和后缀. 判断是否是子序列可 ...
- leecode刷题(31) -- 回文数
leecode刷题(31) -- 回文数 回文数 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输 ...
- 1.bash总体介绍
1.总体介绍1.1 什么是Bash?Bash(Borune-Again SHell)是一个用于Linux操作系统的shell,即命令解释器Bash与sh兼容,并从ksh和csh引进了一些有用的功能,在 ...
- js变量的作用域、变量的提升、函数的提升
变量的作用域在函数之外声明的变量,叫做全局变量,因为它可被当前文档中的任何其他代码所访问.在函数内部声明的变量,叫做局部变量,因为它只能在当前函数的内部访问. ECMAScript 6 之前的 Jav ...
- postgres外部表
在创建外部表的时候遇见: CREATE EXTENSION file_fdw;2018-12-21 17:32:23.822 CST [31237] ERROR: could not open ex ...
- Django的MySQL Driver配置
PEP 249规定了Python的数据库API.MySQL主要有三种API实现: MySQLdb 是Andy Dustman开发的.使用原生代码/C语言绑定的驱动,它已经开发了数十年. mysqlcl ...
- mysql explain解析一 extra中的using index,using where,using index condition
1.简单介绍 using index 和using where只要使用了索引我们基本都能经常看到,而using index condition则是在mysql5.6后新加的新特性,我们先来看看mysq ...
- QString与QByteArray互相转换的方法
本文转载自http://blog.csdn.net/daa20/article/details/51674753 // QString转QByteArray方法 //Qt5.3.2 QString s ...
- CAS JDK 证书错误学习笔记
通过之前生产上发现的问题总结得出以下结论: 问题现象就是:由F5 进行分发到cas 两个服务端 导致 客户端访问时 (时好时坏的现象 ) 通过在服务端的查看apahce 的访问日志得出的结论 发 ...