leetcode算法—无重复字符的最长子串 Longest Substring Without Repeating Characters
关注微信公众号:CodingTechWork,一起学习进步。
题目
Longest Substring Without Repeating Characters:
Given a string, find the length of the longest substring without repeating characters.
无重复字符的最长子串:
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
题解
class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.length() == 0) {
return 0;
}
//HashMap key记录字符,value记录索引
Map<Character, Integer> map = new HashMap<>();
//记录滑动过程中最大子串长度
int max = 0;
//记录窗口左边下标索引
int left = 0;
for (int i = 0; i < s.length(); i++) {
if (map.containsKey(s.charAt(i))) {
//遇到相同的字符,窗口左索引往右移动一位,所以要+1
left = Math.max(left, map.get(s.charAt(i)) + 1);
}
//覆盖重复的key,如abca,第一次是a对应的i=0,第二次put时,a对应的i=3
map.put(s.charAt(i), i);
max = Math.max(max, i-left+1);
}
return max;
}
}
只遍历一次,时间复杂度为o(n)
。
1. 初始状态
2. 滑动i=0,left=0
i = 0时:
- map中不包含a,将a放入map,且map.get(‘a’) = 0,
- left=Math.max(left, map.get(s.charAt(i)) + 1)=0,
- max=Math.max(max, i - left + 1) = 1;
3. 滑动i=1,left=0
i = 1时:
- map中不包含b,将b放入map,且map.get(‘b’) = 1,
- left=Math.max(left, map.get(s.charAt(i)) + 1)=0,
- max=Math.max(max, i - left + 1) = 2;
2. 滑动i=2,left=0
i = 2时,
- map中不包含c,将c放入map,且map.get(‘a’) = 2,
- left=Math.max(left, map.get(s.charAt(i)) + 1) = 0,
- max=Math.max(max, i - left + 1) = 3;
4. 滑动i=3,left=1
i = 3时:
- map中包含a,map.get(s.charAt(i)) = 0, left=Math.max(left, map.get(s.charAt(i)) + 1) = 1,向右移动1位。
- 将a放入map,且map.get(‘a’) = i = 3,
- max=Math.max(max, i - left + 1) = 3;
4. 滑动i=4,left=2
i = 4时:
- map中包含b,map.get(s.charAt(i)) = 1, left=Math.max(left, map.get(s.charAt(i)) + 1) = 2,向右移动1位。
- 将b放入map,且map.get(‘b’) = i = 4,
- max=Math.max(max, i - left + 1) = 3;
5. 滑动i=5,left=3
i = 5时:
- map中包含c,map.get(s.charAt(i)) = 2, left=Math.max(left, map.get(s.charAt(i)) + 1) = 3,向右移动1位。
- 将c放入map,且map.get(‘c’) = i = 5,
- max=Math.max(max, i - left + 1) = 3;
6. 滑动i=6,left=5
i = 6时:
- map中包含b,map.get(s.charAt(i)) = 4, left=Math.max(left, map.get(s.charAt(i)) + 1) =5,向右移动2位。
- 将b放入map,且map.get(‘b’) = i = 6,
- max=Math.max(max, i - left + 1) = 3;
7. 滑动i=7,left=5
i = 7时:
- map中包含b,map.get(s.charAt(i)) = 6, left=Math.max(left, map.get(s.charAt(i)) + 1) =7,向右移动2位。
- 将b放入map,且map.get(‘b’) = i = 7,
- max=Math.max(max, i - left + 1) = 3;
烧不死的鸟就是凤凰
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)