题目链接

Valid Parentheses - LeetCode

注意点

  • 考虑输入为空的情况

解法

解法一:如果是'('、'{'、'['这三者就入栈,否则就判断栈是否为空和栈顶括号是否与之匹配。注意两个判断顺序不可以颠倒,不然会runtime error。时间复杂度为O(n)

class Solution {
public:
bool isValid(string s) {
stack<char> stk;
for(auto &ch:s)
{
if(ch == '('||ch == '{'||ch == '[')
{
stk.push(ch);
}
else if(ch == ')')
{
if(stk.empty()||stk.top() != '(')
{
return false;
}
else
{
stk.pop();
}
}
else if(ch == '}')
{
if(stk.empty()||stk.top() != '{')
{
return false;
}
else
{
stk.pop();
}
}
else if(ch == ']')
{
if(stk.empty()||stk.top() != '[')
{
return false;
}
else
{
stk.pop();
}
}
}
if(!stk.empty())
{
return false;
}
return true;
}
};

小结

一些栈的基本操作:

  • push(): 向栈内压入一个成员;
  • pop(): 从栈顶弹出一个成员;(注意要判断栈是否为空)
  • empty(): 如果栈为空返回true,否则返回false;
  • top(): 返回栈顶,但不删除成员;(注意要判断栈是否为空)
  • size(): 返回栈内元素的大小

Valid Parentheses - LeetCode的更多相关文章

  1. Valid Parentheses [LeetCode 20]

    1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...

  2. Longest Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  3. Longest Valid Parentheses - LeetCode

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  5. [LeetCode] 20. Valid Parentheses 合法括号

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

  6. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  7. [LeetCode] Valid Parentheses 验证括号

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

  8. Java for LeetCode 032 Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. SURF算法的一篇翻译与论证

    原文地址:http://www.sohu.com/a/157742015_715754 SURF: Speeded Up Robust Features 摘要 本文提出了一种新型的具有尺度和旋转不变特 ...

  2. Harbor 学习分享系列3 - Harbor用户指南

    云盘链接 链接:https://pan.baidu.com/s/1wvgI3KGGIckqzlkB-mYz4g 密码:doe7 通过本文无法把本文中的实验进行成功,请联系作者本人,作者会录制视频发送给 ...

  3. php从入门到放弃系列-04.php页面间值传递和保持

    php从入门到放弃系列-04.php页面间值传递和保持 一.目录结构 二.两次页面间传递值 在两次页面之间传递少量数据,可以使用get提交,也可以使用post提交,二者的区别恕不赘述. 1.get提交 ...

  4. ofo容器pass架构分享

    一.我们先要了解一下,为什么企业需要一个paas平台?或者可以说paas到底能做什么? 1.1 我们先来了解一下paas到底是什么? PaaS是Platform-as-a-Service的缩写,意思是 ...

  5. tf导出pb文件,以及如何使用pb文件

    先罗列出来代码,有时间再解释 from tensorflow.python.framework import graph_util import tensorflow as tf def export ...

  6. Burp Suite pro 抓包工具配置

    下载地址: 链接:https://pan.baidu.com/s/1WyuAlJSWZ3HyyEQlpiH3cA 提取码:6l38 破解相关请查看解压文件链接 1.firefox代理设置: burp ...

  7. python 原生态调用server服务————SimpleHTTPServer

    python 原生态调用server服务,接收http传递的参数并且处理返回结果 很多blog中都是抄了官网的,没有说明参数如何接收 1.代码中提供了post与get两种方式来发起请求,但是传递参数时 ...

  8. 11.16 Daily Scrum

    由于今天是工作小周期的最后一天,今天的主要任务是解决了一周留下的技术方面的难题.一些类似于悬浮窗和进度条的bug修复全部在今天得到了解决,修复了数据库的内存泄露bug,软件的搜索功能的完善也接近尾声. ...

  9. 面向对象OO第5-7次作业总结

    面向对象OO第5-7次作业总结 学习OO七周了,深切的感受到了这门课程的不友好.前三次作业能够算是勉强地通过了,但是从第五次作业开始就完全GG了.这三次作业,从多线程电梯开始,然后文件监控,然后到出租 ...

  10. CS小分队第二阶段冲刺站立会议(6月2日)

    昨日成果:攻克了按钮移动的问题: 遇到问题:一开始按钮移动时候,非常慢,因为是根绝相对位移差来移动,延时很严重,后来改用用鼠标的位置作为按钮的移动位置,效果明显. 按钮的mousedown事件和mou ...