题目原文:

Stack with max. Create a data structure that efficiently supports the stack operations (push and pop) and also a return-the-maximum operation. Assume the elements are reals numbers so that you can compare them.

分析:

该题目要求在实现正常stack的push和pop操作外,还需要实现返回maximum的操作,显然一个数组是不够的,需要额外的数组maximums[]来从小到大存储stack内的值,每次返回maximum就返回maximums[n-1],可满足要求。

 import java.util.Arrays;

 /**
* @author evasean www.cnblogs.com/evasean/
*/
public class StackWithMax {
public double[] items;// 为了方便展示结果,我也懒得写遍历方法,就设为public
public double[] maximums; // 存放最大值的数组,为了方便展示结果,我也懒得写遍历方法,就设为public
private int n;
private int cap; public StackWithMax() {
n = 0;
cap = 2;
items = new double[cap];
maximums = new double[cap];
} public boolean isEmpty() {
return n == 0;
} public void push(double item) {
if (n == 0)
maximums[0] = item;
else {
int i;
int j;
for (i = n - 1; i >= 0; i--) {// 这个循环用来找出item在maximums数组中应该放置的位置
if (item <= maximums[i])
continue;
else
break;
}
for (j = n - 1; j > i; j--) {// 将位置i以后的元素都往后挪一个位置
maximums[j + 1] = maximums[j];
} // 循环结束时j指向i
maximums[j + 1] = item;// j+1就是item应该放置的位置
}
items[n++] = item;
if (n == cap)
resize(2 * cap);
} public double pop() {
double item = items[--n];
if (n > 0 && n == cap / 4)
resize(cap / 2);
return item;
} public double maximum() {
return maximums[n - 1];
} private void resize(int cap) {
double[] t1 = new double[cap];
double[] t2 = new double[cap];
for (int i = 0; i < n; i++) {
t1[i] = items[i];
t2[i] = maximums[i];
}
items = t1;
maximums = t2;
this.cap = cap;
} public static void main(String[] args) {
StackWithMax stack = new StackWithMax();
stack.push(9);
stack.push(8);
stack.push(11);
stack.push(0);
stack.push(-9.9);
stack.push(88);
System.out.println(Arrays.toString(stack.items));
System.out.println(Arrays.toString(stack.maximums));
stack.pop();
System.out.println(stack.maximum());
}
}

Coursera Algorithms week2 栈和队列 练习测验: Stack with max的更多相关文章

  1. Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks

    题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized num ...

  2. Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法

    第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...

  3. Coursera Algorithms week2 基础排序 练习测验: Permutation

    题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...

  4. Coursera Algorithms week2 基础排序 练习测验: Intersection of two sets

    题目原文: Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subq ...

  5. Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space

    题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a const ...

  6. Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST

    题目原文: Given a binary tree where each 

  7. Coursera Algorithms week4 基础标签表 练习测验:Java autoboxing and equals

    1. Java autoboxing and equals(). Consider two double values a and b and their corresponding Double v ...

  8. 学习javascript数据结构(一)——栈和队列

    前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...

  9. 剑指Offer面试题:6.用两个栈实现队列

    一.题目:用两个栈实现队列 题目:用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 原文是使用 ...

随机推荐

  1. lnmp环境搭建后续-php安装

    安装PHP7: 下载# wget http://PHP.net/get/php-7.0.2.tar.gz/from/a/mirror 建议安装之前先看看安装帮助文件INSTALL 解压安装 # tar ...

  2. JavaScript学习书签

    JavaScript常用正则表达式 闭包 JavaScipt DOM 变量提升

  3. CPU 指令集(Instruction Set Architecture, ISA)

    本文摘自网络 概念 指令集是存储在CPU内部,对CPU运算进行指导和优化的硬程序,用来引导CPU进行加减运算和控制计算机操作系统的一系列指令集合.拥有这些指令集,CPU就可以更高效地运行.系统所下达的 ...

  4. git 缓存密码导致的不能和远程仓库交互unable to access... 403错误

    尝试了各种方式,包括卸载等最终解决方案: 查看本机的credential 是否已经被清空. 如果输入了 git config credential.helper 命令之后没有输出,说明 git 的配置 ...

  5. HDU 3152 Obstacle Course(优先队列,广搜)

    题目 用优先队列优化普通的广搜就可以过了. #include<stdio.h> #include<string.h> #include<algorithm> usi ...

  6. 思维风暴 codeforces (1060A) Phone Numbers

    这个题我真是我的问题,我看到这种题直接就想着怎么用string去枚举破解,开了一个数组去做结果模拟失败,可能开个stl容器能做的好一点...但是这个题完全不是这样做的...实际上直接比较8的个数和合法 ...

  7. MySQL(端口3306)

    MySQL(二进制)安装: 下载地址:http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.49-linux2.6-x86_64.tar.gz ...

  8. SCI 论文金句

    SCI 不会写?其实英语基础好一点,文献多看一点,多写写自然就能自己写出来了.当然,你肯定会说英语真的好难,好吧,就知道你们懒得学英语了.我给你们整理了一套万能模板,涵盖了论文不同部分的常用句型. 摘 ...

  9. linux学习7-数据流重定向

    数据流重定向 实验介绍 你可能对重定向这个概念感到些许陌生,但你应该在前面的课程中多次见过>或>>操作了,并知道他们分别是将标准输出导向一个文件或追加到一个文件中.这其实就是重定向, ...

  10. 【codeforces 758D】Ability To Convert

    [题目链接]:http://codeforces.com/contest/758/problem/D [题意] 给你一个n进制的数k; 问你它可能的最小的十进制数是多少; [题解] 从右往左; 获取数 ...