【发布时间】:2021-05-04 09:38:33
【问题描述】:
问题链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/
class Solution {
public:
int lengthOfLongestSubstring(string s)
{
if(s.length()==0)
return 0;
if(s.length()==1)
return 1;
int ans=0;
int len=0;
//s.erase(remove(s.begin(), s.end(), ' '), s.end());
vector<int> count(257,0);
vector<int> last_index(257,0);
for(int i=0;i<s.length();i++)
{
if(count[s[i]-'0']==0)
{
count[s[i]-'0']++;
last_index[s[i]-'0']=i; //storing the index of the character
len++;
ans=max(ans,len);
}
else if(count[s[i]-'0']>=1)
{
fill(count.begin(), count.begin()+last_index[s[i]-'0'], 0); // reducing the count to 0 till the last position of the matched character
len=i-last_index[s[i]-'0']; //calculating the new length of the string
count[s[i]-'0']++;
last_index[s[i]-'0']=i; //storing the latest index of the character
}
}
return ans;
}
};
当字符串包含空格时,上述代码出现以下错误。
测试用例: "ab cabcbb"
"bbbbb"
"pwwkew"
""
" "
第 1034 行:字符 34:运行时错误:将无符号偏移量添加到 0x619000000080 溢出到 0x619000000040 (stl_vector.h) 摘要:UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu /9/../../../../include/c++/9/bits/stl_vector.h:1043:34
【问题讨论】:
-
请提供minimal reproducible example,这是一个可以编译的单个文件。作为这里的新用户,也可以使用tour 并阅读How to Ask。
-
测试用例:“ab cabcbb”“bbbbb”“pwwkew”“”“”
-
如果您想扩展您的问题,只需edit。此外,当您使用它时,请使格式保持一致。
-
我在问题正文中添加了...抱歉将其放在评论部分