题目原文:

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. CentOS7将firewall切换为iptables防火墙

  2. nagios 安装pnp4nagios插件

    Naigos install pnp4nagios 绘图插件 原文地址:http://www.cnblogs.com/caoguo/p/5022230.html [root@Cagios ~]# yu ...

  3. Python 之scrapy框架58同城招聘爬取案例

    一.项目目录结构: 代码如下: # -*- coding: utf-8 -*- # Define here the models for your scraped items # # See docu ...

  4. CentOS7阿里云服务器,python程序requests无法正常post网站(报502)

    问题描述: 使用jenkins构建接口自动化测试时,发现新增加的接口case不能访问通,会报502错误(本地可以跑通,在测试服就会502)解决的思路: 缩小调试范围(去掉jenkins db环境,将问 ...

  5. groupbox

    使用groupbox将radiobox 放入其中可以使组框中只选中一个

  6. Charles 下载-破解-安装-配置

    我当前使用版本为V4.2.7 最新版本下载地址 Charles 在线破解工具 下载完之后,先进行安装,安装完之后,根据破解链接中的步骤来就ok了. 比较费劲配置在下面,不过跟着一步步来就一定能好的 点 ...

  7. http-runtime属性

    配置httpRuntime也可以让FileUpload上传更大的文件,不过设置太大了会因用户将大量文件传递到该服务器而导致的拒绝服务攻击(属性有说明) 属性 属性 选项 说明 appRequestQu ...

  8. RDS for MySQL Mysqldump 常见问题和处理

    https://help.aliyun.com/knowledge_detail/41732.html?spm=5176.7841698.2.13.u67H3h

  9. open cursor too much error

    今天遇到一个错误ORA-01000: maximum open cursors exceeded. 客户想增加 DB 的open_cursor这个参数. 但是我看了下,她的程序要打开几千个cursor ...

  10. HDU 5435

    数位DP题,然而不会做.设dp[i][j]表示前i位异或和为j的时候的个数.先dp出所有的可能组合使得异或和为j的个数,然后按位进行枚举.对于dp[i][j],其实不止是前i位,对于后i位的情况同样适 ...