我在leetcode上有问题。此问题的说明如下:
给出一个字符串 s ,找到包含最多2个不同字符的最长子字符串 t 的长度。
示例1:
Input: "eceba"
Output: 3
Explanation: t is "ece" which its length is 3.
示例2:
Input: "ccaabbb"
Output: 5
Explanation: t is "aabbb" which its length is 5.
在尝试使用at
方法之前,我试图进行一些比较。
class Solution {
public:
int lengthOfLongestSubstringTwoDistinct(string s)
{
if (!s.length() || s.length() == 1 || s.length() == 2)
return s.length();
unsigned int left = 0, right = 0; // Use sliding window
map<char, int> mp;
int count;
int len = s.length();
int res = 1; // default answer
while (right < len) {
if (right < len && (mp.find(s.at(right)) == mp.end() || mp[s.at(right)] == 0)) {
count++;
}
mp[s.at(right)]++;
right++;
if (count > 2) // if count is out of scope, just undo last action
{
count--;
right--;
if (right < len)
mp[s.at(right)]--;
if (right - left > res) {
res = right - left;
}
while (count == 2) {
mp[s.at(left)]--;
if (mp[s.at(left)] == 0)
count--;
left++;
}
}
}
return right - left > res ? right - left : res;
}
};
因为所有输入都必须有效。
我的问题是,对于输入aac
,我单击了 Run Code (运行代码)按钮,工作正常。
但是,当我单击提交时,在这种情况下将失败。实际上,我几次遇到这个问题。我只想知道这是否是编译器问题?
答案 0 :(得分:1)
您未能初始化变量DESC
,因此您的程序具有未定义的行为。