提供c#代码答案如下:
HashSet
LastIndexOf(char,index) :从索引0开始到指定的数值位置范围内查找最后一个匹配的的字符串的位置
public static int LengthOfLongestSubstring(string s)
{
char[] arr = s.ToCharArray();
HashSet<string> set = new HashSet<string>();
int num = 0;
for (int i = 0; i < arr.Length; i++)
{
bool bol = set.Add((arr[i].ToString())); //crux: if it exists that can be added correctly.else is false
if (!bol)
{
set.Clear();
i = s.LastIndexOf(arr[i], i - 1);//crux2: LastIndexOf(char,index) 从索引0开始到指定的数值位置范围内查找最后一个匹配的的字符串的位置
}
if (num < set.Count())
{
num = set.Count();
}
}
return num;
}