如何判断一个字符串中的括号是否匹配
2015-09-23 00:30
302 查看
描述:给定一个包含"(",")","[","]"的字符串,如果判断这个字符串中的括号是否正确匹配,匹配输出YES,否则输出NO;
INPUT: [(S)a]
OUTPUT:YES
代码实现一:基于括号数量统计的方法
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
bool match(char* str);
int main()
{
char str[100];
gets(str);
if (match(str))
{
cout << "YES" << endl;
}
else
cout << "NO" << endl;
return 0;
}
bool match(char* str)
{
int len = strlen(str);
int count_left_1 = 0, count_right_1 = 0;
int count_left_2 = 0, count_right_2 = 0;
char *p = str;
while (*p)
{
if (*p=='(')
{
++count_left_1;
}
if (*p==')')
{
++count_right_1;
}
if (*p == '[')
{
++count_left_2;
}
if (*p == ']')
{
++count_right_2;
}
if (count_right_1>count_left_1||count_right_2>count_left_2)
{
return false;
}
++p;
}
if (count_left_1 == count_right_1&&count_left_2 == count_right_2)
{
return true;
}
else
return false;
}
代码实现二:基于栈的思想
#include<iostream>
#include<cstring>
#include <vector>
#include <string>
using namespace std;
int main()
{
char str[100] = { '0' };
while (cin >> str)
{
bool isBracket = false;
int len = strlen(str);
vector<char> stack;
for (int i = 0; i < len; ++i)
{
int size;
size = stack.size();
if (str[i] == '(')
{
if (size>0 && stack.at(size - 1) == ')')
{
stack.pop_back();
}
else
stack.push_back(str[i]);
isBracket = true;
}
else if (str[i] == ')')
{
isBracket = true;
if (size > 0 && stack.at(size - 1) == '(')
{
stack.pop_back();
}
else
stack.push_back(str[i]);
}
else if (str[i] == '[')
{
isBracket = true;
if (size > 0 && stack.at(size - 1) == ']')
{
stack.pop_back();
}
else
stack.push_back(str[i]);
}
else if (str[i] == ']')
{
isBracket = true;
if (size > 0 && stack.at(size - 1) == '[')
{
stack.pop_back();
}
else
stack.push_back(str[i]);
}
}
if (stack.size() == 0&&isBracket)
{
cout << " YES " << endl;
}
else
{
cout << "NO" << endl;
}
}
return 0;
}
代码分析:
两种方法只需要遍历一次字符串,故时间复杂度为O(N);
方法一空间复杂度为O(N),方法二空间复杂度为O(1)
扩展:
INPUT: [(S)a]
OUTPUT:YES
代码实现一:基于括号数量统计的方法
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
bool match(char* str);
int main()
{
char str[100];
gets(str);
if (match(str))
{
cout << "YES" << endl;
}
else
cout << "NO" << endl;
return 0;
}
bool match(char* str)
{
int len = strlen(str);
int count_left_1 = 0, count_right_1 = 0;
int count_left_2 = 0, count_right_2 = 0;
char *p = str;
while (*p)
{
if (*p=='(')
{
++count_left_1;
}
if (*p==')')
{
++count_right_1;
}
if (*p == '[')
{
++count_left_2;
}
if (*p == ']')
{
++count_right_2;
}
if (count_right_1>count_left_1||count_right_2>count_left_2)
{
return false;
}
++p;
}
if (count_left_1 == count_right_1&&count_left_2 == count_right_2)
{
return true;
}
else
return false;
}
代码实现二:基于栈的思想
#include<iostream>
#include<cstring>
#include <vector>
#include <string>
using namespace std;
int main()
{
char str[100] = { '0' };
while (cin >> str)
{
bool isBracket = false;
int len = strlen(str);
vector<char> stack;
for (int i = 0; i < len; ++i)
{
int size;
size = stack.size();
if (str[i] == '(')
{
if (size>0 && stack.at(size - 1) == ')')
{
stack.pop_back();
}
else
stack.push_back(str[i]);
isBracket = true;
}
else if (str[i] == ')')
{
isBracket = true;
if (size > 0 && stack.at(size - 1) == '(')
{
stack.pop_back();
}
else
stack.push_back(str[i]);
}
else if (str[i] == '[')
{
isBracket = true;
if (size > 0 && stack.at(size - 1) == ']')
{
stack.pop_back();
}
else
stack.push_back(str[i]);
}
else if (str[i] == ']')
{
isBracket = true;
if (size > 0 && stack.at(size - 1) == '[')
{
stack.pop_back();
}
else
stack.push_back(str[i]);
}
}
if (stack.size() == 0&&isBracket)
{
cout << " YES " << endl;
}
else
{
cout << "NO" << endl;
}
}
return 0;
}
代码分析:
两种方法只需要遍历一次字符串,故时间复杂度为O(N);
方法一空间复杂度为O(N),方法二空间复杂度为O(1)
扩展:
相关文章推荐
- ios archives 出现的是other items而不是iOS Apps的解决方案
- Director类源代码不完全分析
- 任务推送系统的设计思路
- 操作系统页面置换算法(opt,lru,fifo,clock)实现
- css3动画图片变形,自定义字体
- RMQ with Shifts
- Spring学习日记(3)建立bean工场实现Dao层
- 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程(精)
- java反射机制浅析
- Codeforces Round #320 (Div. 2) 579C A Problem about Polyline(数学)
- 2.精通前端系列技术之JS模块化开发-深入学习seaJs(四)
- initWithCoder, initWithNibName, viewDidLoad的区别
- Linux下如何进行FTP设置
- hdu 4502 吉哥系列故事——临时工计划
- 【编译原理】:哈工大编译原理课程内容记录
- 四则运算题目生成器
- 《构建之法》阅读后不懂的5个问题 和个人项目总结
- Android xml的解析
- ACRush 楼天成回忆录
- MRC和ARC的兼容