LeetCode 55. How to Solve Jump Game Problems

Source: Internet
Author: User

LeetCode 55. How to Solve Jump Game Problems
Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A = [2, 3, 1, 4], return true.

A = [3, 2, 1, 0, 4], return false.
Idea: This question is zero more than that of jump Game II, and may not reach the final point. The Code uses the greedy idea and returns false if it can only go 0. The Code is as follows:

Public class Solution {public boolean canJump (int [] nums) {if (nums. length = 0) return false; int I = 0; // determines whether there is any 0. If there is no 0, the while (I <nums. length) {if (nums [I] = 0) {break;} I ++;} // No 0, certainly can reach if (I = nums. length) {return true;} I = 0; while (I <nums. length) {if (I + nums [I]> = nums. length-1) return true; if (nums [I] = 0) return false; int max = 0; int index = 0; // The maximum step for the next step (int j = I + 1; j-I <= nums [I]; j ++) {if (max <j-I + nums [j]) {max = j-I + nums [j]; index = j ;}} // go to the next index I = index;} return true ;}}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.