在此处查看问题https://leetcode.com/problems/longest-substring-without-repeating-characters/ 除了c ++,我已经在其他编程语言中看到了许多解决此问题的方法。
我试图使用链接列表的滑动窗口算法来解决O(n)时间复杂性的问题,但是编译器给出了非常荒谬的内存错误。有人可以解释,我在这里做错了吗?
/*struct ListNode{
public:
char val;
ListNode* next;
ListNode(char x):val(x),next(NULL) {}
};*/
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int maxLength=1;
int n=s.size();
int start=0,end=0;
ListNode* head=new ListNode(s[0]);
while(start<n&&end<n-1)
{ cout<<1;
if(search(s[++end], head)==false)
{ cout<<2;
maxLength++;
add(s[end], head);
}
else if(search(s[++end], head)==true)
{ cout<<3;
head=del(head);
start++;
}
cout<<4;
}
cout<<5;
return maxLength;
}
bool search(char c, ListNode* head)
{ ListNode* l=head;
while(l!=NULL)
{if((l->val)==c)
{ delete l;
return true;
}
l=l->next;
}
delete l;
return false;
}
void add(char c, ListNode* head)
{ ListNode*l =head;
while(l->next!=NULL)
l=l->next;
l->next=new ListNode(c);
delete l;
}
ListNode* del(ListNode* head)
{
ListNode* l=head;
head=head->next;
delete l;
return head;
}
};
我期待测试用例的正确答案,而不是这个错误。
我得到的标准输出是:1241
== 29 ==错误:AddressSanitizer:地址后释放后使用堆
PC上的0x602000000010 0x0000004074e6 bp 0x7ffd60602ce0 sp 0x7ffd60602cd8
在0x602000000010线程T0读取大小4
__libc_start_main中的#1 0x7fd2003452e0(/ lib / x86_64-linux-
gnu / libc.so.6 + 0x202e0)
0x602000000010 is located 0 bytes inside of 16-byte region
[0x602000000010,0x602000000020)
freed by thread T0 here:
#0 0x7fd201d6c0d8 in operator delete(void*, unsigned long)
(/usr/local/lib64/libasan.so.5+0xeb0d8)
#2 0x7fd2003452e0 in __libc_start_main (/lib/x86_64-linux-
gnu/libc.so.6+0x202e0)
previously allocated by thread T0 here:
#0 0x7fd201d6ace0 in operator new(unsigned long)
(/usr/local/lib64/libasan.so.5+0xe9ce0)
#2 0x7fd2003452e0 in __libc_start_main (/lib/x86_64-linux-
gnu/libc.so.6+0x202e0)
Shadow bytes around the buggy address:
0x0c047fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c047fff8000: fa fa[fd]fd fa fa 00 00 fa fa fa fa fa fa fa fa
0x0c047fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==29==ABORTING