[LeetCode-20] 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.


这一题是典型的使用压栈的方式解决的问题,题目中还有一种valid情况没有说明,需要我们自己考虑的,就是"({[]})"这种层层嵌套但可以完全匹配的,也是valid的一种。

解题思路是这样的:

1、如果S字符串长度为奇数,直接返回valid;

2、我们对字符串S中的每一个字符C,如果C不是右括号,就压入栈stack中。

3、如果C是右括号,弹栈,如果是匹配的,继续取S中的下一个字符;如果不匹配,说明不是valid的,直接返回。

4、当我们遍历了一次字符串S后,注意这里还有一种情况,就是stack中还有残留的字符没有得到匹配,即此时stack不是空的,这时说明S不是valid的,因为只要valid,一定全都可以得到匹配使左括号弹出。


代码如下:

struct  STACK_NODE
{
    char* pData;/*数组,长度为StackLenMax*/
    int top;//栈顶指针的位置
    int StackLenMax;
};

struct STACK_NODE* alloc_stack(int StackSize)
{
     
    if(StackSize <= 0)
        return NULL;
	
	struct STACK_NODE* pStackNode = NULL;
	
    pStackNode = (struct STACK_NODE*)malloc(sizeof(struct STACK_NODE));
    if(NULL == pStackNode) {
		return NULL;
    }
	
    memset(pStackNode, 0, sizeof(struct STACK_NODE));

    pStackNode->pData = (char *)malloc(sizeof(char)*StackSize+1);
    if(NULL == pStackNode->pData){
		goto malloc_failed;
    }
	
    memset(pStackNode->pData, 0, sizeof(char) * StackSize);
    pStackNode->top= -1; /*初始化从0开始*/
	pStackNode->StackLenMax = StackSize;
	
    return pStackNode;

malloc_failed:
	free(pStackNode);
	return NULL;
}

int free_stack(struct STACK_NODE* pStackNode)
{
    if(NULL == pStackNode)
        return -1;

    if(NULL == pStackNode->pData) {
		free(pStackNode);
		return -1;
	}
	
    free(pStackNode->pData);
    free(pStackNode);
	
    return 0;
}

int stack_push(struct STACK_NODE* pStackNode, char value)
{
	/*1.异常处理*/
    if(NULL == pStackNode)
        return -1;
	if(NULL == pStackNode->pData) {
		return -1;
	}
	
	/*2.栈满,不能压入元素*/
    if(pStackNode->top == pStackNode->StackLenMax-1)
        return -1;
	
    pStackNode->pData[++pStackNode->top] = value;

    return 0;
}

int stack_pop(struct STACK_NODE* pStackNode, char* value)
{
    if(NULL == pStackNode || NULL == value)
        return -1;

    if(-1 == pStackNode->top)
        return -1;

    *value = pStackNode->pData[pStackNode->top--];
	
    return 0;
}

int count_stack_number(struct STACK_NODE* pStackNode)
{
    return (pStackNode->top+1);
}

void print_stack_node(struct STACK_NODE *pStackNode) 
{
	/*1.输入的参数有误*/
    if(NULL == pStackNode) {
		printf("[%d] pStackNode is illegal! \n",__LINE__);
		return;
    }
	/*2.输入的链式堆栈为空*/
	if(-1 == pStackNode->top) {
		printf("[%d] pStackNode is empty!\n",__LINE__);
		return ;
	}
	
	struct STACK_NODE *pStackNodeTemp = pStackNode;
	int count = 0;
	
	while(count <= pStackNode->top) {
		printf("%c ",pStackNodeTemp->pData[count]);
		count++;;
	}
	printf("\n");
}

bool Stackpop_isValid(struct STACK_NODE *pStackNode,char temp)
{
	char stemp;

	switch(temp) {
		case ')':
			stack_pop(pStackNode,&stemp);
			if(stemp == '(') {
				return 1;
			} else {
				return 0;
			}
			
		case '}':
			stack_pop(pStackNode,&stemp);
			if(stemp == '{') {
				return 1;
			} else {
				return 0;
			}
			
		case ']':
			stack_pop(pStackNode,&stemp);
			if(stemp == '[') {
				return 1;
			} else {		
				return 0;
			}
			
		default:
			break;
	}
	
	return 0;
}

bool isValid(char* s) 
{
    if(!s)
		return 0;
	int slen = strlen(s);
	/*奇数个元素,肯定是 inValid*/
	if((slen%2)!=0) {
		return 0;	
	}
	struct STACK_NODE *pStackNode = NULL;
	pStackNode = alloc_stack(slen);
	int i = 0;

	for(i = 0;i<slen;i++) {
		if(s[i] == '('||s[i] == '{'||s[i] == '[') {
			stack_push(pStackNode,s[i]);
		} else if(s[i] == ')'||s[i] == '}'||s[i] == ']') {
			if(!Stackpop_isValid(pStackNode,s[i]))
				return 0;
			
		} else {
			return 0;
		}
	}

	return 1;
}

