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系统的学一遍。
改进计划:语句中单词的含义要熟悉。应该有很多常用的单词。