题目原文:

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. 【译】x86程序员手册10 - 第4章系统架构

    1.1.2 Part II -- Systems Programming 系统编程 This part presents those aspects of the architecture that ...

  2. jquery from使用

    jquery form是一个基于jquery的表单异步提交的插件,通过它能快速简便的提交表单. html <div> <form id="ajaxForm" me ...

  3. Ajax系列面试题总结

    1.Ajax是什么?如何创建一个Ajax? Ajax并不算是一种新的技术,全称是asychronous javascript and xml,可以说是已有技术的组合,主要用来实现客户端与服务器端的异步 ...

  4. 关于static关键字的思考

    静态方法是否能调用非静态成员变量?    static关键字具有如下特点:        一.static关键字修饰的属性/方法可以通过类名直接调用,而不必先new一个对象.        二.sta ...

  5. jQuery练习:表单模态框

    代码:基于事件冒泡原理和事件委托 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta cha ...

  6. Python基础-画菱形

    方法一 n = int(input('请输入:')) for i in range(1, n, 2): print(('*'*i).center(n)) for i in reversed(range ...

  7. vue 中全局filter过滤器的配置及使用

    在项目中使用到的经常用到过滤器,比如时间,数据截取等过滤器,如果在每个.vue中都可以复制同一个过滤器,这可以达到目的,但是遇到方法有bug时就需要诸葛修改进入不同的页面修改,这样既费时又费力,优先可 ...

  8. selenium动作链

    简介 一般来说我们与页面的交互可以使用Webelement的方法来进行点击等操作. 但是,有时候我们需要一些更复杂的动作,类似于拖动,双击,长按等等. 这时候就需要用到我们的Action Chains ...

  9. Maven学习总结(4)——Maven核心概念

    Maven学习总结(四)--Maven核心概念 一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1.2.Maven坐标主要组成 groupId:组织标识 ...

  10. 重庆OI2017 老 C 的任务

    老 C 的任务 时间限制: 2 Sec  内存限制: 512 MB 题目描述 老 C 是个程序员. 最近老 C 从老板那里接到了一个任务——给城市中的手机基站写个管理系统.作为经验丰富的程序员,老 C ...