Basic Recursion, Check Balanced Parenthesis

前端 未结 12 1258
孤独总比滥情好
孤独总比滥情好 2020-11-29 17:56

I\'ve written software in the past that uses a stack to check for balanced equations, but now I\'m asked to write a similar algorithm recursively to check for properly neste

12条回答
  •  佛祖请我去吃肉
    2020-11-29 18:31

    It should be a simple use of stack ..

    private string tokens = "{([<})]>";        
        Stack stack = new Stack();   
    
        public bool  IsExpressionVaild(string exp)
        {
            int mid = (tokens.Length / 2)  ;  
    
            for (int i = 0; i < exp.Length; i++)
            {
                int index = tokens.IndexOf(exp[i]);
                if (-1 == index) { continue; }
    
                if(index

提交回复
热议问题