cci-Q3.1 一个数组实现三个栈
来源:互联网 发布:linux war解压 编辑:程序博客网 时间:2024/05/16 05:23
原文:
Describe how you could use a single array to implement three stacks.
译文:
你如何只用一个数组实现三个栈?
根据栈的大小,申请一个3*stacksize的数组。
public class stack3 { int[] buffer = null; int size = 0; int[] stackpt = {-1, -1, -1}; public stack3(int size) { this.size = size; this.buffer = new int[this.size * 3]; } boolean push(int stackNum, int value) { if (stackpt[stackNum] > size - 2) { return false; } int index = stackNum * this.size + stackpt[stackNum] + 1; stackpt[stackNum]++; buffer[index] = value; return true; } int pop(int stackNum) { if (this.stackpt[stackNum] < 0) { return Integer.MIN_VALUE; } int index = stackNum * this.size + stackpt[stackNum]; stackpt[stackNum]--; int val = buffer[index]; buffer[index] = 0; return val; } int peek(int stackNum) { if (this.stackpt[stackNum] < 0) { return Integer.MIN_VALUE; } int index = stackNum * this.size + stackpt[stackNum]; return buffer[index]; } boolean isEmpty(int stackNum) { return stackpt[stackNum] == -1; }}
testcase
public static void main(String args[]) { stack3 stack = new stack3(20); for (int i = 0; i < 20; i++) { stack.push(0, i); } for (int i = 0; i < 10; i++) { System.out.println(stack.pop(0)); } System.out.println(stack.pop(1)); System.out.println(stack.isEmpty(2)); System.out.println(stack.peek(0)); }
- cci-Q3.1 一个数组实现三个栈
- 一个数组实现三个栈
- cci-Q3.2 设计栈可以返回最小值
- 用一个数组实现三个栈
- Stack_Queue 一个数组实现三个栈 @CareerCup
- 用一个数组实现三个栈
- 用一个数组实现三个栈结构
- Chapter 3 | Stacks and Queues--一个数组实现三个栈
- 每日一练--一个数组实现三个栈
- 【Java】一个数组实现三个栈 (未完待续)
- 描述如何只用一个数组来实现三个栈
- 如何只用一个数组来实现三个栈
- 3.1-一个数组模拟三个栈
- 9.3栈和队列(二)——用一个数组来实现三个栈
- Chapter 3 | Stacks and Queues--一个数组实现三个栈(续)
- 程序员面试金典(8)一个数组实现三个栈
- Q3.1
- 在一个数组中实现三个栈,并且数组未满之前栈不能溢出的问题
- java的final关键字与闭包
- Ubuntu下 MIUI V5 adb devices 无法识别
- 多线程实现买票功能
- 滚动条到底自动加载数据
- c shell 整理文件格式命令
- cci-Q3.1 一个数组实现三个栈
- 回车符号与换行符号
- 从决策树学习谈到贝叶斯分类算法、EM、HMM
- hoops画箭头
- webservice的原理及概念
- 文字滚动特效
- gallery利用代码定位图片并且不丢失动画效果
- 修改Linux的帮助文档,通过修改/etc/manpages.config可以指定语言
- 请听一个故事------>你真的认为iPhone只是一部手机?苹果惊天秘密!!