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 ...
随机推荐
- mac 安装 php7 及扩展
mac 版本号:10.12.3 (16D30) 安装内容 php7.0.18(配置apache),composer,phpunit,xdebug扩展,docopts,mongo和redis扩展 php ...
- Centos固定IP
centos7 联网 在虚拟机中以最小化方式安装centos7,后无法上网,因为centos7默认网卡未激活. 而且在sbin目录中没有ifconfig文件,这是因为centos7已经不使用 ifco ...
- 【原创】大叔经验分享(54)flume kudu sink运行一段时间kudu client报错
flume kudu sink运行一段时间报错: 19/05/05 10:15:56 WARN client.ConnectToCluster: Error receiving a response ...
- 腾讯地图JSAPI开发demo 定位,查询
1.IP定位切换 2.点击坐标获取地点 3.查询地点切换坐标 <!DOCTYPE html> <html> <head> <meta http-equiv=& ...
- 防抖与节流函数<转>
参考连接:https://www.cnblogs.com/zhuanzhuanfe/p/10633019.html https://blog.csdn.net/Beijiyang999/article ...
- Ubuntu 18.04 LTS 64位Linux搭建Kubernetes 1.15.3并join子节点的完整过程
1.软件准备 1.1.Ubuntu系统安装 https://ubuntu.com/download#download ubuntu系统需要设置用户,root默认为系统的账户不能被用户设置且每一次开机都 ...
- dedecms 公共模板写法 提高生成速度
{dede:include file="/temp/liuxingfushi.html" ismake='no'/}
- enum:python实现枚举也很优雅
介绍 enum是一个用来枚举的模块 创建枚举类型 import enum # 创建一个类,继承自enum下的Enum class Color(enum.Enum): red = 1 green = 2 ...
- mysql将一个表中字段A的值赋给另一个表的字段B
# mysql 的修改方法 update table_a a inner join table_b b on b.id=a.id set a.description=b.content; # mssq ...
- uestc summer training #1
A 一个很好想的dp ll dp[maxn][]; int main() { scanf("%d%d",&n,&k); memset(dp,,sizeof(dp)) ...