0509第1题(虽然是08做的,但这会已经09了)

题目

Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.

思路

  • 直接用一个后进先出的stack来匹配就好
class Solution {
public:bool isValid(string s) {stack<char> mystack;if(s.length()==1)return false;for(int i=0; i<s.length(); i++){if(mystack.size()==0 & (s[i]==')' || s[i]=='}' || s[i]==']'))return false;else if(s[i]=='(' || s[i]=='{' || s[i]=='[')mystack.push(s[i]);else if(s[i]=='}' & mystack.top()=='{' & mystack.size()!=0)mystack.pop();else if(s[i]==']' & mystack.top()=='[' & mystack.size()!=0)mystack.pop();else if(s[i]==')' & mystack.top()=='(' & mystack.size()!=0)mystack.pop();else return false;}if(mystack.size()==0)return true;else return false;      }
};

然后在提交过程中,发现自己老是忘了各种特殊样例,还是要多刷题鸭!
加油,你是最胖的!

刚刚去偷瞄了大佬的答案,虽然runtime和memory和我差不多,但素人家的看起来很简洁啊!

class Solution {
public:bool isValid(string s) {stack<char> paren;for (char& c : s) {switch (c) {case '(': case '{': case '[': paren.push(c); break;case ')': if (paren.empty() || paren.top()!='(') return false; else paren.pop(); break;case '}': if (paren.empty() || paren.top()!='{') return false; else paren.pop(); break;case ']': if (paren.empty() || paren.top()!='[') return false; else paren.pop(); break;default: ; // pass}}return paren.empty() ;}
};

LeetCode: 20. Valid Parentheses相关推荐

  1. [LeetCode]--20. Valid Parentheses

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

  2. LeetCode 20 Valid Parentheses (C++)

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

  3. [swift] LeetCode 20. Valid Parentheses

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

  4. LeetCode 20. Valid Parentheses(c++)

    利用栈的操作,遇到"(","[","{"即进栈,遇到")","]","}"判断是 ...

  5. LeetCode Longest Valid Parentheses

    原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...

  6. LeetCode 20. Valid Parentheses--笔试题--Python解法

    题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...

  7. 最长有效括号子串长度 c语言,LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)...

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

  8. Leetcode: Longest Valid Parentheses

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

  9. LeetCode 20. Valid Parenthese

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

最新文章

  1. python三十二:os模块
  2. excel xml mysql_数据库表转换为xml格式,excel转换为xml格式文件
  3. String.subString内存泄露
  4. Ubuntu19.04安装mysql8.0版本(亲测OK)
  5. ora-01591:锁被未分布式事物处理/Distrib tran
  6. leetcode453. 最小操作次数使数组元素相等(贼难的简单题)
  7. 使用MSDN学习ASP.NET的工作流程
  8. 亚马逊AWS EC2服务器配置教程
  9. 嵌入式Linux配置内核后编译过程中报未定义引用错误的解决
  10. 关于Excel不能多开多个窗口的问题
  11. 智能车竞赛·通过虚拟示波器实现电机PID调参
  12. 淘宝虚拟物品自动发货---DiPiPi网店自动发货助手免费版
  13. Nginx_01_Nginx三大基础功能(静态服务器、虚拟主机、负载均衡/服务端代理)
  14. 浙江台州3名乡村教师谈育人:教育源于爱
  15. mac系统python配置
  16. 养老展,2023中国北京国际养老产业博览会
  17. BSCI标准审核纲要 建议收藏
  18. flex布局: justify-content
  19. 微信小程序数据交互(wx.request)
  20. 您可以在PlayStation 4上使用的30种语音命令

热门文章

  1. 干货!top白帽子 Gr36_ 手把手教你挖漏洞|2017 先知白帽大会
  2. tar.xz、tar.bz2 压缩包解压方式
  3. mysqld服务器系统变量和状态变量
  4. 使用 PHP 构建的 Web 应用如何避免 XSS 攻击
  5. Unirest 轻量级的HTTP开发库
  6. WebService简单验证:SoapHeader
  7. 用Python解“两个数的简单计算器”题
  8. 怎么读取h5文件内容_【Python编程特训连载72】读取two.txt文件,模拟输出“两会”内容 答案公布...
  9. 排序算法(五):快速排序
  10. Python之pandas读取Excel表格空值为nan的处理