题目描述
给一个字符串,求一个长度最大的子串,在这个子串中没有重复的字母。
题目分析
暴力搜寻,依次遍历每一个字符,找出当以该字符为首时,最大没有重复字母的长度。
代码
#include <map>
class Solution {
public:
int lengthOfLongestSubstring(std::string s) {
std::map<char, int> flag;
unsigned result = 0;
unsigned j = 0;
unsigned i = 0;
while (j < s.length()) {
for ( ; i < s.length(); ++i) {
if (flag[s[i]] == 0) {
flag[s[i]]++;
} else {
break;
}
}
if (result < i - j) {
result = i - j;
}
flag[s[j]]--;
j++;
}
return result;
}
};
总结
想复杂了
近期评论