当前位置:网站首页>[leetcode] 3. Longest substring without repeated characters - go language problem solution
[leetcode] 3. Longest substring without repeated characters - go language problem solution
2022-07-25 01:41:00 【Cabbage that wants to become powerful】
List of articles
One 、 Title Description
Given a string s , Please find out that there are no duplicate characters in it Longest substrings The length of .
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 be Substring The length of ,"pwke" Is a subsequence , Not substring .
Tips :
0 <= s.length <= 5 * 104
s By the English letters 、 Numbers 、 Symbols and spaces
Two 、 Their thinking
Create an empty slice for each character in the string ( Finally, the slice of each character stores the longest substring that does not contain repeated characters from that character , Then we can find the longest one ), Then repeat the following process for each character :
- Set the longest string length
maxby 0; - First, add the first character to its empty slice , Traverse backward on the original string , If the characters traversed are inconsistent Any character in the slice repeat , Just add it to the empty slice ;
- When encountering repeated characters, the traversal ends , Calculate whether the current slice length is the longest , Update at the longest max Value ;
- Carry out the above process for all characters in the original string in turn .
3、 ... and 、 My explanation
Go The language code :
func lengthOfLongestSubstring(s string) int {
if len(s) == 0{
return 0
}
max := 0
var flag bool
for i,x := range(s){
// Store from location i Start the longest substring without repeated characters
slice := []rune{
x}
for _,y := range(s[i+1:]){
flag = false // No repetition
for _,p := range(slice){
if p == y {
flag = true // repeat
}
}
if flag == false{
slice = append(slice,y)
}else{
break
}
}
if len(slice) > max{
max = len(slice)
}
}
return max
}
3、 ... and 、 Further optimization
In the above solution, we nested three for loop , There's a lot of time complexity ;( improvement : Double pointer sliding window )
We created a slice for each character , Space complexity is also great ;( improvement : Hashtable )
Here are my improvements (Golang):
func lengthOfLongestSubstring(s string) int {
n := len(s)
max := 0
m := make(map[byte]int)
j := 0 // Point to the next bit of the last bit of the current sliding window
for i,_ := range(s){
//i Is the head of the current sliding window
if i > 0{
m[s[i-1]]--
}
// Slide the end of the window back to find , Until the repetition stops
for j < n && m[s[j]] == 0{
m[s[j]]++
j++
}
if j-i > max{
max = j-i
}
}
return max
}
Judge the result :

边栏推荐
- Amd epyc 9654 Genoa CPU cache test exposure L1 bandwidth up to 30tb/s
- Ireport export PDF font bold failure
- Antdb database products were selected into the global database industry map (2022) of the China Academy of communications and communications
- Worthington carboxyl transfer carbonic anhydrase application and literature reference
- How to implement the server anti blackmail virus system is a problem we have to consider
- Peripherals: timer, watchdog and RTC
- Take C language from 0 to 1 - program structure and use examples
- Data integration | what are the tools for data integration at home and abroad?
- Three modes of executing programs, memory and cache
- Web Security Foundation - file upload
猜你喜欢

Target segmentation for 10000 frames of video, less than 1.4GB of video memory, open source code | ECCV 2022

Young people who lost the IPO

Amd epyc 9654 Genoa CPU cache test exposure L1 bandwidth up to 30tb/s

2022.7.20 linear table

How to implement a state machine?

Actf questions (dropper+master_of_dns)

Green low-carbon Tianyi cloud, a new engine of digital economy!

How to empty localstorage before closing a page
![Deamnet|filenotfounderror: [winerror 3] the system cannot find the specified path.: '/ Datasettest\\Set12‘](/img/7c/0af10dceb20c0b392d0bab749eb355.png)
Deamnet|filenotfounderror: [winerror 3] the system cannot find the specified path.: '/ Datasettest\\Set12‘

What does it operation and maintenance management mean? How to establish an effective IT operation and maintenance management system?
随机推荐
Top priority of dry goods: common indicators and terms in data analysis!
Eolink - quickly develop interfaces through document driven
Beijing Zhun electric clock, Beidou clock server, GPS network time server, NTP satellite timing system
Jsonp solves cross domain plug-ins (JS, TS)
Pychart exits pytest mode (run pytest in mode)
Yolov7:oserror: [winerror 1455] the page file is too small to complete the final solution of the operation
How can arm access the Internet through a laptop?
After burning up 130 billion yuan in ten years, vertical e-commerce will eventually enter the dust of history
Take C language from 0 to 1 - program structure and use examples
[recognize cloud Nativity] Chapter 4 cloud network section 4.9.4.3 - smart network card usage scenario - network acceleration implementation
Fabric. JS centered element
Custom type
Prosci anti-CD22 antibody epratuzum28 flow cytometry display
Interview questions
Kernel structure and design
Cloud native platform, let edge applications play out!
Deamnet|filenotfounderror: [winerror 3] the system cannot find the specified path.: '/ Datasettest\\Set12‘
Open sharing of scientific data in the context of open science: the practice of the national Qinghai Tibet Plateau scientific data center
[summer daily question] Luogu p1605 maze
[26. String hash]