A*******e 发帖数: 2419 | 1 下面的算法有什么问题吗?已经是O(n),在C++统计里排到最后,落入C#区间了。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int result = 0;
int start = 0;
int end = 0;
unordered_map hash;
for (int i = 0; i < s.length(); ++i) {
char ch = s[i];
const auto it = hash.find(ch);
if (it != hash.end() && it->second >= start) {
result = max(result, end - start);
start = it->second + 1;
}
hash[ch] = i;
++end;
}
return max(result, end - start);
}
}; |
|