当前位置:网站首页>leetcode/子串中不能有重复字符的最长子串
leetcode/子串中不能有重复字符的最长子串
2022-08-03 23:20:00 【xcrj】
代码
package com.xcrj;
import java.util.HashSet;
import java.util.Set;
/** * 剑指 Offer II 016. 不含重复字符的最长子字符串 * 子串中不能有重复字符 * 给定一个字符串 s ,请你找出其中不含有重复字符的 最长连续子字符串 的长度。 */
public class Solution16 {
/** * 双指针+set散列表 * 由set是否包含字符,决定左右指针的右移 * set<Character> set.contains()是否包含重复字符 * 不包含重复字符 right右移 * 包含重复字符 left右移 */
public int lengthOfLongestSubstring1(String s) {
Set<Character> set = new HashSet<>(s.length());
int i = 0;
int maxLen = 0;
for (int j = 0; j < s.length(); j++) {
// 不包含重复字符 右指针一直右移
while (i < s.length() && !set.contains(s.charAt(i))) {
set.add(s.charAt(i));
i++;
}
// 包含重复字符 左指针一直右移(尝试把重复的那个字符移除掉)
set.remove(s.charAt(j));
maxLen = Math.max(maxLen, i - j);
}
return maxLen;
}
public static void main(String[] args) {
Solution16 solution16 = new Solution16();
System.out.println(solution16.lengthOfLongestSubstring1("abc"));
}
}
参考
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/wtcaE1/solution/bu-han-zhong-fu-zi-fu-de-zui-chang-zi-zi-4g7m/
来源:力扣(LeetCode)
边栏推荐
猜你喜欢

Pytest学习-setup/teardown

FinClip最易用的智能电视小程序

【开源框架】国内首个通用云计算框架,任意程序都可做成云计算。

逆波兰表达式求值

CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply

生成器版和查看器版有什么区别?

Interpretation of ML: A case of global interpretation/local interpretation of EBC model interpretability based on titanic titanic rescued binary prediction data set using interpret

响应式织梦模板除尘器类网站

易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元

射频芯片(RFIC)的协议之5G及其调制
随机推荐
Interpretation of ML: A case of global interpretation/local interpretation of EBC model interpretability based on titanic titanic rescued binary prediction data set using interpret
Click the icon in Canvas App to generate PDF and save it to Dataverse
Deep integration of OPC UA and IEC61499 (1)
1067 Sort with Swap(0, i)
使用tf.image.resize() 和tf.image.resize_with_pad()调整图像大小
Kotlin - extension functions and operator overloading
Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
3D 语义分割——2DPASS
工作小计 QT打包
Unity2021发布WebGL雾效消失问题
How many way of calling a function?
静态文件快速建站
CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应
AOSP CameraLatencyHistogram的原理与使用
如何创建一个Web项目
剑指offer第22题-链表中倒数第K个节点
OPC UA 与IEC61499 深度融合(1)
跨域的学习
navicat 连接 mongodb 报错[13][Unauthorized] command listDatabases requires authentication
rosbridge-WSL2 && carla-win11