public Stack(int s){ maxSize=s; stackArray=new char[maxSize]; top=-1; }
public void push(char j){ if(top==maxSize) stackArray=new char[2*maxSize]; stackArray[++top]=j; }
public char pop(){ return stackArray[top--]; }
import java.util.*; public class IntStack { private int size; private int[] elements; private int top = -1; public IntStack(int size) { if (size < 0 ) throw new NegativeArraySizeException(); this.size = size; elements = new int[size]; } public void push(int value) { if (++top >= size) { size *= 2; int[] oldData = elements; elements = Arrays.copyOf(oldData, size); } elements[top] = value; } public int pop() { if (top < 0) throw new IndexOutOfBoundsException(); return elements[top--]; } } O(1)的方法我没想到怎么实现比较好
import java.util.*;class StackEmptyException extends RuntimeException{}; class NegativeStackSizeException extends RuntimeException{};public class IntStack { private int size; private int[] elements; private TreeMap<Integer,Integer> values = new TreeMap<Integer,Integer>(); private int top = -1; public IntStack(int size) { if (size < 0 ) throw new NegativeArraySizeException(); this.size = size; elements = new int[size]; } public void push(int value) { if (++top >= size) { size *= 2; int[] oldData = elements; elements = Arrays.copyOf(oldData, size); } Integer freq = values.get(value); freq = ( freq == null) ? 1 : freq + 1; values.put(value, freq); elements[top] = value; } public int pop() { if (top < 0) throw new StackEmptyException(); int result = elements[top--]; int freq = values.get(result); if (freq == 1) values.remove(result); else values.put(result,freq - 1); return result; } public int getMax() { if (values.isEmpty()) throw new StackEmptyException(); return values.lastKey(); } }
maxSize=s;
stackArray=new char[maxSize];
top=-1;
}
public void push(char j){
if(top==maxSize)
stackArray=new char[2*maxSize];
stackArray[++top]=j;
}
public char pop(){
return stackArray[top--];
}
import java.util.*;
public class IntStack
{
private int size;
private int[] elements;
private int top = -1; public IntStack(int size)
{
if (size < 0 ) throw new NegativeArraySizeException();
this.size = size;
elements = new int[size];
} public void push(int value)
{
if (++top >= size)
{
size *= 2;
int[] oldData = elements;
elements = Arrays.copyOf(oldData, size);
}
elements[top] = value;
} public int pop()
{
if (top < 0) throw new IndexOutOfBoundsException(); return elements[top--];
}
}
O(1)的方法我没想到怎么实现比较好
import java.util.*;class StackEmptyException extends RuntimeException{};
class NegativeStackSizeException extends RuntimeException{};public class IntStack
{
private int size;
private int[] elements;
private TreeMap<Integer,Integer> values = new TreeMap<Integer,Integer>();
private int top = -1; public IntStack(int size)
{
if (size < 0 ) throw new NegativeArraySizeException();
this.size = size;
elements = new int[size];
} public void push(int value)
{
if (++top >= size)
{
size *= 2;
int[] oldData = elements;
elements = Arrays.copyOf(oldData, size);
}
Integer freq = values.get(value);
freq = ( freq == null) ? 1 : freq + 1;
values.put(value, freq);
elements[top] = value;
} public int pop()
{
if (top < 0) throw new StackEmptyException();
int result = elements[top--];
int freq = values.get(result);
if (freq == 1) values.remove(result);
else values.put(result,freq - 1);
return result;
} public int getMax()
{
if (values.isEmpty()) throw new StackEmptyException();
return values.lastKey();
}
}