Coursera Algorithms week2 栈和队列 练习测验: Stack with max
题目原文:
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的更多相关文章
- Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks
题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized num ...
- Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法
第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...
- Coursera Algorithms week2 基础排序 练习测验: Permutation
题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...
- 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 ...
- 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 ...
- Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST
题目原文: Given a binary tree where each
- Coursera Algorithms week4 基础标签表 练习测验:Java autoboxing and equals
1. Java autoboxing and equals(). Consider two double values a and b and their corresponding Double v ...
- 学习javascript数据结构(一)——栈和队列
前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...
- 剑指Offer面试题:6.用两个栈实现队列
一.题目:用两个栈实现队列 题目:用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 原文是使用 ...
随机推荐
- Win32编程笔记
我都决定了目前不再接触这些个浪费精力的API了,结果为了DirectX编程我特么又回来了.....微软你的东西真是坑人 以前用这玩意的时候需要什么就查,查完就忘了,这次记一记,以后再用也不至于忘的太离 ...
- java的四种引用,强弱软虚和jvm优化
1.强引用(StrongReference)强引用是使用最普遍的引用.如果一个对象具有强引用,那垃圾回收器绝不会回收它.如下: Object o=new Object(); // 强引用 当内存 ...
- python合并多个txt文件成为一个文件
#coding=utf-8 import os import os.path #文件夹遍历函数 #获取目标文件夹的路径 filedir = './data/click_data' #获取当前文件夹中的 ...
- array_map 等php回调函数使用问题(关联数组下标获取)
前言:我自己用此类回调函数,来替代 foreach 纯粹是用为代码的简洁性,让代码更好看.(我有点代码小洁癖~) 1.array_reduce 当迭代处理一个一维索引数组时,在回调函数内是无法获取到当 ...
- Python模块 os.walk
Os.walk os.walk(top,topdown=True,onerror=None,followlinks=False) os.walk()是python中内置(built-in)的目录树生成 ...
- 【转载】Apache shutdown unexpectedly启动错误解决方法
http://blog.csdn.net/dong123dddd/article/details/21372179 xampp启动时显示的错误为: 9:52:41 [Apache] Attempti ...
- Django——13 Auth系统 登陆注册实例 权限的实现
Django Auth系统中的表 注册登陆实例 权限的实现 登陆权限 操作权限 组操作 Auth系统中的表 从表的名称我们就能看出,auth_user,auth_group,auth_permiss ...
- ubuntu添加开机自启和sysv-rc-conf
此文ubuntu使用sysvinit,而非upstart UBUNTU添加开机自动启动程序方法 1. 开机启动时自动运行程序 Linux加载后, 它将初始化硬件和设备驱动, 然后运行第一个进程i ...
- 清北学堂模拟赛d2t1 一道图论神题(god)
题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图,只有点权. LYK想把这个图删干净,它的方法是这样的.每次选择一个点,将它删掉,但删这个点是需要代价的.假 ...
- SSM(spring mvc+spring+mybatis)学习路径——2-2、spring MVC拦截器
目录 2-2 Spring MVC拦截器 第一章 概述 第二章 Spring mvc拦截器的实现 2-1 拦截器的工作原理 2-2 拦截器的实现 2-3 拦截器的方法介绍 2-4 多个拦截器应用 2- ...