Given a string s, find the length of the longest substring without repeating characters.
乍看之下,原意将字符串中的字符遍历,记录每个字符的位置,
遇到相同的字符更新位置,然后比较长度。
来看看㊣官方範例↓
看完範例后,如果你的感想是这样的话↓
没关係~看看我的解法吧!
public class Solution { public int LengthOfLongestSubstring(string s) { var Dict = new HashSet<char>(); var str = ""; var max = 0; var cur = 0; for (int i = 0; i < s.Length; i++) { if (Dict.Contains(s[i])) { max = Math.Max(str.Length, max); str = ""; Dict.Clear(); i = (++cur); } Dict.Add(s[i]); str += s[i]; } return Math.Max(max, str.Length); }}
看看执行结果