leetcode-20 valid-parentheses(有效的括号)
先看一下题目描述:
通过题目描述可以清楚的明白题目规则,输出true或者false。这道题需要用借用栈来实现,不说理解直接放代码
1 public boolean isValidParentheses(String s) { 2 // Write your code here 3 Stack<Character> stack = new Stack<>(); 4 for (char c : s.toCharArray()) { 5 if (c == '(' || c == '[' || c == '{') { 6 stack.push(c); 7 } 8 if (c == ')') { 9 if (stack.isEmpty() || stack.pop() != '(') { 10 return false; 11 } 12 } 13 if (c == ']') { 14 if (stack.isEmpty() || stack.pop() != '[') { 15 return false; 16 } 17 } 18 if (c == '}') { 19 if (stack.isEmpty() || stack.pop() != '{') { 20 return false; 21 } 22 } 23 } 24 return stack.isEmpty(); 25 } 26 }
还有一种进阶版,代码更少,但是思路更犀利,不太容易想到,同样借助栈
1 public boolean isValid(String s) { 2 Stack<Character> stack = new Stack<>(); 3 for(char c:s.toCharArray()){ 4 if(c=='('){ 5 stack.push(')'); 6 } 7 if(c=='{'){ 8 stack.push('}'); 9 } 10 if(c=='['){ 11 stack.push(']'); 12 } 13 else if(stack.isEmpty()||stack.pop()!=c){ 14 return false; 15 } 16 } 17 return stack.isEmpty(); 18 }
其实两段代码思路一模一样,非常巧妙
转载于:https://www.cnblogs.com/qingshan0216/p/9994952.html
leetcode-20 valid-parentheses(有效的括号)相关推荐
- LeetCode: 20. Valid Parentheses
0509第1题(虽然是08做的,但这会已经09了) 题目 Given a string containing just the characters '(', ')', '{', '}', '[' a ...
- 20. Valid Parentheses 有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...
- [LeetCode]--20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20 Valid Parentheses (C++)
问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- [swift] LeetCode 20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20. Valid Parentheses(c++)
利用栈的操作,遇到"(","[","{"即进栈,遇到")","]","}"判断是 ...
- 最长有效括号子串长度 c语言,LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)...
题目描述: Given a string containing just the characters'(' and')', find the length of the longest valid ...
- LeetCode Longest Valid Parentheses
原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...
- LeetCode 20. Valid Parentheses--笔试题--Python解法
题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...
- LeetCode 20. Valid Parenthese
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
最新文章
- MFC消息处理学习总结
- 在哪开启oracle服务器,开启企业殿堂的钥匙 Oracle服务器的安装
- Azure DevOps Server (TFS) 代码库Repo管理培训
- Feature Extraction
- 中国人工智能学会通讯——人工智能在各医学亚专科的发展现状及趋势 1.6 结束语...
- 托马斯微积分第十一版_企业微服务第一部分
- 报名系统 服务器,全国人事考试服务平台系统报名步骤(带报名入口)
- Ogre 天龙八部地形管理器(2) --- 地形和静态对象的加载
- html调用ckplayer说明,CKplayer功能配置(示例代码)
- 通用发票在线OCR识别,报销场景适用,支持近20种票据
- 质量免费--读书笔记(上篇)
- Softmax-with-Loss层的计算图 | Softmax梯度推导 | Loss损失函数
- 算法的时间复杂度表示
- 十六进制颜色值 (美观必备 - 设置颜色)
- GameofMir引擎架设传奇服务器【4:架设微端】
- gurobi中的lp
- Hexo博客icarus主题定制篇
- PUCCH(3)matlab验证ZC序列的性质
- Java-万物皆对象
- python星座属相查询_python的生肖和星座计算函数