20. Valid Parentheses (Stack)
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.
struct Stack{
struct StackNode* top;
int size;
}; struct StackNode{
char value;
struct StackNode* next;
}; bool isEmpty(struct Stack* s){
if(s->size==) return true;
else return false;
} void push(struct Stack* s, char value){
struct StackNode* node = malloc(sizeof(struct StackNode));
node->value = value;
node->next = s->top;
s->top = node;
s->size++;
} char pop(struct Stack* s){
struct StackNode* node = s->top;
char value = node->value;
s->top = node->next;
free(node);
s->size--;
return value;
} bool isValid(char* s) {
struct Stack* charStack = malloc(sizeof(struct Stack));
charStack->top = NULL;
charStack->size = ; while(*s!='\0'){
if(*s == ')'){
if(isEmpty(charStack) || pop(charStack)!='(') return false;
}
else if(*s == ']'){
if(isEmpty(charStack) || pop(charStack)!='[') return false;
}
else if(*s == '}'){
if(isEmpty(charStack) || pop(charStack)!='{') return false;
}
else{
push(charStack,*s);
}
s++;
} if(isEmpty(charStack)) {
free(charStack);
return true;
}
else{
free(charStack);
return false;
}
}
20. Valid Parentheses (Stack)的更多相关文章
- Leetcode 20 Valid Parentheses stack的应用
判断括号是否合法 1.用stack存入左括号“([{”,遇到相应的右括号“)]}”就退栈 2.判断stack是否为空,可以判断是否合法 class Solution { public: bool is ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- 20. Valid Parentheses【leetcode】
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- leetCode练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- 刷题20. Valid Parentheses
一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
随机推荐
- leetcode218
from heapq import * class Solution: def getSkyline(self, LRH): skyline = [] i, n = 0, len(LRH) liveH ...
- leetcode1002
class Solution: def commonChars(self, A: 'List[str]') -> 'List[str]': n = len(A) if n == 1: retur ...
- addEventListener 的事件冒泡
语法 target.addEventListener(type, listener, useCapture); target 文档节点.document.window 或 XMLHttpRequest ...
- tp5中ajax方式提交表单
用ajax提交表单,迅速,快捷,实现页面无刷新提交表单. <!DOCTYPE html> <html lang="en"> <head> < ...
- inno setup 安装前判断进程是否存在,以及停止相应进程<转>
打包的时候遇到了这样的需求:假似用户都是傻瓜 式操作,如果更新安装程序的时候,之前的老程序还在运行这个时候如果你去提示让用户吧老程序手动退掉也不现实. 所以当遇到这种 ...
- python文件处理指针的移动
控制文件指针移动 with open('a.txt',mode='rt',encoding='utf-8')as f: res=f.read(4) print(res) 强调:只有t模式下read(n ...
- csredis base usage
Basic usage Whenever possible, server responses are mapped to the appropriate CLR type. using (var r ...
- Extjs4 自定义颜色选择器ColorField
使用元素的颜色选择器 Ext.create("Ext.picker.Color",{ renderTo:Ext.getBody(), listeners:{ select:func ...
- 处理TypeError: Converting circular structure to JSON
// Demo: Circular reference var o = {}; o.o = o; // Note: cache should not be re-used by repeated ca ...
- python小实例一:简单爬虫
本文所谓的爬虫就是通过本地远程访问url,然后将url的读成源代码形式,然后对源代码进行解析,获取自己需要的数据,相当于简单数据挖掘.本文实现的是将一个网页的图片爬出保存到本地的过程,例子很简单,用的 ...