leetcode 提交问题出错
来源:3-4 关于Leetcode的更多说明
慕斯0370139
2018-05-14
做leetcode的第232问题,用栈实现队列,点击执行代码,提示运行通过,点击提交解答报错
代码如下
import java.util.Stack;
class MyQueue<T> {
private Stack<T> stack;
/** Initialize your data structure here. */
public MyQueue() {
stack = new Stack<>();
}
/** Push element x to the back of queue. */
public void push(T e) {
stack.push(e);
}
/** Removes the element from in front of queue and returns that element. */
public T pop() {
return stack.pop();
}
/** Get the front element. */
public T peek() {
return stack.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack.empty();
}
// public void add(int index, T e) {
// if (index < 0 || index > size) {
// throw new IllegalArgumentException("index must > 0 || index < size");
// }
// if (size == myStack.length) {
// throw new IllegalArgumentException("Stack is Full");
// }
// for (int i = size - 1; i >= index; i--) {
// myStack[i + 1] = myStack[i];
// }
// myStack[index] = e;
// size++;
// }
// public T remove(int index) {
// if (index < 0 || index > size) {
// throw new IllegalArgumentException("index must > 0 || index < size");
// }
// T ret = myStack[index];
// for (int i = index + 1; i < size; i++) {
// myStack[i - 1] = myStack[i];
// }
// size --;
// return ret;
// }
// public T get(int index) {
// if (index < 0 || index > size) {
// throw new IllegalArgumentException("index must > 0 || index < size");
// }
// return myStack[index];
// }
// public T getFirst() {
// return get(0);
// }
// public T getLast() {
// return get(size - 1);
// }
// public T removeFirst() {
// return remove(0);
// }
// public T removeLast() {
// return remove(size - 1);
// }
}
这是用java自带的stack实现的结果
以下是自己实现的栈,然后运行,报同样的错
class MyQueue<T> {
private T[] myStack;
private int size;
/** Initialize your data structure here. */
public MyQueue() {
myStack = (T[])new Object[10];
size = 0;
}
/** Push element x to the back of queue. */
public void push(T e) {
add(size, e);
}
/** Removes the element from in front of queue and returns that element. */
public T pop() {
return removeLast();
}
/** Get the front element. */
public T peek() {
return getLast();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return size == 0;
}
public void add(int index, T e) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("index must > 0 || index < size");
}
if (size == myStack.length) {
throw new IllegalArgumentException("Stack is Full");
}
for (int i = size - 1; i >= index; i--) {
myStack[i + 1] = myStack[i];
}
myStack[index] = e;
size++;
}
public T remove(int index) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("index must > 0 || index < size");
}
T ret = myStack[index];
for (int i = index + 1; i < size; i++) {
myStack[i - 1] = myStack[i];
}
size --;
return ret;
}
public T get(int index) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("index must > 0 || index < size");
}
return myStack[index];
}
public T getFirst() {
return get(0);
}
public T getLast() {
return get(size - 1);
}
public T removeFirst() {
return remove(0);
}
public T removeLast() {
return remove(size - 1);
}
}
报错都是
Exception in thread "main" java.lang.RuntimeException: Object type [class java.lang.Integer] not yet implemented. at __Serializer__.serialize(Unknown Source) at __Driver__.__helperSelectMethod__(__Driver__.java:19) at __Driver__.__helper__(__Driver__.java:39) at __Driver__.main(__Driver__.java:84)
很是不解
1回答
-
题目中给出的类模板没有泛型,自己实现的时候不能添加这个泛型哦。由于Leetcode中的测试main函数不是自己写的,而是系统写定的,所以只会按照题目模板的类定义方式进行初始化,不会做泛型类的初始化的:)
012018-05-15
相似问题