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;
}
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
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;
}