【leetcode】Valid Parentheses

Question :  

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.

Anwser 1 :    Stack

class Solution {
public:
    bool isValid(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        stack<char> st;
        for(int i = 0; i < s.size(); i++)
        {
            if(s[i] == '(' || s[i] == '{' || s[i] == '['){
                st.push(s[i]);
            }
               
            if(s[i] == ')')
            {
                if(st.empty() || st.top() != '(')
                   return false;
                st.pop();
            }
            if(s[i] == '}')
            {
                if(st.empty() || st.top() != '{')
                   return false;
                st.pop();
            }
            if(s[i] == ']')
            {
                if(st.empty() || st.top() != '[')
                   return false;
                st.pop();
            }            
        }
        if(st.empty() == 0)
           return false;
           
        return true;
    }
};


Anwser 2 :   

class Solution {
public:
    bool isValid(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        stack<char> st;
        for (int i = 0; i < s.size(); i++) {
            char c = s[i];
            if (isLeft(c)) {    // push
                st.push(c);
            } else {
                if (st.empty()) {
                    return false;
                }
                char d = st.top();      // pop
                st.pop();
                if (!match(d,  c)) {
                    return false;
                }
            }
        }
 
        if (st.empty()) {
            return true;
        }
        else {
            return false;
        }
    }
 
    bool isLeft(char c) {
        return c == '{' || c == '[' || c == '(';
    }
    
    bool match(char c, char d) {
        return (c == '(' && d == ')') || (c == '[' && d == ']') || (c == '{' && d == '}');
    }
};


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
LeetCode是一个非常受欢迎的在线编程平台,提供了大量的算法题目,涵盖了各种难度级别和题型。下面是一些常用的LeetCode算法: 1. 两数之和(Two Sum):给定一个整数数组和一个目标值,找出数组中和为目标值的两个数的索引。 2. 反转字符串(Reverse String):反转给定字符串中的字符顺序。 3. 最长公共前缀(Longest Common Prefix):找出一组字符串中最长的公共前缀。 4. 合并两个有序链表(Merge Two Sorted Lists):将两个有序链表合并为一个新的有序链表。 5. 有效的括号(Valid Parentheses):判断给定的字符串中的括号是否有效。 6. 盛最多水的容器(Container With Most Water):给定一组非负整数,表示一组垂直线在x轴上的坐标,找出两条线与x轴组成的容器可以容纳的最大水量。 7. 三数之和(3Sum):给定一个包含n个整数的数组nums,判断nums中是否存在三个元素a,b,c,使得a + b + c = 0。 8. 最长回文子串(Longest Palindromic Substring):找出给定字符串中的最长回文子串。 9. 二叉树的最大深度(Maximum Depth of Binary Tree):计算二叉树的最大深度,即从根节点到最远叶子节点的最长路径上的节点数。 10. 两个排序数组的中位数(Median of Two Sorted Arrays):给定两个大小为m和n的有序数组,找出这两个数组合并后的中位数。 以上只是LeetCode中的一小部分常用算法题目,LeetCode还有很多其他类型的题目,包括动态规划、回溯、贪心算法等等。如果你有具体的算法问题或者需要更多的题目推荐,请告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值