当前位置:网站首页>每日一题-无重复字符的最长子串
每日一题-无重复字符的最长子串
2022-07-05 05:26:00 【ThE wAlkIng D】
题目描述
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
问题解析(本题使用滑动窗口+HashMap)
- 首先建立一个map集合(记录不同字符串的存放位置)以及一个临时变量存储最长字符长度
- 使用双指针 end, start;遍历字符串,首先把end指针的字符取出来
- 如果map集合有相同的字符,更改起始位置
- 否则就更新Res的值,在map集合存储不相同字符以及字符的位置。
代码实例
class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length();
int res = 0;
Map<Character,Integer> map = new HashMap<>();
for(int end = 0, start = 0; end < n; end++){
char c = s.charAt(end);
if(map.containsKey(c)){
start = Math.max(map.get(c),start);
}
res = Math.max(res,end - start + 1);
map.put(s.charAt(end),end + 1);//为什么end+1需要注意下,start保证start起始位置要在重复字符串的下一位。
}
return res;
}
}
边栏推荐
- Summary of Haut OJ 2021 freshman week
- [merge array] 88 merge two ordered arrays
- A new micro ORM open source framework
- [to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
- Demonstration of using Solon auth authentication framework (simpler authentication framework)
- High precision subtraction
- Learning notes of "hands on learning in depth"
- Development error notes
- 读者写者模型
- SAP-修改系统表数据的方法
猜你喜欢

质量体系建设之路的分分合合

Heap sort summary

Service fusing hystrix

剑指 Offer 53 - I. 在排序数组中查找数字 I

On-off and on-off of quality system construction

A new micro ORM open source framework

Reverse one-way linked list of interview questions
![[转]MySQL操作实战(一):关键字 & 函数](/img/b1/8b843014f365b786e310718f669043.png)
[转]MySQL操作实战(一):关键字 & 函数

Applet live + e-commerce, if you want to be a new retail e-commerce, use it!

Fragment addition failed error lookup
随机推荐
Demonstration of using Solon auth authentication framework (simpler authentication framework)
[binary search] 69 Square root of X
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
SAP-修改系统表数据的方法
第六章 数据流建模—课后习题
Optimization scheme of win10 virtual machine cluster
Introduction to memory layout of FVP and Juno platforms
Insert sort
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
Heap sort summary
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
[to be continued] [depth first search] 547 Number of provinces
Yolov5 adds attention mechanism
Csp-j-2020-excellent split multiple solutions
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
[turn]: Apache Felix framework configuration properties
质量体系建设之路的分分合合
[轉]: OSGI規範 深入淺出
Haut OJ 1347: addition of choice -- high progress addition
Solon Auth 认证框架使用演示(更简单的认证框架)