Valid Parentheses
来源:互联网 发布:安全聊天软件 编辑:程序博客网 时间:2024/06/14 00:06
写这个简单的程序就用了两个小时,各种边界条件考虑不周,各种语法错误,正是醉了
思路很简单,基于栈的思想。如果遇到匹配的就出栈,不匹配就入栈,最后如果栈为空则返回true,否则则返回false。
1.C++中string类的使用 length()求长度,at()查找相应位置的字符,下标从0开始
2.栈为空的时候,需要进行判断
3.break直接跳出循环,循环不执行;continue 跳出本次循环,循环继续执行。
class Solution {public: bool isValid(string s) { int i, head; char stack[100], ch; i = 0; head = 0; while(i < s.length()){ ch = s.at(i); i++; if(head > 0 && (ch == ')' && stack[head - 1] == '(' || ch == '}' && stack[head - 1] == '{' || ch == ']' && stack[head - 1] == '[')){ head = head -1; continue; }else if(ch == ')' || ch == '}' || ch == ']'){ return false; } stack[head] = ch; head++; } if(head == 0) return true; else return false; }};
0 0
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- java基础_集合
- OSI七层模型与TCP/IP协议(族)
- c++中的继承和多态
- 在web项目中禁止在jsp中的EL表达式
- Windows远程登录到VirtualBox安装的Ubuntu14.04 LTS
- Valid Parentheses
- switch的参数类型
- Reverse Words in a String --leetcode
- 那些不能错过的Xcode插件
- 第12周项目2-刑警的射击成绩
- php 验证码制作
- LA3938 & UVA1400 - Ray, Pass me the dishes!(线段树)
- MySQL的timeout相关配置参数小结一把
- 近阶段计划 - 图论算法理论、实现及应用