当前位置:网站首页>The longest substring that cannot have repeating characters in a leetcode/substring
The longest substring that cannot have repeating characters in a leetcode/substring
2022-08-03 23:21:00 【xcrj】
代码
package com.xcrj;
import java.util.HashSet;
import java.util.Set;
/** * 剑指 Offer II 016. 不含重复字符的最长子字符串 * Substrings cannot have repeating characters * 给定一个字符串 s ,请你找出其中不含有重复字符的 最长连续子字符串 的长度. */
public class Solution16 {
/** * 双指针+set散列表 * 由set是否包含字符,Determines the right movement of the left and right pointers * set<Character> set.contains()Whether to contain repeating characters * 不contains repeating characters right右移 * contains repeating characters 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++) {
// 不contains repeating characters The right pointer moves all the way to the right
while (i < s.length() && !set.contains(s.charAt(i))) {
set.add(s.charAt(i));
i++;
}
// contains repeating characters The left pointer moves all the way to the right(Try to remove the repeated character)
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)
边栏推荐
- CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply
- What is the difference between the generator version and the viewer version?
- 【深度学习】基于tensorflow的服装图像分类训练(数据集:Fashion-MNIST)
- Zilliz 2023 秋季校园招聘正式启动!
- 关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析
- FinClip,助长智能电视更多想象空间
- 用队列模拟实现栈
- Minimized installation of debian11
- 如何创建一个Web项目
- Unity2021 releases WebGL fog effect disappearing problem
猜你喜欢

用栈实现队列

Flutter教程之为什么 Flutter 是创业的最佳选择?

冰河又一MySQL力作出版(文末送书)!!

设置工作模式与环境(下):探查和收集信息

override learning (parent and child)

CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应

node连接mysql数据库报错:Client does not support authentication protocol requested by server

图论-虚拟节点分层建图

响应式织梦模板塑身瑜伽类网站

First domestic open source framework 】 【 general cloud computing framework, any program can be made into cloud computing.
随机推荐
Quickly build a website with static files
UVa 1025 - A Spy in the Metro (White Book)
utlis thread pool
Creo 9.0创建几何点
Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素科研试剂
伴随着元宇宙、web3.0等概念的兴起,数字人、数字场景等诸多数字化的形态开始出现
Redis persistence method
HCIP BGP实验报告
CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply
代码随想录笔记_动态规划_416分割等和子集
响应式织梦模板塑身瑜伽类网站
剑指offer第22题-链表中倒数第K个节点
How many way of calling a function?
UVa 10003 - Cutting Sticks (White Book, Interval DP)
ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例
.NET6之MiniAPI(十四):跨域CORS(上)
Pytest学习-skip/skipif
win10系统下yolov5-V6.1版本的tensorrt部署细节教程及bug修改
[2022强网杯] polydiv和gamemaster
The salary of soft testers at each stage, come to Kangkang, how much can you get?