Jump Game 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/jump-game/description/


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

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

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

Solution

class Solution {
private:
int max(int a, int b) {
return a > b ? a : b;
}
public:
bool canJump(vector<int>& nums) {
int size = nums.size();
if (size == 0)
return false;
if (size == 1)
return true;
if (nums[0] >= size - 1)
return true; int* farthestPoint = new int[size];
farthestPoint[0] = nums[0];
for (int i = 1; i < size; i++) {
if (i <= farthestPoint[i - 1]) {
farthestPoint[i] = max(farthestPoint[i - 1], i + nums[i]);
} else {
delete []farthestPoint;
return false;
}
}
bool result = farthestPoint[size - 2] >= size - 1;
delete [] farthestPoint;
return result; }
};

解题描述

这道题我使用的是贪心算法,通过遍历nums来更新每一个点上能够达到的最远点,最终判断是否能够达到终点。

最新文章

  1. 使用Ldoc给Lua生成文档
  2. 今天心情好,一起探讨下《送给大家的200兆SVN代码服务器》怎么管理我们的VS代码?
  3. Android-调用优酷SDK上传视频
  4. ExtJs4学习MVC中的Store
  5. hdu 4674 Trip Advisor(缩点+倍增lca)
  6. nodejs-fs使用
  7. HDOJ 1098 Ignatius&#39;s puzzle
  8. python 基础篇(一)--linux命令篇
  9. eclipse 使用hadoop-plugins 插件出现EOFException问题
  10. twemproxyMemcache协议解析探索——剖析twemproxy代码正编补充
  11. .NET开发微信小程序-生成二维码
  12. python2和python3共存时,设置默认python为python3
  13. XFS文件系统的备份和恢复
  14. \r\n 回车换行浅析
  15. Material Designer的低版本兼容实现(三)——Color
  16. amin例子的简单研究
  17. jdbcTemplate in
  18. linux下高并发网络应用注意事项
  19. 关于那些oj链接
  20. python稀疏矩阵得到每列最大k项的值,对list内为类对象的排序(scipy.sparse.csr.csr_matrix)

热门文章

  1. Linux-Shell脚本编程-学习-3-Shell编程-shell脚本基本格式
  2. selenium自动化登录qq网页
  3. CCF-NOIP-2018 提高组(复赛) 模拟试题(七)
  4. 1107 Social Clusters (30 分)(并查集)
  5. AGV小车典型设计算法及应用
  6. LeetCode 3——无重复字符的最长子串
  7. BZOJ 4004 JLOI2015 装备购买 高斯消元+线性基
  8. DPDK Qos之报文处理流水线
  9. Linux中安装apache
  10. [C/C++] C++常见面试题