leetcode刷题-堆栈3
来源:互联网 发布:淘宝第五大道是正品吗 编辑:程序博客网 时间:2024/06/05 09:08
package com.zwd.wkst.leetcode.stack;import java.util.Stack;/** * Created by zhangwd4 on 2017/11/2. * * 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; 当程序运行完,栈是空,返回true */public class Solution2 { public boolean isValid(String s) { if(s ==null || s.isEmpty()){ return false; } Stack<Character> stack = new Stack<>(); for(char c : s.toCharArray()){ if(c == '(' || c== '[' || c == '{') stack.push(c); else if(c == ')' && !stack.empty() && stack.peek() == '(') stack.pop(); else if(c == ']' && !stack.empty() && stack.peek() == '[') stack.pop(); else if(c == '}' && !stack.empty() && stack.peek() == '{') stack.pop(); else return false; } return stack.isEmpty(); }}
阅读全文
0 0
- leetcode刷题-堆栈3
- leetcode刷题-堆栈
- leetcode刷题-堆栈2
- leetcode 堆栈
- LeetCode 之堆栈 stack
- LeetCode-20-Valid Parentheses(堆栈)-Easy
- 堆栈练习3:HDOJ1022
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 转置3乘3
- WebPack详细入门教程(三)之loader加载器
- wordpress首页无法看到添加的目录
- springboot+bootstrap fileupinput 插件实现文件上传
- js父子页面相互调用变量及方法
- leetcode刷题-堆栈3
- 利用HttpUrlConnection 上传 接收文件的实现方法
- 1004. Counting Leaves (30)
- Etherlab源码解析----同步管理器SM配置(邮箱通信)
- 11.2笔记整理及作业
- input type=file 如何达到去除输入框效果
- java访问https请求
- 两个栈实现一个队列
- 服务器