讨论/题目交流/394,求大佬帮忙解决/
394,求大佬帮忙解决

感觉思路没错,就是数组越界还是内存问题,求大佬帮忙解决一下
执行错误:
AddressSanitizer:DEADLYSIGNAL

==29==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x00000040ce7f bp 0x7ffe4e400ec0 sp 0x7ffe4e4005a0 T0)
==29==The signal is caused by a READ memory access.
==29==Hint: address points to the zero page.
#2 0x7fa2548b42e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)

AddressSanitizer can not provide additional info.
==29==ABORTING

代码如下:

class Solution {
public:
    string decodeString(string s) {
        stack<char> st;
        string str="";
        int n=s.size();
        if(n==0) return str;
        for(int i=0;i<n;i++)
        {
            if(isdigit(s[i])||s[i]=='[') st.push(s[i]);
            if(st.empty()&&isalpha(s[i])) str+=s[i];
            if((!st.empty())&&isalpha(s[i])) st.push(s[i]);
            if(s[i]==']') 
            {
                stack<char> t;
                string ss="";
                while((!st.empty())&&isalpha(st.top())) {t.push(st.top());st.pop();}
                while(!t.empty()) {ss+=t.top();t.pop();}            
                int count=0; 
                int j=1;
                st.pop();                
                while(isdigit(st.top()))
                {
                    count+=(int(st.top()-'0'))*j;
                    j=10*j;
                    st.pop();
                }                
                for(int k=0;k<count;k++) 
                {           
                    if(st.empty()) str+=ss;
                    else
                        for(int m=0;m<ss.size();m++)
                        {
                            st.push(ss[m]);
                        }                        
                }                 
            }
        }        
     return str;    
    }
};
展开讨论
Wangxc发起于 2019-08-20
最近编辑于 2019-08-21

我也遇到过类似问题,在visual studio上调试都没有问题,提交leetcode就报错了

展开全部 2 讨论