#20 Valid Parentheses
来源:互联网 发布:有趣的网站 知乎 网盘 编辑:程序博客网 时间:2024/06/11 09:50
题目链接: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) { //返回栈顶元素,空栈时返回'\0' 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;}
0 0
- LeetCode 20: Valid Parentheses
- LeetCode(20) Valid Parentheses
- leetcode 20 Valid Parentheses
- Leetcode【20】:Valid Parentheses
- leetcode 20-Valid Parentheses
- [leetcode 20] Valid Parentheses
- 20、Valid Parentheses
- [Leetcode] 20 - Valid Parentheses
- LeetCode | #20 Valid Parentheses
- leetcode.20-----------Valid Parentheses
- [leetcode] 20 Valid Parentheses
- leetcode-20 Valid Parentheses
- leetCode #20 Valid Parentheses
- leetcode 20 Valid Parentheses
- Leetcode 20 Valid Parentheses
- LeetCode 20 - Valid Parentheses
- #20 Valid Parentheses
- LeetCode 20: Valid Parentheses
- uvalive 3363(区间dp)
- java.util.ArrayList.indexOf(Object) 方法
- 自定义语言的实现——解释器模式
- Linux测试标准------stress压力测试
- android 蓝牙学习资料
- #20 Valid Parentheses
- 网络编程之TCP/UDP及其流程比较
- 极光推送 别名
- DxgkInitialize的原理分析
- Wireshark数据抓包教程之安装Wireshark
- deepID result
- spring websocket服务器遇到问题记录1
- Horizon Is Easy, Horizon Is Complex
- Android ORMLite数据库简介