算法Sedgewick第四版-第1章基础-010一检查括号是否成对出现
/******************************************************************************
* Compilation: javac Parentheses.java
* Execution: java Parentheses
* Dependencies: In.java Stack.java
*
* Reads in a text file and checks to see if the parentheses are balanced.
*
* % java Parentheses
* [()]{}{[()()]()}
* true
*
* % java Parentheses
* [(])
* false
*
******************************************************************************/ public class Parentheses {
private static final char LEFT_PAREN = '(';
private static final char RIGHT_PAREN = ')';
private static final char LEFT_BRACE = '{';
private static final char RIGHT_BRACE = '}';
private static final char LEFT_BRACKET = '[';
private static final char RIGHT_BRACKET = ']'; public static boolean isBalanced(String s) {
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == LEFT_PAREN) stack.push(LEFT_PAREN);
if (s.charAt(i) == LEFT_BRACE) stack.push(LEFT_BRACE);
if (s.charAt(i) == LEFT_BRACKET) stack.push(LEFT_BRACKET); if (s.charAt(i) == RIGHT_PAREN) {
if (stack.isEmpty()) return false;
if (stack.pop() != LEFT_PAREN) return false;
} else if (s.charAt(i) == RIGHT_BRACE) {
if (stack.isEmpty()) return false;
if (stack.pop() != LEFT_BRACE) return false;
} else if (s.charAt(i) == RIGHT_BRACKET) {
if (stack.isEmpty()) return false;
if (stack.pop() != LEFT_BRACKET) return false;
}
}
return stack.isEmpty();
} public static void main(String[] args) {
In in = new In();
String s = in.readAll().trim();
StdOut.println(isBalanced(s));
}
}
/*************************************************************************
*
* % java Ex_1_3_04
* [()]{}{[()()]()}
* true
*
* % java Ex_1_3_04
* [(])
* false
*
*************************************************************************/ public class Ex_1_3_04
{
public static boolean isBalanced(String s)
{
Stack<Character> open = new Stack<Character>();
int n = s.length(); for (int i = 0; i < n; i++)
{
char c = s.charAt(i); if (c == '(' || c == '[' || c == '{')
open.push(c);
else if ((c == ')' && (open.isEmpty() || open.pop() != '(')) ||
(c == ']' && (open.isEmpty() || open.pop() != '[')) ||
(c == '}' && (open.isEmpty() || open.pop() != '{')))
return false;
} return open.isEmpty();
} public static void main(String[] args)
{
String s = StdIn.readAll().trim(); StdOut.println(isBalanced(s));
}
}
算法Sedgewick第四版-第1章基础-010一检查括号是否成对出现的更多相关文章
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)
一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...
- 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的
1. package algorithms.stacks13; /******************************************************************* ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法
1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...
随机推荐
- fastadmin学习文档
https://doc.fastadmin.net/docs/index.html 介绍 FastAdmin是一款基于ThinkPHP5+Bootstrap的极速后台开发框架. 主要特性 基于Auth ...
- spring学习-3
spring的自动装配 spring IOC容器可以自动装配bean,只需要在bean的autowire属性指定自动装配的模式. 模式: 1.byType:根据类型自动装配.根据bean的类型和当前b ...
- @Override重写
package com.wisezone.f; //父类 public class Person { //姓名 private String name; //年龄 private int age; / ...
- swing之复杂登陆界面的实现
package jiemian; import gonggong.message; import gonggong.messageType; import gonggong.user; import ...
- 高并发下Redis如何保持数据一致性(避免读后写)
通常意义上我们说读后写是指针对同一个数据的先读后写,且写入的值依赖于读取的值. 关于这个定义要拆成两部分来看,一:同一个数据:二:写依赖于读.(记住这个拆分,后续会用到,记为定义一.定义二)只有当这两 ...
- Dubbo与Zookeeper
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提供的功 ...
- Python函数-any()
any(iterable) 作用: 如果iterable的任何元素不为0.''.False,all(iterable)返回True.如果iterable为空,返回False. 函数等价于: def a ...
- ubuntu lts install licode tag pre-v5.4
1. Requirements Ubuntu 14.04 LTS 2. Clone Licode codeYou first need to clone our code from github.Yo ...
- debian7.2+nginx+mysql
1.安装mysql sudo aptitude install mysql-server netstat -atln 查看3306端口 数据库目录: /var/lib/mysql/ 配置文件/usr/ ...
- 我和domino不得不说的故事(连载2016-3-2)
1.关于NotesViewEntry 注意:通过NotesViewEntry获取某列的值时,若该列的值为@IsExpandable or @DocNumber 或者是常量时,将不会显示. Set en ...