zoukankan      html  css  js  c++  java
  • 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 };
  • 相关阅读:
    Go语言- import 导入包的语法
    go语言学习入门篇 3-- 程序执行流程
    go语言学习入门篇 2--轻量级线程的实现
    go语言学习入门篇1---go语言的主要特性与发展
    网络传输中的各种加密算法+SSL+CA证书详解
    压测工具 jmeter入门教程及汉化修改
    Array.isArray and Object.prototype.toString.call
    trim() 方法
    回文字符串判断
    监听微信返回按钮事件
  • 原文地址:https://www.cnblogs.com/wangxiaobao/p/5758373.html
Copyright © 2011-2022 走看看