/// <summary> /// 获取一个字符串的没有重复字符的最大子串长度,比如abcabcbb所求值为abc、bca、cab即为3 /// </summary> /// <param name="args"></param> static void Main(string[] args) { string str = "abcabcbb"; int initlength = str.Length; //长度位移法获取子串 Dictionary<int, List<string>> strlist = new Dictionary<int, List<string>>(); for (var i = 0; i < initlength-1; i++) { List<string> valuelist = new List<string>(); var valuelength = initlength - 1 - i; for (var j = 0; j <= initlength - valuelength; j++) { var value = str.Substring(j, valuelength); //(位移,长度) valuelist.Add(value); } strlist.Add(valuelength, valuelist); } //单元素去重校验 foreach (var item in strlist.OrderByDescending(p=>p.Key)) { foreach (var itemchild in item.Value) { bool isRepeat = false; for (var i = 0; i < itemchild.ToCharArray().Length; i++) { for (var j = 0; j < itemchild.ToCharArray().Length; j++) { if (i!=j && itemchild.ToCharArray()[i] == itemchild.ToCharArray()[j]) { isRepeat = true; break; } } if (isRepeat) { break; } } if (!isRepeat) { Console.WriteLine($"结果为:{item.Key}"); Console.ReadLine(); } } } }