我在leetcode的以下代码中使用`st.pop()时收到此错误:
AddressSanitizer:DEADLYSIGNAL
=================================================================
==31==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x0000003a1925 bp 0x7ffd7b62dce0 sp 0x7ffd7b62dcd0 T0)
==31==The signal is caused by a READ memory access.
==31==Hint: this fault was caused by a dereference of a high value address (see register values below). Dissassemble the provided pc to learn which register was used.
#8 0x7f3d81a1e0b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
AddressSanitizer can not provide additional info.
==31==ABORTING
代码如下:class Solution {
public:
string removeOuterParentheses(string S) {
stack<char> st;
int count=0;
string ns;
for(int i=0;i<S.size();i++)
{
if(S[i] == 40 && count++ > 0)
{
st.push(S[i]);
ns+=S[i];
}
if(S[i] == 41 && count-- > 0)
{
st.pop();
ns+=S[i];
}
}
return ns;
}
};
最佳答案
如果您有输入字符串"(some text)"
,则条件S[i] == 40 && count++ > 0
将始终评估为false。当S[i]=='('
为true时,由于比较后count++ > 0
递增,因此count
为false。但是,count
递增,这样第二个条件S[i] == ')' && count-- > 0
最终将为true。然后您点击st.pop()
。由于st
为空,将导致问题。
另外,如果您修改了代码,例如通过编写S[i] == 40 && ++count > 0
,不会删除括号,因为名称暗示了。
关于c++ - 地址 sanitizer :DEADLYSIGNAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64969581/