当前位置:网站首页>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)
边栏推荐
- QQ是32位还是64位软件(在哪看电脑是32位还是64位)
- How to use buffer queue to realize high concurrent order business (glory Collection Edition)
- Vivo official website app full model UI adaptation scheme
- 【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
- 参与开源社区还有证书拿?
- [paper reading] unpaired image to image translation using cycle consistent advantageous networks
- Clickhouse notes 02 -- installation test clickvisual
- Array of sword finger offer question bank summary (I) (C language version)
- 文件操作详解
- Principle analysis of bootloader
猜你喜欢
![[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference](/img/0f/8ce2d5487b16d38a152cfd3ab454bb.png)
[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference
![[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now

Notes - record a cannotfinddatasourceexception: dynamic datasource can not find primary datasource problem solving
![[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console

程序的编译和运行

【单细胞高级绘图】07.KEGG富集结果展示
![[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

各厂商网络虚拟化的优势

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

Kubernetes advanced part learning notes
随机推荐
Network protocol: TCP part2
Online random coin tossing positive and negative statistical tool
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
[Infographics Show] 248 Public Domain Name
Apache MINA框架「建议收藏」
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born
[cloud native] use of Nacos taskmanager task management
QQ是32位还是64位软件(在哪看电脑是32位还是64位)
Google pixel 6A off screen fingerprint scanner has major security vulnerabilities
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
[paper reading] unpaired image to image translation using cycle consistent advantageous networks
Chapter VI modified specification (SPEC) class
process.env
Working principle of radar water level gauge and precautions for installation and maintenance
Do you still have certificates to participate in the open source community?
Formatdatetime explanation [easy to understand]
Web crawler principle analysis "suggestions collection"
DIY个人服务器(diy存储服务器)
Socket error Event: 32 Error: 10053. Connection closing...Socket close
MySQL 日期【加号/+】条件筛选问题