LeetCode(20)Valid Parentheses
题目
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.
分析
典型的括号匹配问题,数据结构一书中的典型实例。其程序实现,借助栈结构是最佳方法。
AC代码
class Solution {
public:
bool isValid(string s) {
int len = strlen(s.c_str());
if (len == 0)
return true;
if (len % 2 != 0)
return false;
//括号匹配问题,用数据结构栈辅助实现
stack<char> sta;
for (int i = 0; i < len; i++)
{
if (s[i] != ')' && s[i] != ']' && s[i] != '}')
sta.push(s[i]);
else
{
if (sta.size() <= 0)
return false;
if (s[i] == PairLetter(sta.top()))
sta.pop();
else
return false;
}//else
}//for
if (sta.size() == 0)
return true;
else
return false;
}
char PairLetter(const char &c)
{
switch (c)
{
case '(':
return ')'; break;
case '[':
return ']'; break;
case '{':
return '}'; break;
default:
return 0; break;
}
}
};
LeetCode(20)Valid Parentheses的更多相关文章
- LeetCode(49)-Valid Parentheses
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- 【LeetCode算法-20】Valid Parentheses
LeetCode第20题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...
- LeetCode(36)Valid Sudoku
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode(242)Valid Anagram
题目 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...
- leetcode第20题--Valid Parentheses
Problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
- LeetCode(20):有效的括号
Easy! 题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭 ...
- LeetCode(125) Valid Palindrome
题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ign ...
- LeetCode(22)Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode(65) Valid Number
题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
随机推荐
- Oracle中dblink的创建与删除
查询数据库中有哪些dblink连接 select * from dba_objects where object_type='DATABASE LINK'; 删除公有的EMIS_PRODUCTOIN连 ...
- 知乎模拟登录 requests session
Python 3.5 # -*- coding: utf-8 -*- """ Created on Wed May 3 16:26:55 2017 @author: x- ...
- iOS MD5 (Swift3)
import Foundation extension Int { func hexedString() -> String { return NSString(format:"%02 ...
- AdventureWorks2012.mdf的使用
AdventureWorks2012.mdf的使用,在数据库管理器界面中,右击数据库,然后附加,然后选择好AdventureWorks2012.mdf,然后删掉log,然后确定即可.
- C++构造函数详解(复制构造函数 也是 拷贝构造函数)
构造函数是干什么的 该类对象被创建时,编译系统对象分配内存空间,并自动调用该构造函数,由构造函数完成成员的初始化工作,故:构造函数的作用:初始化对象的数据成员. 构造函数的种类 1 class Com ...
- Codeforces Round #544 (Div. 3) A.Middle of the Contest
链接:https://codeforces.com/contest/1133/problem/A 题意: 给两个时间点,求中间时间点. 思路: 数学 代码: #include <bits/std ...
- Win10 Hyper-v 中安装 CentOS 搭建开发环境
Windows 环境 操作系统:Windows 10 开发环境:VS2005(需启动.NET Framework 3.5 ,才能正常安装使用) Linux 环境 发行版:CentOS 7_x64 安 ...
- jmeter(十八)属性和变量
一.Jmeter中的属性: 1.JMeter属性统一定义在jmeter.properties文件中,我们可以在该文件中添加自定义的属性 2.JMeter属性在测试脚本的任何地方都是可见的(全局),通常 ...
- 74LVC2G241双缓冲3态驱动器
- RHEL 6.5----heartbeat
主机名 IP 所需软件 master 192.168.30.130 heartbeat.httpd node-1 192.168.30.131 nfs node-2 192.168.30.1 ...