Valid Parentheses

Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
An input string is valid if:
1.Open brackets must be closed by the same type of brackets.
2.Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.

Example 1:

Input: “()”
Output: true

Example 2:

Input: " ( ) [ ] { } "
Output: true

Example 3:

Input: “( ]”
Output: false

Example 4:

Input: " ( [ ) ] "
Output: false

Example 5:

Input: " { [ ] } "
Output: true

C++解法:

class Solution 
{
public:
    bool isValid(string s) {
        unordered_map<char,int> m{{'(',1},{'[',2},{'{',3},
                                {')',4},{']',5},{'}',6}};
        stack<char> st;
        bool istrue=true;
        for(char c:s){
            int flag=m[c];
            if(flag>=1&&flag<=3) st.push(c);
            else if(!st.empty()&&m[st.top()]==flag-3) st.pop();
            else {istrue=false;break;}
        }
        if(!st.empty()) istrue=false;
        return istrue;
    }
};

学习目标: 熟悉堆的使用

今日内容:练习通过堆完成括号匹配

目标是否成功:Y

复述自己成功的过程或者是失败的原因: 虽然任务完成了,对与堆的使用也更加熟悉了。但是这个堆不是自己写的。没有起到为考研加深学习的作用,因为我感觉是自己能够很快速的写出来堆的所有功能和用法。那才算实现目的。从另一个角度来讲也算是为计试做了训练吧,也算收获不小。

时长:60分钟
效率评估:中等

收获:1. 函数和标记的名字尽量用合理的英语;2.STL的map的使用方法和含义;

观点有什么改变: 重点是做关于实现堆的题。而不是应用堆栈。需要去哔哩哔哩深入学习一下STL。一道题的时间可能得小时起步。一天做不了几道题。3到4道题已经很极限了。

规律+原则: 遇到知识要成体现的学。比如这个题目里面的map我就不是很熟悉。那么我就把整个STL系统的学一遍。

改进计划:语句中单词的含义要熟悉。应该有很多常用的单词。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值