当前位置:网站首页>每日一题-无重复字符的最长子串-0712
每日一题-无重复字符的最长子串-0712
2022-08-05 05:17:00 【菜鸡程序媛】
题目:给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
解题思路:在map中存放字符串字符,当存在已有字符和当前的字符相等时,要更新滑动窗口的start,要取max(start, s.charAt(i) +1),因为当存在的相等的字符在更前面时,就会造成错误的区域划分,如"tmmzuxt",不取最大值的话,就会把第一个m当成新的开始值,但是m自身已经是重复的字符了。
其实可以这样想,如果start没有变大的话,就证明一直没有重复的字符出现过;既然有更大的值,就证明前面已经有重复的了,只能取更大的。即滑动窗口的左边届开始的后面不应该有重复的字符。
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0)
return 0;
int left = 0;
int max = 0;
Map<Character, Integer> map = new HashMap<>();
for(int i = 0; i < s.length(); i ++){
if(map.containsKey(s.charAt(i)))
left = Math.max(left, map.get(s.charAt(i)) + 1);
// 在更新重复的字符,如上面的第一个m和第二个m,第二个m写入就会把第一个覆盖掉
map.put(s.charAt(i), i);
max = Math.max(max, i - left + 1);
}
return max;
}
}
边栏推荐
- LeetCode刷题之第54题
- 基于STM32F407的WIFI通信(使用的是ESP8266模块)
- It turns out that the MAE proposed by He Yuming is still a kind of data enhancement
- 读论文 - Unpaired Portrait Drawing Generation via Asymmetric Cycle Mapping
- LeetCode刷题之第746题
- 【ts】typescript高阶:条件类型与infer
- 基于STM32F4的FFT+测频率幅值相位差,波形显示,示波器,时域频域分析相关工程
- 浅谈遇到的小问题
- 五、请求处理—Rest映射是怎样实现的?
- leetCode刷题之第31题
猜你喜欢

LeetCode刷题之第55题

【Shell编程】第一章:子串

五、请求处理—Rest映射是怎样实现的?

【ts】typescript高阶:模版字面量类型

读论文- pix2pix

【UiPath2022+C#】UiPath Switch
![[Intensive reading of the paper] R-CNN's Bounding box regression problem is detailed](/img/ef/a058ec08bd0a6313e3610a4ebc9685.png)
[Intensive reading of the paper] R-CNN's Bounding box regression problem is detailed

SQL (2) - join window function view

LeetCode刷题之第530题

Detailed explanation of BroadCast Receiver (broadcast)
随机推荐
伪RTOS-ProroThread在CH573芯片上的移植
【UiPath2022+C#】UiPath 练习和解决方案-变量、数据类型和控制流程
[Kaggle project actual combat record] Steps and ideas sharing of a picture classification project - taking leaf classification as an example (using Pytorch)
2021电赛资源及经验总结
Redis集群(docker版)——从原理到实战超详细
每日一题-单调栈
电子产品量产工具(4)-UI系统实现
电子产品量产工具(5)- 页面系统实现
每日一题-括号生成-0721
网络通信及相关函数介绍
dataframe 常用操作
CVPR2021 - Inception Convolution with Efficient Dilation Search
(C语言)动态内存管理
SQL (2) - join window function view
深度学习系列(一)简介、线性回归与成本函数
Redis设计与实现(第二部分):单机数据库的实现
6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!
CVPR 2022 | 70% memory savings, 2x faster training
栈的应用——力扣 20.有效的括号
函数在开发环境中的应用(简易实例)