当前位置:网站首页>One brush 146 force buckle hot question-3 longest substring without repeated characters (m)
One brush 146 force buckle hot question-3 longest substring without repeated characters (m)
2022-07-03 16:57:00 【Tang and Song Dynasties】
subject :
Given a string s , Please find out that there are no duplicate characters in it Longest substrings The length of .
------------------
Example :
Input : s = "abcabcbb"
Output : 3
explain : Because the longest substring without repeating characters is "abc", So its length is 3.
Example 2:
Input : s = "bbbbb"
Output : 1
explain : Because the longest substring without repeating characters is "b", So its length is 1.
Example 3:
Input : s = "pwwkew"
Output : 3
explain : Because the longest substring without repeating characters is "wke", So its length is 3.
Please note that , Your answer must be Substring The length of ,"pwke" Is a subsequence , Not substring .
Example 4:
Input : s = ""
Output : 0
Tips :
0 <= s.length <= 5 * 104
s By the English letters 、 Numbers 、 Symbols and spaces
------------------
reflection :
label : The sliding window
The time complexity of violent solution is high , Will reach O(n^2)O(n 2), Therefore, the sliding window method is adopted to reduce the time complexity
Define a map Data structure storage (k, v), among key The value is the character ,value Value is character position +1,
Add 1 Indicates that the character is not repeated until after the character position
We define the starting position of non repeating substring as start, End position is end
With end Go back and forth , Will meet with [start, end] If the characters in the interval are the same , In this case, the character is used as key value ,
Get its value value , And update the start, here [start, end] There are no repeating characters in the interval
Whether updated or not start, Will update it map Data structure and results ans.
Time complexity :O(n)O(n)
Code
Java
class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) return 0;
int res = 0;
Map<Character, Integer> map = new HashMap<>();
for (int start = 0, end = 0; end < s.length(); end++) {
char ch = s.charAt(end);
if (map.containsKey(ch)) {
start = Math.max(map.get(ch), start);
}
map.put(ch, end + 1);
res = Math.max(res, end - start + 1);
}
return res;
}
}
边栏推荐
- MySQL converts comma separated attribute field data from column to row
- [combinatorics] recursive equation (constant coefficient linear homogeneous recursive equation | constant coefficient, linear, homogeneous concept description | constant coefficient linear homogeneous
- ucore概述
- visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
- What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
- [sword finger offer] 58 - I. flip the word order
- IDEA-配置插件
- [combinatorics] polynomial theorem (polynomial coefficients | full arrangement of multiple sets | number of schemes corresponding to the ball sub model | polynomial coefficient correlation identity)
- [try to hack] active detection and concealment technology
- 跨境电商:外贸企业做海外社媒营销的优势
猜你喜欢

Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer

Bcvp developer community 2022 exclusive peripheral first bullet

MySQL single table field duplicate data takes the latest SQL statement

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises

Leetcode: lucky number in matrix

Meituan side: why does thread crash not cause JVM crash

NSQ source code installation and operation process

Mysql database DDL and DML

Prepare for the golden three silver four, 100+ software test interview questions (function / interface / Automation) interview questions. win victory the moment one raises one 's standard

PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
随机推荐
Execute script unrecognized \r
静态程序分析(一)—— 大纲思维导图与内容介绍
Assembly instance analysis -- screen display in real mode
visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
[combinatorial mathematics] counting model, common combinatorial numbers and combinatorial identities**
A survey of state of the art on visual slam
建立自己的网站(23)
Netease UI automation test exploration: airtest+poco
斑馬識別成狗,AI犯錯的原因被斯坦福找到了
线程池:业务代码最常用也最容易犯错的组件
Necessary ability of data analysis
Central South University | through exploration and understanding: find interpretable features with deep reinforcement learning
Kindeditor editor upload image ultra wide automatic compression -php code
Arduino esp32: overall framework of lvgl project (I)
NSQ source code installation and operation process
What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR
Difference between JSON and bson
Top k questions of interview
QT serial port UI design and solution to display Chinese garbled code
Thread pool: the most common and error prone component of business code