当前位置:网站首页>Sliding window_
Sliding window_
2022-07-26 00:13:00 【Lin Shu ໌້ᮨ】
1. Power button _ The longest substring without repeating characters
/**
* Longest substring without repeating characters
* Characters stored in set aggregate , If there is a repetition, directly from the previous set Collection removal , Add new characters
*/
import java.util.HashSet;
import java.util.Set;
/**
* Ideas : The sliding window
* With (a)bcabcbb The longest string to start with is (abc)abcbb;
* With a(b)cabcbb The longest string to start with is a(bca)bcbb;
* With ab(c)abcbb The longest string to start with is ab(cab)cbb;
* With abc(a)bcbb The longest string to start with is abc(abc)bb;
* With abca(b)cbb The longest string to start with is abca(bc)bb;
* With abcab(c)bb The longest string to start with is abcab(cb)b;
* With abcabc(b)b The longest string to start with is abcabc(b)b;
* With abcabcb(b) The longest string to start with is abcabcb(b).
* Now observe , We need to remove characters while adding characters . Store characters one by one set aggregate , If the following element has repeated characters , be set The set element is out of the set until there is no such repeating element , Then put this element into set aggregate
*/
public class Num48_ Longest substring without repeating characters {
public int lengthOfLongestSubstring(String s) {
int max=0;
int right=0;
Set<Character> set = new HashSet<>();
for (int left = 0; left < s.length(); left++) {
// The left pointer traverses backward , remove left The previous element of
if(left!=0){
set.remove(s.charAt(left-1));
}
// The left pointer is fixed , The right pointer moves backwards , Elements are not only put in set aggregate , Until I met with set The middle element repeats or goes to the empty record length
while (right<s.length()&&(!set.contains(s.charAt(right)))){
set.add(s.charAt(right));
right++;
}
max=Math.max(max,right-left);
}
return max;
}
}
边栏推荐
- Solid smart contract development - 3.2-solid syntax array, structure, mapping
- The bull market is not over yet, and there is still 2021-05-18 in the second half
- Leetcode high frequency question 66. add one, give you an array to represent numbers, then add one to return the result
- matlab实时作出串口输出数据的图像
- Nest.js 用了 Express 但也没完全用
- SHIB(柴犬币)一月涨幅数百倍,百倍币需具备哪些核心要素?2021-05-09
- VMware ESXI7.0版本的安装与配置
- 栈与队列——150. 逆波兰表达式求值
- BGR and RGB convert each other
- 滑动窗口_
猜你喜欢
34-SparkSQL自定义函数的使用、SparkStreaming的架构及计算流程、DStream转换操作、SparkStreaming对接kafka和offset的处理

06_ UE4 advanced_ Set up a large map using the terrain tool

Stack and queue - 239. Sliding window maximum

Observer model of behavioral model

Binary tree 101. Symmetric binary tree

复盘:推荐系统—— 负采样策略

【一库】mapbox-gl!一款开箱即用的地图引擎

URL address mapping configuration

牛市还没有结束,还有下半场 2021-05-18

栈的表示和实现(C语言)
随机推荐
MPLS实验
06_ue4进阶_使用地形工具设置大地图
VMware ESXI7.0版本的安装与配置
Binary tree -- 700. Search in binary search tree
Js理解之路:Object.call与Object.create()实现继承的原理
二叉树相关知识
Pyqt5 rapid development and actual combat.pdf sharing
The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function
JSON data development
Leetcode high frequency question 66. add one, give you an array to represent numbers, then add one to return the result
What are the precautions for using MySQL index? (answer from six aspects)
Stack and queue - 150. Inverse Polish expression evaluation
MySQL——数据库日志
Js理解之路:什么是原型链
matlab实时作出串口输出数据的图像
Weight file and pre training file of yolov3
C language actual combat guessing game
没错,请求DNS服务器还可以使用UDP协议
FreeRTOS个人笔记-互斥量
Thymeleaf view integration