题目描述
请实现一种数据结构SetOfStacks,由多个栈组成,其中每个栈的大小为size,当前一个栈填满时,新建一个栈。该数据结构应支持与普通栈相同的push和pop操作。
给定一个操作序列int[][2] ope(C++为vector<vector<int>>),每个操作的第一个数代表操作类型,若为1,则为push操作,后一个数为应push的数字;若为2,则为pop操作,后一个数无意义。请返回一个int[][](C++为vector<vector<int>>),为完成所有操作后的SetOfStacks,顺序应为从下到上,默认初始的SetOfStacks为空。保证数据合法。
根据下面代码完成功能的编写:可知使用ArrayList<Integer>模拟栈及其操作
import java.util.*;
public class SetOfStacks {
public ArrayList<ArrayList<Integer>> setOfStacks(int[][] ope, int size) {
// write code here
}
}
则代码 如下:
import java.util.*;
public class SetOfStacks {
public ArrayList<ArrayList<Integer>> setOfStacks(int[][] ope, int size) {
// write code here
// ArrayList<Integer>模拟栈及其操作
ArrayList<ArrayList<Integer>> stacks=new ArrayList<ArrayList<Integer>>(); // 栈列表
ArrayList<Integer> currunt=null; //当前的栈
for(int i = 0; i < ope.length; i++){
if(ope[i][0] == 1){ // push
if(currunt == null || currunt.size() >= size){ //新建栈
currunt = new ArrayList<Integer>();
currunt.add(ope[i][1]);
stacks.add(currunt);
}else{
currunt.add(ope[i][1]);
}
}else{ // pop
if(currunt.size() > 0){
currunt.remove(currunt.size()-1);
}else{
stacks.remove(stacks.size()-1);
currunt = stacks.get(stacks.size()-1);
currunt.remove(currunt.size()-1);
}
}
}
return stacks;
}
}
根据程序员面试金典:
实现弹出指定的子栈
package com.msjd.list;
public class Stack {
private int capacity;
public Node top, buttom;
public int size = 0;
public Stack(int capacity) {
this.capacity = capacity;
}
public boolean isFull() {
return capacity == size;
}
public void join(Node above, Node below) {
if (below != null) {
below.above = above;
}
if (above != null) {
above.below = below;
}
}
public boolean push(int v) {
if (size >= capacity) {
return false;
}
size++;
Node n = new Node(v);
if (size == 1)
buttom = n;
join(n, top);
top = n;
return true;
}
public int pop() {
Node t = top;
top = top.below;
size--;
return t.value;
}
public boolean isEmpty() {
return size == 0;
}
public int removeButtom() {
Node b = buttom;
buttom = buttom.above;
if (buttom != null) {
buttom.below = null;
}
size--;
return b.value;
}
class Node {
Node above;
Node below;
int value;
public Node(int value) {
this.value = value;
}
}
}
package com.msjd.list;
import java.util.ArrayList;
public class SetOfStacks1 {
ArrayList<Stack> stacks = new ArrayList<Stack>();
public int capacity;
public SetOfStacks1(int capacity) {
this.capacity = capacity;
}
public Stack getLastStack() {
if (stacks.size() == 0)
return null;
return stacks.get(stacks.size() - 1);
}
// 入栈
public void push(int v) {
Stack last = getLastStack();
if (last != null && !last.isFull()) { // add to the last stack
last.push(v);
} else { // to build a new stack
Stack stack = new Stack(capacity);
stack.push(v);
stacks.add(stack);
}
}
public int pop() {
Stack last = getLastStack();
int v = last.pop();
if (last.size == 0) // if the last stack is empty, remove it
stacks.remove(stacks.size() - 1);
return v;
}
public int popAt(int index) {
return leftShift(index, true);
}
// pop the index of stacks
public int leftShift(int index, boolean b) {
// TODO Auto-generated method stub
Stack stack = stacks.get(index);
int item;
if(b)item = stack.pop();
else item = stack.removeButtom();
if(stack.isEmpty()){
stacks.remove(index);
}else if(stacks.size() > index + 1){
int v = leftShift(index+1, false);
stack.push(v);
}
return item;
}
}