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,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 原文是使用 ...
随机推荐
- Python_多线程1(创建线程,简单线程同步)
threading 模块除了包含 _thread 模块中的所有方法外,还提供的其他方法: threading.currentThread(): 返回当前的线程变量. threading.enumera ...
- linux搭建mysql服务器及可视化工具
环境: ubutnu 18.4 mysql 5.7 参考: 安装 https://www.cnblogs.com/opsprobe/p/9126864.html 配置用户权限 https://baij ...
- 用 ilasm 反编译、修改.net dll文件
有些.net dll我们没有源码,如果要修改某些东西,可以用ilasm.exe反编译为il代码,修改后再编译回dll ilasm通常放在以下路径 C:\Windows\Microsoft.NET\Fr ...
- 使用原生JS的AJAX读取json全过程
首先ajax(async javascript and xml)是用于前端与后端文件比如xml或者json之间的交互.他是一种异步加载技术,意味着你点击某个加载事件是再也不用刷新整个页面,而是发送局部 ...
- EF-Linq
一丶基本语法(from a in Table where a.id="001" select a).Tolist(); 隐式内连接from a in table1 join b i ...
- 16.1 foreach 循环中捕获变量的变化
在 foreach 循环内的匿名函数(通常为Lambda表达式)中捕获循环 变量时要格外小心.代码清单16-1就展示了这样一个简单的示例,它看上去似乎会输出 x . y . z . string[] ...
- 总结这几天js的学习内容
对js中难点的理解 1.把变量对象像遍历数组一样简单 对于数组 ,迭代出来的是数组元素,对于对象 ,迭代出来的是对象的属性: var obj = { w: "wen", j: &q ...
- 基础命令history
history 记录历史命令 环境变量: HISTSIZE: 命令历史记录的条数: HISTFILE: 命令历史记录的文件,~/.bash_history: HISTFILESIZE: 命令历史 ...
- linux - redis基础
目录 linux - redis基础 redis 源码编译安装 redis 数据结构 1. strings类型 2. list 类型 3. sets集合类型 有序集合 5. 哈希数据结构 centos ...
- Dijkstra算法求最短路径
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h&g ...