LeetCode 20. Valid Parentheses(c++)
利用栈的操作,遇到"(","[","{"即进栈,遇到")","]","}"判断是否与栈顶匹配,若不匹配则false。
class Solution {
public:
bool isValid(string s) {
stack<char> c;
for(int i=;i<s.size();i++){
if(!c.empty()){
if(s[i]=='('||s[i]=='{'||s[i]=='[')
c.push(s[i]);
else if(s[i]==')'){
if(c.top()=='(') c.pop();
else return false;
}
else if(s[i]=='}'){
if(c.top()=='{') c.pop();
else return false;
}
else if(s[i]==']'){
if(c.top()=='[') c.pop();
else return false;
}
}
else c.push(s[i]);
}
if(!c.empty()) return false;
return true;
}
};
LeetCode 20. Valid Parentheses(c++)的更多相关文章
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- [LeetCode] 20. Valid Parentheses ☆
转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...
- LeetCode 20 -- Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Java [leetcode 20]Valid Parentheses
题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
随机推荐
- Keil MDK5的ITM调试
https://blog.csdn.net/burgesskzg/article/details/77100453
- JAVA流读取文件并保存数据
如图有文本如下数据 写方法读取数据 private String[][] getData(){ // 使用ArrayList来存储每行读取到的字符串 ArrayList<String> a ...
- 洛谷P1122最大子树和题解
题目 一道比较好想的树形\(DP\) 完全可以用树形DP的基本思路,递归,然后取最优的方法. \(Code\) #include <iostream> #include <cstri ...
- 怎么用MATLAB产生FPGA所需的hamming窗系数
需求 在FPGA处理中如果需要对待处理数据加窗,则需要窗系数存储在ROM中以供使用. 前言 MATLAB窗函数说明 流程 比如加个hamming窗,8192点,16bit放大,最终系数18bit位宽. ...
- 【BZOJ4001】[TJOI2015]概率论(生成函数)
[BZOJ4001][TJOI2015]概率论(生成函数) 题面 BZOJ 洛谷 题解 这题好仙啊.... 设\(g_n\)表示\(n\)个点的二叉树个数,\(f_n\)表示\(n\)个点的二叉树的叶 ...
- [openssh-server]install and enable ssh in debian9 / 在debian9上安装并启用ssh
新安装的debian9.8 with XFCE 发现没有ssh,下载debian-9.8.0-amd64-DVD-1.iso并挂在到ESXi虚拟机/media/cdrom0. 清空或保存/etc/ap ...
- cenos7上部署python3环境以及mysqlconnector2.1.5
本机的python2不要管他,因为可能有程序依赖目前的python2环境,比如yum!!!!! 一.安装python3依赖环境: yum -y install zlib-devel bzip2-dev ...
- Oracle windows64位 百度云下载链接
oracle11g安装包 去官网需要登录 这个是百度云盘链接 链接:https://pan.baidu.com/s/18lYrkqqHG8u4aDdQekHc3g 提取码:fg2v
- window.location.href 传参中文乱码问题!!!
不是所有地方都会用Ajax 当你使用window.location.href 来传中文参数的时候 如何避免乱码问题 js 是这样写的 下面代码中 方式 封装编码 参数 username ...
- Java如何计算一个程序的运行时间
话不多说 直接看代码 package com.mowcode; /** * * @ClassName: Code_01_ProjectTime * @Description: 拿到程序运行时间 * @ ...