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 ']', ...
随机推荐
- 关于存session,cookie还是数据库或者memcache的优劣,部分网上抄录
从效率考虑:cookie > memcache > 数据库cookie对服务器端负载没影响,如果加密.解密会多消耗一点点cpu.带宽倒是会消耗得多一点,同域名下的所有http reques ...
- Appium -选择、操作元素2
选择元素的方法 根据xpath 在Appium中,我们没法使用css,因为css是web专用的 Appium支持xpath来定位元素 对于一些比较复杂的元素的定位,我们可以用它 driver.find ...
- HTML学习-1网页基础知识
HTML超文本标记语言:HyperText Markup Language. 由浏览器运行解析. 它包括了静态页面.html .htm.动态页面.php .aspx .jsp,从数据库提取. 今天 ...
- 机器学习进阶-图像梯度运算-Sobel算子 1. cv2.Sobel(使用Sobel算子进行计算) 2. cv2.convertScalerAbs(将像素点进行绝对值的计算)
1.cv2.Sobel(src, ddepth, dx, dy, ksize) 进行sobel算子计算 参数说明:src表示当前图片,ddepth表示图片深度,这里使用cv2.CV_64F使得结果可 ...
- jqGrid基本使用
jqGrid: 参照网址:官网地址http://www.trirand.com/ http://blog.mn886.net/jqGrid/(快速获取demo) http://www.guriddo. ...
- iOS多语言
https://blog.csdn.net/huangmindong/article/details/53464334 App多语言,字符串统一放在 Localizable.strings 文件里. ...
- Spark 调优(转)
Spark 调优 返回原文英文原文:Tuning Spark Because of the in-memory nature of most Spark computations, Spark pro ...
- Apache Mina UDP连接目标服务器地址时出现异常
俩种情形,第一种是开始连接时候就没连上服务器:第二种是服务器关闭连接,出现的异常: 第一种: java.lang.reflect.InvocationTargetException at sun.re ...
- thread == 售票
import org.apache.xerces.util.SymbolTable; public class ThreadDemo1 { public static void main(String ...
- Java解决输出数组问题
package test; public class doubleshuzu { public static void main(String[] args) { double a[][]; Stri ...