C# unity游戏开发 遍历字符串,倘若不在队列中则将其压入队列,并判断当前个数是不是最大,在队列中则不断弹出头节点直到没有重复
代码
public class Solution {
public int LengthOfLongestSubstring(string s) {
Queue a = new Queue();
int result = 0;
foreach (var item in s)
{
while (a.Contains(item))
{
a.Dequeue();
}
a.Enqueue(item);
if(a.Count>result)
{
result = a.Count;
}
}
return result;
}
}
作者:yokaihon
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/wu-zhong-fu-zi-fu-de-zui-chang-zi-chuan-wrbvc/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
LeetCode_无重复字符的最长子串_C#
最新推荐文章于 2024-08-17 15:37:21 发布