全部代码如下:

// LeetCode20-Valid Parentheses.cpp : 定义控制台应用程序的入口点。
//

//Valid Parentheses
//Written by ZP1015
//2015.10.22

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct  STACK_NODE
{
    char* pData;/*数组,长度为StackLenMax*/
    int top;//栈顶指针的位置
    int StackLenMax;
};

struct STACK_NODE* alloc_stack(int StackSize)
{
     
    if(StackSize <= 0)
        return NULL;
	
	struct STACK_NODE* pStackNode = NULL;
	
    pStackNode = (struct STACK_NODE*)malloc(sizeof(struct STACK_NODE));
    if(NULL == pStackNode) {
		return NULL;
    }
	
    memset(pStackNode, 0, sizeof(struct STACK_NODE));

    pStackNode->pData = (char *)malloc(sizeof(char)*StackSize+1);
    if(NULL == pStackNode->pData){
		goto malloc_failed;
    }
	
    memset(pStackNode->pData, 0, sizeof(char) * StackSize);
    pStackNode->top= -1; /*初始化从0开始*/
	pStackNode->StackLenMax = StackSize;
	
    return pStackNode;

malloc_failed:
	free(pStackNode);
	return NULL;
}

int free_stack(struct STACK_NODE* pStackNode)
{
    if(NULL == pStackNode)
        return -1;

    if(NULL == pStackNode->pData) {
		free(pStackNode);
		return -1;
	}
	
    free(pStackNode->pData);
    free(pStackNode);
	
    return 0;
}

int stack_push(struct STACK_NODE* pStackNode, char value)
{
	/*1.异常处理*/
    if(NULL == pStackNode)
        return -1;
	if(NULL == pStackNode->pData) {
		return -1;
	}
	
	/*2.栈满,不能压入元素*/
    if(pStackNode->top == pStackNode->StackLenMax-1)
        return -1;
	
    pStackNode->pData[++pStackNode->top] = value;

    return 0;
}

int stack_pop(struct STACK_NODE* pStackNode, char* value)
{
    if(NULL == pStackNode || NULL == value)
        return -1;

    if(-1 == pStackNode->top)
        return -1;

    *value = pStackNode->pData[pStackNode->top--];
	
    return 0;
}

int count_stack_number(struct STACK_NODE* pStackNode)
{
    return (pStackNode->top+1);
}

void print_stack_node(struct STACK_NODE *pStackNode) 
{
	/*1.输入的参数有误*/
    if(NULL == pStackNode) {
		printf("[%d] pStackNode is illegal! \n",__LINE__);
		return;
    }
	/*2.输入的链式堆栈为空*/
	if(-1 == pStackNode->top) {
		printf("[%d] pStackNode is empty!\n",__LINE__);
		return ;
	}
	
	struct STACK_NODE *pStackNodeTemp = pStackNode;
	int count = 0;
	
	while(count <= pStackNode->top) {
		printf("%c ",pStackNodeTemp->pData[count]);
		count++;;
	}
	printf("\n");
}

bool Stackpop_isValid(struct STACK_NODE *pStackNode,char temp)
{
	char stemp;

	switch(temp) {
		case ')':
			stack_pop(pStackNode,&stemp);
			if(stemp == '(') {
				return 1;
			} else {
				return 0;
			}
			
		case '}':
			stack_pop(pStackNode,&stemp);
			if(stemp == '{') {
				return 1;
			} else {
				return 0;
			}
			
		case ']':
			stack_pop(pStackNode,&stemp);
			if(stemp == '[') {
				return 1;
			} else {		
				return 0;
			}
			
		default:
			break;
	}
	
	return 0;
}

bool isValid(char* s) 
{
    if(!s)
		return 0;
	int slen = strlen(s);
	/*奇数个元素,肯定是 inValid*/
	if((slen%2)!=0) {
		return 0;	
	}
	struct STACK_NODE *pStackNode = NULL;
	pStackNode = alloc_stack(slen);
	int i = 0;

	for(i = 0;i<slen;i++) {
		if(s[i] == '('||s[i] == '{'||s[i] == '[') {
			stack_push(pStackNode,s[i]);
		} else if(s[i] == ')'||s[i] == '}'||s[i] == ']') {
			if(!Stackpop_isValid(pStackNode,s[i]))
				return 0;
			
		} else {
			return 0;
		}
	}

	return 1;
}
int main()
{
	char s[] = "[()[()]{}]";
	
	printf("%d\n",isValid(s));
	
	getchar();
	getchar();
	
	return 0;
}







你可能感兴趣的:(LeetCode)