In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how the C# algorithm realizes the longest substring of non-repetitive characters". In the daily operation, I believe that many people have doubts about how the C# algorithm realizes the longest substring of non-repetitive characters. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize the longest substring of non-repetitive characters by C# algorithm". Next, please follow the editor to study!
Title
Given a string, please find out the length of the longest substring that does not contain repeating characters.
Example 1:
Enter: "abcabcbb"
Output: 3
Explanation: because the longest substring of non-repeating characters is "abc", its length is 3.
Example 2:
Enter: "bbbbb"
Output: 1
Explanation: because the longest substring of non-repeating characters is "b", its length is 1.
Example 3:
Enter: "pwwkew"
Output: 3
Explanation: because the longest substring of non-repeating characters is "wke", its length is 3.
Note that the string is empty, the variable is null, the string length Length = 1, and so on.
Test example
Enter "au", "abcabcbb", "bbbbb", "pwwkew" and "aab" the expected results are respectively 1jing2jing3jing1jing3jing2
Code format template
Public class Solution {public int LengthOfLongestSubstring (string s) {}} the author's code is for reference only.
Use the stupidest way, 200ms around
Public class Solution {public int LengthOfLongestSubstring (string s) {if (s = = null | | s = = ") return 0; char [] a = s.ToCharArray (); / / string to character array int start = 0; / / start position int stop = 0 / / interval end position int newMax = 1; / / current interval number int max = 1; / / maximum interval number for (stop = 1; stop)
< a.Length; stop++) //每次向后移动一位 { bool b = false; //是否存在重复 for (int i = start; i < stop; i++) //检查当前元素在区间是否有相同值 { if (a[stop] == a[i]) //如果stop+1位在区间找到相同的字符 { char ls = a[stop]; if (newMax >Max) max = newMax; start = I + 1; / / reset the start position of the interval newMax = stop-start + 1; b = true; break }} if (b = = false) newMax + = 1;} if (newMax > max) max = newMax; return max;}} complete test code (console) using System Namespace ConsoleApp1 {public class Testa {public int LengthOfLongestSubstring (string s) {if (s = = null | | s = = ") return 0; char [] a = s.ToCharArray (); / / string to character array int start = 0; / / start position int stop = 0 / / interval end position int newMax = 1; / / current interval number int max = 1; / / maximum interval number for (stop = 1; stop)
< a.Length; stop++) //每次向后移动一位 { bool b = false; //是否存在重复 for (int i = start; i < stop; i++) //检查当前元素在区间是否有相同值 { if (a[stop] == a[i]) //如果stop+1位在区间找到相同的字符 { char ls = a[stop]; if (newMax >Max) max = newMax; start = I + 1; / / reset the interval start position newMax = stop-start + 1; / / reset the interval number b = true; break }} if (b = = false) / add 1 newMax + = 1 when the interval number is not reset;} if (newMax > max) max = newMax; return max }} class Program {static void Main (string [] args) {Testa T1 = new Testa (); / / correct result Console.WriteLine (t1.LengthOfLongestSubstring ("")); / / 1 Console.WriteLine (t1.LengthOfLongestSubstring ("au")) / / 2 Console.WriteLine (t1.LengthOfLongestSubstring ("abcabcbb")); / 3 Console.WriteLine (t1.LengthOfLongestSubstring ("bbbbb")); / / 1 Console.WriteLine (t1.LengthOfLongestSubstring ("pwwkew")); / / 3 Console.WriteLine (t1.LengthOfLongestSubstring ("aab")); / / 2 Console.ReadKey () } using hash sets is faster, 100ms-150ms public int LengthOfLongestSubstring (string s) {int n = s. Length; HashSet set = new HashSet (); / / set int ans = 0, start = 0, stop = 0; / / ans is the length of the string, the beginning of the starp interval, and the end of the stop interval while (start)
< n && stop < n) { // try to extend the range [i, j] if (!set.Contains(s[stop])) { set.Add(s[stop++]); ans = Math.Max(ans, stop - start); //或者ans = ans >(stop-start) Ans: (stop-start)} else {set.Remove (s [start + +]);}} return ans;} complete console test code using System;using System.Collections.Generic;using System.Linq Namespace ConsoleApp2 {public class Solution {public int LengthOfLongestSubstring (string s) {int n = s. Length; HashSet set = new HashSet (); / / set int ans = 0, start = 0, stop = 0; / / ans is the string length, the beginning of the starp interval, and the end of the stop interval while (start)
< n && stop < n) { // try to extend the range [i, j] if (!set.Contains(s[stop])) { set.Add(s[stop++]); ans = Math.Max(ans, stop - start); //或者ans = ans >(stop-start) Ans: (stop-start)} else {set.Remove (s [start + +]);}} return ans;}} class Program {static void Main (string [] args) {Solution T1 = new Solution () / / correct result Console.WriteLine (t1.LengthOfLongestSubstring (")); / / 1 Console.WriteLine (t1.LengthOfLongestSubstring (" au ")); / / 2 Console.WriteLine (t1.LengthOfLongestSubstring (" abcabcbb ")); / / 3 Console.WriteLine (t1.LengthOfLongestSubstring (" bbbbb ")) / / 1 Console.WriteLine (t1.LengthOfLongestSubstring ("pwwkew")); / / 3 Console.WriteLine (t1.LengthOfLongestSubstring ("aab")); / / 2 Console.ReadKey ();}} at this point, the study of "how the C# algorithm implements the longest substring without repeating characters" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.