mardi 20 octobre 2020

What exact is segmentation fault when using Stack and how to fix it?

Im writing a program to check the score of parenthesis, Leetcode Question 856. However, with the algorithm I used, I'm encountering a "Segmentation Fault (core dumped)" error. I'm unsure as to how there is a segmentation fault when using stack and how could I fix it?

string s;
   cin >> s;
   int score = 0;
   stack<int> st;
   for (int i = 0; i < s.size(); i++){
     char a = s[i];
     if (a == '('){
       st.push(score);
       score = 0;
     }
     else{
       score = st.top() + max(score*2, 1);
       st.pop();
     }
   }
   cout << score;
}

Aucun commentaire:

Enregistrer un commentaire