LeetCode: Jump Game

少了一个 =, 两次过

 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 (!n) return true;

 7         int left = 0;

 8         int right = A[0];

 9         while (left <= right) {

10             if (right >= n-1) return true;

11             right = max(right, left+A[left]);

12             left++;

13         }

14         return false;

15     }

16 };

 C#

 1 public class Solution {

 2     public bool CanJump(int[] nums) {

 3         if (nums.Length == 0) return true;

 4         int left = 0;

 5         int right = nums[0];

 6         while (left <= right) {

 7             if (right >= nums.Length - 1) return true;

 8             right = Math.Max(right, left + nums[left]);

 9             left++;

10         }

11         return false;

12     }

13 }
View Code

 

你可能感兴趣的:(LeetCode)