Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report