【题目】
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.
【分析】
这是栈的应用。
注意一下情况:
(1)[]) 左右括号数目不匹配,左括号少,右括号多,因此出栈时需判断栈是否空if(!brackets.empty())
(2) (( 左右括号数目不匹配,左括号多,右括号少,因此遍历完后
需判断栈是否空
【代码】
/*********************************
* 日期:2015-01-23
* 作者:SJF0115
* 题目: 20.Valid Parentheses
* 网址:https://oj.leetcode.com/problems/valid-parentheses/
* 结果:AC
* 来源:LeetCode
* 博客:
**********************************/
#include <iostream>
#include <stack>
using namespace std;
class Solution {
public:
bool isValid(string s) {
bool result = false;
int len = s.length();
if(len <= 1){
return result;
}//if
stack<char> brackets;
for(int i = 0;i < len;++i){
// '(' '[' '{' 进栈
if(s[i] == '(' || s[i] == '[' || s[i] == '{'){
brackets.push(s[i]);
continue;
}//if
// 否则出栈,比较
if(!brackets.empty()){
char bracket = brackets.top();
brackets.pop();
if(s[i] == ')' && bracket != '('){
return false;
}//if
if(s[i] == ']' && bracket != '['){
return false;
}//if
if(s[i] == '}' && bracket != '{'){
return false;
}//if
}//if
// 数目不匹配
else{
return false;
}
}//for
if(!brackets.empty()){
return false;
}//if
return true;
}
};
int main(){
Solution solution;
string s = "((";
bool result = solution.isValid(s);
// 输出
cout<<result<<endl;
return 0;
}
分享到:
相关推荐
第四章 Leetcode 题解 ...20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 26. Remove Dupli
Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3
leetcode 2 有效括号 给定一个只包含字符'(' , ')' , '{' , '}' , '['和']'的字符串,确定输入字符串是否有效。 输入字符串在以下情况下有效: * 左括号必须由相同类型的括号封闭。 * 左括号必须以正确的顺序关闭。 ...
# [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...
20 days hard 48 about take 10 days Summarize and implement all sort algorithms Tree/Graph traverse: DFS,BFS,LFS 07/01/2015 Set up IntelliJ for Github Finish easy problems in Leetcode using java. 1....
20.Valid Parentheses 26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and ...
Valid Parentheses 运行时间:40 毫秒内存使用:13.8 MB 22. Generate Parentheses 运行时间:164 毫秒内存使用:22.5 MB 26. Remove Duplicates from Sorted Array 运行时间:100 毫秒内存使用:15.2 MB 27. Remove...
Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 18 3 sum 扩展版, 外层多套一个循环即可。注意判断重复及细节优化 19 细节:nodelist前插入一个dummy node. 删除倒数第n 等于正数查到len ...
lru cache leetcode #算法解题报告 主要记录我每天做的题目,包括leetcode, 剑指offer等在线编程平台,以前做过的等时间够再一起分享。 使用swift解题 ...20.Valid Parentheses 21.Merge Two Sorted L
Parentheses] [21. Merge Two Sorted Lists] [53. Maximum Subarray] [70. Climbing Stairs] [101. Symmetric Tree] [104. Maximum Depth of Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II...
丢失的最小正整数leetcode LeetCode Note 7. ...Parentheses: 对于括号匹配问题,自然而然的想到利用栈来实现。也成功实现了括号匹配,再去一观其他人答案,实际可引入map去提前设置好匹配规则,这
java lru leetcode :ice_cream: LeetCode ...Parentheses 26 Remove Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73
20 Valid Parentheses 有效的括号 26 Remove Duplicates from Sorted Array 删除排序数组中的重复项 32 Longest Valid Parentheses 最长有效括号 33 Search in Rotated Sorted Array 搜索旋转排序数组 34 Find First...
leetcode题库 LeetCode 题解合集 本仓库展示了LeetCode题库中部分题目的解法(持续更新),所有代码均采用C++编写并配有输入输出样例 代码清单如下: 序号 题目 程序源码 ...Parentheses.cpp 22 括号生成 G
Parentheses 有效的括号 Stack / 栈 用栈实现配对 Daily Challenge 2020/08/14 28 Implement strStr() 实现 strStr() String / 字符串 循环遍历即可 algorithm-pattern: quick start 43 Multiply S
Leetcode 32 Longest Valid Parentheses (最长有效括号) 给定一个只包含 ‘(‘ 和 ‘)’ 的字符串,找出最长的包含有效括号的子串的长度。 示例 1: 输入: “(()” 输出: 2 解释: 最长有效括号子串为 “()” 示例 2...
leetcode怎么改密码 Code leetcode easy 测试一下本地的... emmmmm之前修改了一下,现在用ssh提交 应该不用输入密码了吧 ~~emmmmm 先在这里立个flag!!...Valid Parentheses :cross_mark:暴力解法(未通过)
LeetCode字符串换行 Solve_Leetcode 复习的过程,来刷刷题,坚持坚持坚持! 逆波兰表达式 题目如下: 使用了 class Stack A more complete and consistent ...Valid Parentheses 题目如下: Stack泛型
Parentheses 用栈判断括号匹配 Regular Expression Matching 递归匹配 wildcard matching 动态规划 longest common prefix , 简单 valid number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to ...
java lru leetcode Learn-algorithms :laptop: Java实现的各种算法题解,主要有: ...Parentheses.java) [动态规划-Maximum Length of Repeated Subarray](./leetcode/动态规划-Maximum Length of Repeated Subar