当前位置:网站首页>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;
}
}
边栏推荐
- Stm32- analyze latency based on assembly
- LeetCode_55_跳跃游戏
- Binary tree - 617. Merge binary tree
- Elementary C language - branch statements (if, switch)
- Leetcode169-多数元素详解
- Binary tree - 112. Path sum
- 多任务编程
- SHIB(柴犬币)一月涨幅数百倍,百倍币需具备哪些核心要素?2021-05-09
- Pytoch learning record (I): introduction to pytoch
- How long can this bull market last Answers to questions 2021-05-11
猜你喜欢

The items of listview will be displayed completely after expansion

痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异

NVIDIA cudnn learning

Getaverse, a distant bridge to Web3

如何用120行代码,实现一个交互完整的拖拽上传组件?

MySQL——数据库日志

NVIDIA cuDNN学习

Stm32 systeminit trap during simulation debugging

"Demons dance", is the bull market over? 2021-05-13

Leetcode169 detailed explanation of most elements
随机推荐
BOM browser object model
Binary tree 101. Symmetric binary tree
Leetcode169 detailed explanation of most elements
URL address mapping configuration
Module II operation
如何让你的 JS 代码写得更漂亮
Old laptop becomes server (laptop + intranet penetration)
Solidity智能合约开发 — 3.2-solidity语法数组、结构体、映射
VMware ESXI7.0版本的安装与配置
MPLS实验
LeetCode 刷题系列 -- 931. 下降路径最小和
Pyqt5 rapid development and actual combat.pdf sharing
Binary tree - 404. Sum of left leaves
Leetcode shahutong series -- 63. Different paths II
Get JD product details original data API
本轮牛市还能持续多久?|疑问解答 2021-05-11
京东按关键字搜索商品 API 的使用说明
Js理解之路:Object.call与Object.create()实现继承的原理
After using MQ message oriented middleware, I began to regret
Recent impressions about bull market and defi 2021-05-17