• 匹配括号


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ValidParentheses
    {
    
       // 如果不存在星号,我们甚至连stack都可以不用,直接用一个变量,遇到左括号,自增1,遇到右括号,如果变量为0,直接返回false,否则自减1
       //最后只要看变量是否为0即可。
        class Program
        {
            public class Node
            {
                public string val;
                public Node Next;
                public Node(string s)
                {
                    val = s;
                }
            }
            public class Stack
            {
                public Node Top;
                public void PushIn(Node nd)
                {
                    if (Top != null)
                    {
                        Node temp = Top;
                        Top = nd;
                        Top.Next = temp;
                    }
                    else
                    {
                        Top = nd;
                        Top.Next = null;
                    }
                }
                public Node PopOut()
                {
                    Node nd = null;
                    if (Top != null)
                    {
                        nd.val = Top.val;
                        Top = Top.Next;
                    }
                    return nd;
                }
            }
    
            public static void Match(string s, ref Stack A, ref Stack B)
            {
                //取元素,如果是左括号压入栈A中。如果是右括号,将元素放入栈B中,直到取到与之匹配的左括号,如果A空之后,右括号还没有找到,将AB互换。但是找到了,将B的元素压入A中。
    
                if (s == "(" || s == "[" || s == "{")
                {
                    A.PushIn(new Node(s));
    
                }
                else if (s == ")" || s == "]" || s == "}")
                {
    
                    while (A.Top != null)
                    {
                        Node temp = A.PopOut();
                        if (temp.val == "(" && s == ")" || temp.val == "[" && s == "]" || temp.val == "{" && s == "}")
                        {
                            s = string.Empty;
                            break;
                        }
                        else
                        {
                            B.PushIn(temp);
                        }
                    }
                    if (s != string.Empty)
                    {
                        B.PushIn(new Node(s));
                    }
                    if (A.Top == null)
                    {
                        Stack st = A;
                        A = B;
                        B = st;
                    }
                    else
                    {
                        while (B.Top != null)
                        {
                            A.PushIn(B.PopOut());
                        }
                    }
    
                }
            }
            static void Main(string[] args)
            {
                string[] slist = { "a","(","[","+","}",")","]"};
                Stack A = new Stack();
                Stack B = new Stack();
                for (int i = 0; i < slist.Length; i++)
                {
                   Match(slist[i],ref A,ref B);              
                }
                while (A.Top != null)
                {
                    Console.Write(A.Top.val+" ");
                    A.Top = A.Top.Next;
                }
                Console.ReadKey();
            }
        }
    }
  • 相关阅读:
    mysqld参数配置
    mysql配置详解
    前台获取json未定义问题之两种常用解决办法
    Java相关框架资料及其基础资料、进阶资料、测试资料之分享
    Jmeter之tomcat性能测试+性能改进措施
    Jmeter之mysql性能测试
    MyBatis之反射技术+JDK动态代理+cglib代理
    MyBatis之动态sql
    MyBatis+Hibernate+JDBC对比分析
    前台返回json数据的常用方式+常用的AJAX请求后台数据方式
  • 原文地址:https://www.cnblogs.com/hehe625/p/7773312.html
Copyright © 2020-2023  润新知