Redian新闻
>
LeetCode Jump Game [Runtime Error]
avatar
LeetCode Jump Game [Runtime Error]# JobHunting - 待字闺中
b*6
1
神奇的是
Old OJ 大集合通过
new OJ Runtime Error Last executed input: [0,2,3]
百思不得其解, code 如下。
public class Elem {
int index;
int step;
}
public boolean canJump(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
int len = A.length;

if (len == 0) return false;
if (len == 1) return true;
if (len == 2) return A[0] != 0;

Stack s = new Stack();

Elem tail = new Elem();
tail.index = len -2;
tail.step = A[len-2]==0?0:1;
s.push(tail);
for (int i = len -3; i >=0 ; i--) {

int last = A[i];
while (!s.isEmpty()){

if (i+A[i]>=len-1) {
s.pop();
} else if (i+A[i]>=s.peek().index){
last = s.peek().step;
s.pop();
} else {
break;
}
}
Elem curr = new Elem();
curr.index = i;
curr.step = last;
s.push(curr);
}
return s.peek().step != 0;
}
avatar
z*1
2
你确定这事O(N)的算法?

【在 b********6 的大作中提到】
: 神奇的是
: Old OJ 大集合通过
: new OJ Runtime Error Last executed input: [0,2,3]
: 百思不得其解, code 如下。
: public class Elem {
: int index;
: int step;
: }
: public boolean canJump(int[] A) {
: // Start typing your Java solution below

avatar
b*m
3
新的oj不支持内部类,运行时没把内部里放在classpath里。

【在 b********6 的大作中提到】
: 神奇的是
: Old OJ 大集合通过
: new OJ Runtime Error Last executed input: [0,2,3]
: 百思不得其解, code 如下。
: public class Elem {
: int index;
: int step;
: }
: public boolean canJump(int[] A) {
: // Start typing your Java solution below

avatar
b*6
4
是的。
每个元素进出栈最多一次。

【在 z******1 的大作中提到】
: 你确定这事O(N)的算法?
avatar
s*5
5
为什么会有人总喜欢写又臭又长的code?C++不到10行的事。
bool canJump(int A[], int n) {
if(n <= 1) return n == 1;
int las = A[0], i = 1;
if(las >= n-1) return true;
while(i < n) {
if(i > las) return false;
las = max(las, i+A[i]);
if(las >= n-1) return true;
i++;
}
return true;
}
avatar
j*r
6
bool canJump(int A[], int n) {
int reach = A[0];
int i = 0;
while(i <= reach) {
reach = max(reach, A[i]+i);
if (i >= n-1) return true;
i++;
}
return false;
}

【在 s***5 的大作中提到】
: 为什么会有人总喜欢写又臭又长的code?C++不到10行的事。
: bool canJump(int A[], int n) {
: if(n <= 1) return n == 1;
: int las = A[0], i = 1;
: if(las >= n-1) return true;
: while(i < n) {
: if(i > las) return false;
: las = max(las, i+A[i]);
: if(las >= n-1) return true;
: i++;

avatar
s*5
7
你贴出来是神马意思?给个n=0你就crash了。

【在 j********r 的大作中提到】
: bool canJump(int A[], int n) {
: int reach = A[0];
: int i = 0;
: while(i <= reach) {
: reach = max(reach, A[i]+i);
: if (i >= n-1) return true;
: i++;
: }
: return false;
: }

avatar
j*r
8
haha, good catch, 目的达到了。 谢谢你指出程序(太久,忘了是不是我的了)中的
bug。

【在 s***5 的大作中提到】
: 你贴出来是神马意思?给个n=0你就crash了。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。