Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use leetcode to realize the longest substring without repeating characters in golang

Shulou Source: shulou.com Published: 2022-06-02 07:44:50 09月14日 Update

This article introduces how to use leetcode to achieve the longest substring without repeating characters in golang. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Given a string, please find out the length of the longest substring that does not contain repeating characters.

Example 1:

Input: "abcabcbb" output: 3 explanation: because the longest substring without repeating characters is length 3.

Example 2:

Input: "bbbbb" output: 1 explanation: because the longest substring without repeating characters is, its length is 1.

Example 3:

Input: "pwwkew" output: 3 explanation: because the longest substring without repeating characters is, its length is 3. Please note that your answer must be the length of the substring, not a substring.

Ideas for solving the problem:

1, this is a sliding window problem, you need to move the left and right pointer

2. To judge whether a character is repeated, hashmap is usually used, and space is used for time.

3. Since hashmap only needs to indicate whether the character exists, it can be used to store the position of the character in the string (starting from 1). This is a little trick.

4. If the character does not appear, the right pointer moves to the right, and the length increases.

5, if it ever appears

A, if the position appears before the left pointer, record the length from the current position to the left pointer and compare it with the maximum length (that is, the left pointer does not move)

B, if the position that appears after the left pointer is obviously shorter than the current length, there is no need to compare (that is, the left pointer moves to the next place of the last occurrence)

6. Update the position where the characters appear in hashmap to the latest position this time

Write 1:

Func lengthOfLongestSubstring (s string) int {m:=make (map [byte] int) res:=0 left:=0 for ipura

< len(s);i++{ if m[s[i]] >

0 & & left

< m[s[i]]{ left= m[s[i]] } m[s[i]] = i + 1 if i-left+1>

Res {res=i-left+1}} return res}

Write method 2:

Func lengthOfLongestSubstring (s string) int {m:=make (map [byte] int) res:=0 left:=0 for ipura

< len(s);i++{ if m[[]byte(s)[i]] == 0 || m[[]byte(s)[i]] < left{ res = max(res, i - left + 1) } else { left = m[s[i]] } m[s[i]] = i + 1 }return res} func max(a,b int)int{ if a>

B {return a} return b} on how to use leetcode in golang to achieve the longest substring without repeating characters is shared here. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Character length position pointer longest example explanation input output content writing more this is topic help yes maximum no interest place Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft Redmi vpn MySQL