Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Runtime: 185 ms, faster than 24.87% of Java online submissions for Jump Game.
- Memory Usage: 40.9 MB, less than 52.14% of Java online submissions for Jump Game.
- */
- class Solution {
- public boolean canJump(int[] nums) {
- int len = nums.length;
- if (len == 0) return false;
- if (len == 1) return true;
- boolean[] visited = new boolean[len - 1];
- visited[0] = true;
- for (int i = 0; i < len - 1; i++) {
- if (!visited[i]) {
- continue;
- }
- int maxReach = i + nums[i];
- if (maxReach >= len - 1 || maxReach < 0) {
- return true;
- }
- for (int k = i + 1; k <= maxReach; k++) {
- visited[k] = true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement