当前位置:网站首页>Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
2022-07-25 20:35:00 【Javanese Muse】
One 、 subject
Given a string s, Please find out There are no duplicate characters Of Longest substrings The length of .
Two 、 Example
Example 1:
【 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 beSubstringThe length of ,"pwke" It's aSubsequence, Not substring .
Tips :
0 <= s.length <= 5 * 104sBy the English letters 、 Numbers 、 Symbols and spaces
3、 ... and 、 Their thinking
3.1> Ideas 1: Brute force
Through two layers for Loop can realize brute force cracking to find non repeating strings , Outermost layer i loop , As the header node of each sub string generation ; The second floor j loop , Each time from i Position start , Assemble substring . Finally, select the longest as the return value . The specific logic is shown in the figure below :

3.2> Ideas 2: The sliding window
adopt start Pointers and i The pointer , We can Construct a window , All elements in the window , There is a very important feature , Namely —— All are No repetition Of . therefore , Elements in the window , There is no need to traverse the comparison . therefore , When duplicate elements are found , We assign values directly start = index(c) -1 To achieve start The jump of . Because in the whole window , In order to start Start with i The end of the , therefore , adopt i - start You can calculate the length of the non repeating string . The specific logic is shown in the figure below :

Four 、 Code implementation
4.1> Realization 1: Brute force
class Solution {
public int lengthOfLongestSubstring(String s) {
int result = 0, temp = 0;
Map<Character, Integer> map;
for (int i = 0; i < s.length(); i++) {
map = new HashMap();
temp = 0;
for (int j = i; j < s.length(); j++) {
if (map.get(s.charAt(j)) != null) {
break;
} else {
map.put(s.charAt(j), j);
result = Math.max(result, ++temp);
}
}
}
return result;
}
}
4.2> Realization 2: The sliding window
class Solution {
public int lengthOfLongestSubstring(String s) {
int[] mark = new int[128];// ascii code
int start = 0, result = 0;
for (int i = start; i < s.length(); i++) {
if (mark[s.charAt(i)] - 1 >= start ) { // Duplicate values encountered , And the dotted subscript value is not less than start Pointer subscript
result = Math.max(result, i - start); // Calculate the current length without duplicate characters
start = mark[s.charAt(i)]; // Reset start The pointer
}
mark[s.charAt(i)] = i + 1;
}
return Math.max(result, s.length() - start);
}
}
That's all for today's article :
Writing is not easy to , An article completed by the author in hours or even days , I just want to get your praise for a few seconds & Share .
More technical dry goods , Welcome everyone to follow the official account “ Java Muse ”(^o^)/~ 「 Dry cargo sharing , Weekly update 」
Previous selections
LeetCode Work: ——565. Array nesting ( difficulty : secondary )
LeetCode Work: ——2. Addition of two numbers ( difficulty : secondary )
LeetCode Work: ——735. Planetary collision ( difficulty : secondary )
LeetCode Work: ——1252. The number of odd cells ( difficulty : Simple )
Title source : Power button (LeetCode)
边栏推荐
- [tensorrt] trtexec tool to engine
- Docker builds redis cluster
- ROS_ Rqt toolbox
- Link list of sword finger offer question bank summary (III) (C language version)
- 参与开源社区还有证书拿?
- Timing analysis and constraints based on xlinx (1) -- what is timing analysis? What are temporal constraints? What is temporal convergence?
- CarSim simulation quick start (XV) - ADAS sensor objects of CarSim sensor simulation (1)
- [advanced mathematics] [1] function, limit, continuity
- 文件操作详解
- Socket error Event: 32 Error: 10053. Connection closing...Socket close
猜你喜欢
![Vulnhub | dc: 6 | [actual combat]](/img/7e/de7d5b56724bde5db2bb8338c35aa8.png)
Vulnhub | dc: 6 | [actual combat]
![[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
![Summarize the level of intelligent manufacturing discussion [macro understanding]](/img/84/3addabdf857c562535a4085782d3e8.png)
Summarize the level of intelligent manufacturing discussion [macro understanding]

Working principle of radar water level gauge and precautions for installation and maintenance

【高等数学】【4】不定积分

Kubernetes进阶部分学习笔记

各厂商网络虚拟化的优势

Introduction to several scenarios involving programming operation of Excel in SAP implementation project
![[paper reading] unpaired image to image translation using cycle consistent advantageous networks](/img/73/69651dd8ecfdddd1cae13a1d223d51.png)
[paper reading] unpaired image to image translation using cycle consistent advantageous networks

Online random coin tossing positive and negative statistical tool
随机推荐
FormatDateTime说解[通俗易懂]
Key network protocols in tcp/ip four layer model
[advanced mathematics] [8] differential equation
Working principle of radar water level gauge and precautions for installation and maintenance
增加 swap 空间
【高等数学】【4】不定积分
TGA file format (waveform sound file format)
第六章 修改规范(SPEC)类
The database empties the table data and makes the primary key start from 1
[paper reading] unpaired image to image translation using cycle consistent advantageous networks
网络爬虫原理解析「建议收藏」
Follow up of Arlo's thinking
QML combines qsqltablemodel to dynamically load data MVC "recommended collection"
[advanced mathematics] [3] Application of differential mean value theorem and derivative
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
RF、GBDT、XGboost特征选择方法「建议收藏」
[cloud native] use of Nacos taskmanager task management
Online random coin tossing positive and negative statistical tool
【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
10. < tag dynamic programming and subsequence, subarray> lt.53. maximum subarray and + lt.392. Judge subsequence DBC