boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - leetcode请教: time complexy
相关主题
is access to int[] faster than List?
help: 两个Java的问题
出个简单题,看你Java APi熟悉到什么程度
List, LinkedList and Vector
如何定义这样的数组?
java 的内存分布?
几个Java面试题
Java SE6 LinkedList implementation issue
Java是如何处理ArrayList和LinkedList的内存的?
a question regarding spring collection initialization
相关话题的讨论汇总
话题: node话题: integer话题: topele话题: int话题: arraylist
进入Java版参与讨论
1 (共1页)
r****4
发帖数: 168
1
我在做leetcode,这个是要求所有push, pop, top, getMin, 时间消耗都是常量。
这个是我的code,
class MinStack {
List stack = new ArrayList();
List mins = new ArrayList();
int min = 0;
public void push(int x) {

stack.add(x);
if(mins.isEmpty() || x < min) {
mins.add(stack.size());
min = x;
}
}
public void pop() {
Integer temp = mins.get(mins.size() - 1 );
if(temp == stack.size()) {
mins.remove(temp);
}
stack.remove(stack.get(stack.size() - 1));
}
public int top() {
int topEle = stack.get(stack.size() - 1);
return topEle;

}
public int getMin() {
int topEle = mins.get(mins.size() - 1 );
return stack.get(topEle - 1);
}
}
运行后几万个递增的push到minstack 后,报time limit exceed problem。我没懂我的
这个为啥会超时? 我的push操作在时间上是常量啊。
找了一个答案是用Node, 这个node里放最小值和当前值,还有前一个Node的地址。这
个答案的push是每一个新数据进来,先new 一个node,记录前一个Node地址。
我不知道为什么用Node 会没有超时的问题。
谁能解答下吗?
b******y
发帖数: 9224
2
不就是这个解决办法吗?
http://stackoverflow.com/questions/685060/design-a-stack-such-t
这个答案里没有用到单独的Node啊。我也不明白为啥用一个单独的Node。继续探讨吧。
F****n
发帖数: 3271
3
ArrayList uses an underlying array, and if it length is exceeded a new array
will be created and data copied, which takes O(N) time.
For tests and interview, you should use LinkedList.
For real world projects, I recommend use new ArrayList(N) in
practice, where N is the estimated maximal number of elements in your stack.

【在 r****4 的大作中提到】
: 我在做leetcode,这个是要求所有push, pop, top, getMin, 时间消耗都是常量。
: 这个是我的code,
: class MinStack {
: List stack = new ArrayList();
: List mins = new ArrayList();
: int min = 0;
: public void push(int x) {
:
: stack.add(x);
: if(mins.isEmpty() || x < min) {

g**e
发帖数: 6127
4
In real world linked list performance is worse than array list in almost all
use cases.

array
stack.
量。

【在 F****n 的大作中提到】
: ArrayList uses an underlying array, and if it length is exceeded a new array
: will be created and data copied, which takes O(N) time.
: For tests and interview, you should use LinkedList.
: For real world projects, I recommend use new ArrayList(N) in
: practice, where N is the estimated maximal number of elements in your stack.

F****n
发帖数: 3271
5
That's not true. There are cases where you need a million of objects with
small but variant-sized lists. In such cases you should use LinkedList
because the memory will be fragmented anyway.

all

【在 g**e 的大作中提到】
: In real world linked list performance is worse than array list in almost all
: use cases.
:
: array
: stack.
: 量。

g*****g
发帖数: 34805
6
Of course there are cases you want to use linklist, such as a queue.

all

【在 g**e 的大作中提到】
: In real world linked list performance is worse than array list in almost all
: use cases.
:
: array
: stack.
: 量。

1 (共1页)
进入Java版参与讨论
相关主题
a question regarding spring collection initialization
Generic type cast warning
再请教一个 编译错误
How to check if an element is in an array?
请教一个Queue实现的问题
问个初级的generic的问题
问一个 java generic问题
如何删除 linked list 的最后一个元素
今天碰到一个面试题
诡异问题求助
相关话题的讨论汇总
话题: node话题: integer话题: topele话题: int话题: arraylist