栈(4)
来源:互联网 发布:淘宝首页设计怎么设计 编辑:程序博客网 时间:2024/05/19 22:03
原题:
/** * Created by gouthamvidyapradhan on 25/02/2017. * Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. * <p> * The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. */
答案:
public class ValidParentheses { public static void main(String[] args) { System.out.println(hasBalancedBrackets("(h[e")); } private static Map<Character, Character> MAP = new HashMap<>(); // METHOD SIGNATURE BEGINS, THIS METHOD IS REQUIRED public static int hasBalancedBrackets(String str) { if (str == null) return 1; MAP.put(')', '('); MAP.put('}', '{'); MAP.put('>', '<'); MAP.put(']', '['); Stack<Character> stack = new Stack<>(); for (int i = 0, l = str.length(); i < l; i++) { switch (str.charAt(i)) { case '(': case '{': case '[': case '<': stack.push(str.charAt(i)); break; case ')': case '}': case ']': case '>': if (stack.isEmpty()) return 0; char top = stack.pop(); if (top != MAP.get(str.charAt(i))) return 0; break; default: //ignore } } return stack.isEmpty() ? 1 : 0; }}
阅读全文
0 0
- 栈(4)
- 数据结构4(顺序栈)
- 考研复习(4)-栈操作
- 数据结构与算法(4)顺序栈
- 栈 4
- 4、栈
- 面试---数据结构(4)(队列和栈)
- 数据结构基础(4)C语言实现栈--链式存储(动态栈)
- linux内核网络协议栈学习笔记(4)
- Activity的启动模式和任务栈(4)
- 数据结构(4) 顺序栈 c++ 模板实现
- 每天一个小程序(4)——顺序栈
- 第三章:栈与队列算法设计题(4)
- 数据结构和算法学习(4)-栈和队列
- 第6周项目4 数字转换(栈)
- 2015年大二上-数据结构-栈(4)- 数制转换
- Linux内核协议栈(4) 跟踪sendto调用
- (第4讲)栈和队列的数组实现
- uboot移植与源码分析总结(4)-以太网驱动
- java EE开发之Struts2第一章
- angulaJS正反选单个删除
- 栈(3)
- 智能化社会训练
- 栈(4)
- AngularJS用户列表的增删改查样式
- KCF代码分析
- Pandas的 loc iloc ix 区别
- banner实现无限轮播+下拉刷新上拉加载+ 下拉时 listview 和轮播图一起刷新
- ListCtrl----虚拟列表的用法
- 两指针(1)
- C 数组
- 7.0 设置中语言列表语言多出XB XA 两种语言