zoukankan      html  css  js  c++  java
  • #20 Valid Parentheses

    题目链接:https://leetcode.com/problems/valid-parentheses/


    Given a string containing just the characters '('')','{''}''[' and ']', determine if the input string is valid.

    The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

    typedef struct StackRecord {    //不考虑溢栈实现简单的堆栈
    	char ch[100];
    	int top;
    }*Stack;
    bool isEmpty(Stack stack) {
    	if (stack->top >= 0)
    		return false;
    	return true;
    }
    Stack init() {
    	Stack stack = (Stack)malloc(sizeof(struct StackRecord));
    	stack->top = -1;
    	return stack;
    }
    void push(char ch, Stack stack) {
    	stack->ch[++stack->top] = ch;
    }
    char top(Stack stack) {     //返回栈顶元素,空栈时返回''
        if(!isEmpty(stack))
    	    return stack->ch[stack->top];
    	return 0;
    }
    void pop(Stack stack) {
    	--stack->top;
    }
    bool isValid(char* s) {
    	Stack stack = init();
    	while (*s) {
    		switch (*s) {
    		case '(':       //左括号入栈
    		case '[':
    		case '{':
    			push(*s, stack);
    			break;
    		case ')':      //右括号。查看栈顶是否为相应左括号。

    不是返回false;否则出栈 if (top(stack) != '(') return false; pop(stack); break; case ']': if (top(stack) != '[') return false; pop(stack); break; case '}': if (top(stack) != '{') return false; pop(stack); break; default: return false; } ++s; } if (!isEmpty(stack)) //栈内不空说明有多余左括号 return false; return true; }



  • 相关阅读:
    【网络流24题----15】汽车加油行驶问题
    【网络流24题】最小路径覆盖问题
    网络流二·最大流最小割定理
    贪吃蛇
    【SCOI2008】着色方案
    DARK的锁链
    【NOIP2014】飞扬的小鸟
    [NOIP2012] 借教室
    [NOIP2012] 开车旅行
    [NOIP2012] 国王游戏
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7063076.html
Copyright © 2011-2022 走看看