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.

 1 class Solution {
 2 public:
 3     bool canJump(int A[], int n) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         if(1==n) return true;
 7         if(0==n) return false;
 8         
 9         int position = 0;
10         
11         while(position != (n-1)){
12             if(0 == A[position])
13                 return false;
14                 
15             position += A[position];
16             if(position >=n)
17                 return true;
18         }
19         
20         return true;
21     }
22 };
我的答案

相关文章:

  • 2022-02-19
  • 2021-12-15
  • 2022-12-23
  • 2021-07-18
  • 2021-11-21
  • 2021-08-03
  • 2021-05-29
  • 2021-10-20
猜你喜欢
  • 2021-09-24
  • 2021-08-23
  • 2021-08-18
  • 2021-07-22
  • 2021-07-06
  • 2022-01-01
  • 2021-10-31
相关资源
相似解决方案