Jump Game <leetcode>

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,1,4], return true.

A = [3,2,1,0,4], return false.

算法:首先看能一次性到达最后一点的点,比如2,3,1,1,4,  能到达A[4]的是A[1],A[3],此时A[1]肯定也能到达A[1]后面所有的点,所以把A[1],当做关键点,向前知道到达第一个节点。也可以把A[3]当做关键点,因为当数据很多时,找右侧的第一个点比较容易,找左边第一个点比较费时。两个代码如下:

 1 //  52ms
 2
 3
 4 class Solution {
 5 public:
 6     bool canJump(int A[], int n) {
 7         if(n==1)  return true;
 8         else
 9         {
10             for(int i=0;i<=n-2;i++)
11             {
12                 if(A[i]+i>=n-1)
13                 {
14                     return canJump(A,i+1);
15                 }
16             }
17             return false;
18         }
19     }
20 };
 1 //44ms
 2
 3
 4 class Solution {
 5 public:
 6     bool canJump(int A[], int n) {
 7         if(n==1)  return true;
 8         else
 9         {
10             for(int i=n-2;i>=0;i--)
11             {
12                 if(A[i]+i>=n-1)
13                 {
14                     return canJump(A,i+1);
15                 }
16             }
17             return false;
18         }
19     }
20 };
时间: 2024-10-12 22:08:30

Jump Game <leetcode>的相关文章

[LeetCode]55.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:

Frog Jump

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord

LeetCode45 Jump Game II

题目: 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. Your goal is to reach the last index in the minimum number of j

55. 45. Jump Game II *HARD*

1. 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

LeetCode OJ: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 = 

LeetCode——Jump Game

Description: 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

leetcode - Jump Game II

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. Your goal is to reach the last index in the minimum number of jumps

【Jump Game II 】cpp

题目: 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. Your goal is to reach the last index in the minimum number of j

Jump Game

https://leetcode.com/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

jump display

查找了数据库,再在while里拼接成json是很麻烦的,所以,jump display 获得数组 <?php header("Content-Type:text/html; charset=utf-8"); /*json接口测试*/ $g=$_GET['g']; $v=$_GET['v']; $msu=$_GET['msu']; print_r ($g); print_r ($v); print_r ($msu); /*json*接口测试*/ //进行my数据库连接 $con =