LeetCode——Valid Parenthese
Leetcode——Valid Parenthese
#20
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.
这一题明显是使用栈来处理这种括号匹配问题。主要的思路可以理出来,给一个字符串,有以下几种情况:
1. 对于左括号,直接放入栈中
2. 对于右括号,如果栈为空,返回false;如果栈不为空,如果栈顶为匹配的左括号,则成功,将左括号出栈,如果不是匹配的左括号,返回false。
这样就可以很容易的写出代码段:
class Solution {
public:bool isValid(string s) {stack<char> stack_p;for(int i = 0;i < s.size();i++){if(s[i] == '(' || s[i] == '{' || s[i] == '[')stack_p.push(s[i]);else{if(stack_p.empty()) return false;if(s[i] == ')' && stack_p.top() != '(') return false;if(s[i] == '}' && stack_p.top() != '{')return false;if(s[i] == ']' && stack_p.top() != '[')return false; stack_p.pop();} }return stack_p.empty();}
};
java的代码思路基本是一样的。
- Java
class Solution {public boolean isValid(String s) {if(s.length() <= 1)return false;Stack<Character> st = new Stack<Character>();for(int i = 0;i < s.length();i++){if(s.charAt(i) == '('||s.charAt(i) == '{'||s.charAt(i) == '['){st.push(s.charAt(i));else{if(st.size()==0)return false;char top = st.pop();if(s.charAt(i)==')')if(top!='(')return false;else if(s.charAt(i)=='}')if(top!='{')return false;else if(s.charAt(i)==']')if(top!='[')return false;}}return st.isEmpty();
}
- Python
class Solution:def isValid(self, s):""":type s: str:rtype: bool"""parenthese = []map_parenthese = {')':'(','}':'{',']':'['}for c in s:if c in map_parenthese:if map_parenthese[c] != parenthese.pop()return false
速度上,python肯定是要慢的。
LeetCode——Valid Parenthese相关推荐
- 32 Longest Valid Parenthese
32 Longest Valid Parenthese 题意 解法 代码 算法复杂度 注意 题目链接:32 Longest Valid Parenthese 题意 给出一个由字符'('或')'组成的字 ...
- LeetCode 20. Valid Parenthese
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- Leetcode 20.Valid Parenthese
我的思路 很简单, 遇到 ().[].{} 这三对符号的左边的就入栈, 遇到右边的就直接将栈顶元素与其对应进行比较, 能组成一对符号就出栈, 否则就返回 false. 因为有 6 种情况就使用 swi ...
- LeetCode Valid Sudoku
原题链接在这里:https://leetcode.com/problems/valid-sudoku/ 题目: Determine if a Sudoku is valid, according to ...
- leetcode Valid Palindrome
题目连接 https://leetcode.com/problems/valid-palindrome/ Valid Palindrome Description Given a string, de ...
- leetcode—Valid Parentheses
1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...
- LeetCode : Valid Parentheses
问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- LeetCode - Valid Sudoku
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode --- Valid Parentheses
题目链接 Problem discription Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...
最新文章
- 【iOS】iOS之Button segue弹出popOver消除(dismiss)问题
- myeclipse10安装了activiti插件后创建BPMN 文件时报错,
- python元类的简单了解
- spring的aop的动态代理机制都有哪些_Spring学习(4):Spring AOP
- Android 获取外网IP,实测有效
- javascript 继承、子类化
- 【2019/5/24】周进度报告
- asp小偷转html,ASP中实现小偷程序的原理和简单示例
- python爬取酷狗音乐付费音乐包_一篇文章教会你使用Python网络爬虫下载酷狗音乐...
- 基于FPGA的AD/DA实验
- 为什么标签天线振子长度小于半个波长?
- 重磅 | 智能工厂整体实施方案(附PPT下载)
- 虚拟化技术——VLAN策略
- URLRewrite地址重定向的实现
- 2021-11-02发电机转子方程的推导
- 【剑指offter】【C++】【二叉树】27. 二叉树的镜像
- 游戏脚本引色粉,无脑圈钱攻略
- 兴趣图谱聚集用户 社区化电子商务正爆发
- web-pwa 未来10年核心前端技术
- 三亚自由行八天七晚旅行攻略