LeetCode20 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. (Easy)

这道题是8月5号做的,居然没有写博客当时...最近真是乱了,顺便整理了一下做题日志...

分析:

比较简单,弄一个栈,左括号压栈,右括号匹配了就弹栈,不匹配或没有元素了return false, 一遍走完了判断栈是否为空。

 1 class Solution {
 2 public:
 3     bool isValid(string s) {
 4         if (s.size() == 0) {
 5             return true;
 6         }
 7         stack<char> sta;
 8         sta.push(s[0]);
 9         for (int i = 1; i < s.size(); ++i) {
10             if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
11                 sta.push(s[i]);
12                 continue;
13             }
14             if ( (s[i] == ')' || s[i] == ']' || s[i] == '}') && sta.empty() ) {  // "()]"
15                 return false;
16             }
17             if (s[i] == ')') {
18                 if (sta.top() == '(') {
19                     sta.pop();
20                     continue;
21                 }
22                 else {
23                     return false;
24                 }
25             }
26             if (s[i] == ']') {
27                 if (sta.top() == '[') {
28                     sta.pop();
29                     continue;
30                 }
31                 else {
32                     return false;
33                 }
34             }
35             if (s[i] == '}') {
36                 if (sta.top() == '{') {
37                     sta.pop();
38                     continue;
39                 }
40                 else {
41                     return false;
42                 }
43             }
44             return false;
45         }
46         if (sta.empty()) {
47             return true;
48         }
49         else {
50             return false;
51         }
52     } 
53 };

第一次提交的时候忘了sta.empty()也return false 的情况;

其次,代码冗余太多,写的有点长,优化一下。

 1 class Solution {
 2 public:
 3     bool isValid(string s) {
 4         if (s.size() == 0) {
 5             return true;
 6         }
 7         stack<char> sta;
 8         sta.push(s[0]);
 9         for (int i = 1; i < s.size(); ++i) {
10             if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
11                 sta.push(s[i]);
12                 continue;
13             }
14             else {
15                 if ( sta.empty() ) {  // "()]"
16                     return false;
17                 }
18                 if (s[i] == ')' && sta.top() != '(') {
19                      return false;
20                 }
21                 if (s[i] == ']' && sta.top() != '[') {
22                      return false;
23                 }
24                 if (s[i] == '}' && sta.top() != '{') {
25                      return false;
26                 }
27                 sta.pop();
28             }
29             
30         }
31         return sta.empty();
32     } 
33 };

 

posted @ 2016-08-10 20:49  wangxiaobao1114  阅读(157)  评论(0编辑  收藏  举报