leetcode hot 10
1.leetcode hot 012.leetcode hot 023.leetcode hot 044.leetcode hot 035.leetcode hot 056.leetcode hot 067.leetcode hot 078.leetcode hot 089.leetcode hot 09
10.leetcode hot 10
11.leetcode hot 1212.leetcode hot 1313.leetcode hot 1414.leetcode hot 1515.leetcode hot 1116.leetcode hot 1617.leetcode hot 1718.leetcode hot 1819.leetcode hot 19解题思路:滑动窗口问题,关键在于记录之前出现过的字符,当遍历过程中碰到之前出现的,就从之前出现的下一个作为start,实时比较当前值和最长值并赋值,最后返回最长值。java可以采用hashmap记录出现过的字符,value保存它的位置+1的值,作为出现重复的字符的时候的start的参考,取max(start,hashmap.get(str));或者用deque,当出现重复的字符就将之前的元素丢出直到丢出重复的那个字符为止,计算长度。
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character,Integer> hashmap = new HashMap<>();
int maxlen = 0;
int start = 0;
for(int end = 0;end < s.length();end++){
char str = s.charAt(end) ;
if(hashmap.containsKey(str)){
start = Math.max(hashmap.get(str), start);
}
if(maxlen<end-start+1){
maxlen = end-start+1;
}
hashmap.put(str,end+1);
}
return maxlen;
}
}
class Solution {
public int lengthOfLongestSubstring(String s) {
char[] strs = s.toCharArray();
Deque deque = new LinkedList();
int maxlen = 0;
//peek offer poll
for(char str:strs){
while(deque.contains(str)){
deque.pollLast();
}
deque.offerFirst(str);
if(maxlen<deque.size()) maxlen = deque.size();
}
return maxlen;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)