在优快云论坛看到这2道题,有一道在我博客里已经用DP解答过
http://topic.youkuaiyun.com/u/20070930/11/fa87b186-148e-4cc3-a582-013562e64de0.html
另外一道有人说用KMP做
我觉得在LeetCode上这个解答更好
这里是地址:http://www.leetcode.com/2011/05/longest-substring-without-repeating-characters.html
#include <iostream>
#include <string>
int lengthOfLongestSubstring(std::string s)
{
int n = s.length();
int i = 0;
int j = 0;
int maxLen = 0;
bool exist[256] = {false};
while(j < n)
{
if(exist[s[j]])
{
maxLen = std::max(maxLen, j - i);
while(s[i] != s[j])
{
exist[s[i]] = false;
i++;
}
i++;
j++;
}
else
{
exist[s[j]] = true;
j++;
}
}
maxLen = std::max(maxLen, n - i);
return maxLen;
}
本文介绍了一种求解最长无重复字符子串长度的方法,通过滑动窗口结合布尔数组记录字符出现状态,实现了高效查找。同时对比了KMP算法,并提供了清晰的C++实现代码。
2216

被折叠的 条评论
为什么被折叠?



