Task with braces, brackets and parenthesis

前端 未结 5 1273
予麋鹿
予麋鹿 2021-01-29 14:38

Task is to check if given string contains balanced sets of {}, [] and ().

For example, check(\"{[}]\") must return

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-29 14:44

    Please tell me what is wrong with my code it's always giving NO

    string isBalanced(string s) {
    stack comp;
    char temp;
    int i;
    for(i=s.size()-1;(s[i]=='}' ) || (s[i]==')' ) || (s[i]==']');i--)
    {
        comp.push(s[i]);
    }
    
    if(comp.size()!=i+1)
    return ("NO");
    
    while(!comp.empty() && i>=0)
    {
        temp=comp.top();
        if(temp!=s[i])
        return ("NO");
        comp.pop();
        i--;
    }
    
    return("YES");
    }
    

提交回复
热议问题