[关闭]
@Lin-- 2020-02-24T14:01:30.000000Z 字数 509 阅读 339

canJump

Leetcode

依据题意:跳步最大值为该当前数组值,若能跳出。返回true,跳不出,返回false。
解题思路:贪心算法,当前能到达最大下标记录为temp,遍历nums[i]+i,若出现temp>numsSize,则说明能跳出去。否则跳不出去。

  1. /*
  2. *File:canJump.c:
  3. *Author:0HP
  4. *Date:20200224
  5. *Purpose:to solve the problem in Leetcode
  6. *https://leetcode.com/problems/jump-game/
  7. */
  8. #include<stdio.h>
  9. #include<stdlib.h>
  10. #include<stdbool.h>
  11. bool canJump(int* nums, int numsSize)
  12. {
  13. if(numsSize==0)return false;
  14. if(numsSize==1)return true;
  15. int temp=0;
  16. for(int i=0;i<=temp;++i)
  17. {
  18. if(temp<nums[i]+i)
  19. {temp=nums[i]+i;}
  20. if(temp>=(numsSize-1)){return true;}
  21. }
  22. return false;
  23. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注