题目

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.

分析

该题目考察的贪心算法的应用。从第一步开始计算可以前进的最大步长,每走一步比较更新该值,始终保持当前位置的时候可前进步长最大。到达最终位置前,若出现步长<= 0的情况,说明失败。

AC代码

#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm> using namespace std; //贪心算法
class Solution {
public:
bool canJump(vector<int>& nums) {
if (nums.empty())
return false; int maxStep = nums[0];
int len = nums.size(); for (int i = 1; i < len; ++i)
{
if (maxStep <= 0)
return false;
else{
maxStep = max(--maxStep, nums[i]);
}//else
}//for return true;
}
};

GitHub测试程序源码

LeetCode(55)Jump Game的更多相关文章

  1. LeetCode (45) Jump Game II

    题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...

  2. LeetCode(55): 跳跃游戏

    Medium! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...

  3. LeetCode(一) jump game

    一. 1. #include<iostream> #include<cmath> using namespace std; bool CanJump(int n[],int n ...

  4. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  5. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  6. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  7. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  8. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  9. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

随机推荐

  1. Oracle - RMAN备份 之 incarnation的实验和小结

    对于RMAN恢复我一直都不是很明白,因为,老是搞不清楚不完全恢复该怎么进行,今天,通过这个实验即是对不完全恢复的实践,也是希望搞清楚incarnation到底是怎么工作的.很可惜,本人对Oracle的 ...

  2. php安装的扩展php -m可以看到,但是phpinfo()看不到,php-fpm关闭了重新打开还是不行?

    问答 问答详情   php安装的扩展php -m可以看到,但是phpinfo()看不到,php-fpm关闭了重新打开还是不行? centos apache linux html php 3.2k 次浏 ...

  3. CMake学习笔记二:cmake 常用变量和常用环境变量

    1 cmake 变量引用的方式 使用 ${} 进行变量的引用.在 IF 等语句中,是直接使用变量名而不通过 ${} 取值. 2 cmake 自定义变量的方式 主要有隐式定义和显式定义两种,举一个隐式定 ...

  4. NOI2018D2T1 屠龙勇士

    安利一下松松松的OJ: 传送门 Description: ​ 有N条巨龙, 对于每个龙含有\(a_i\)的生命, 你有N + M把砍刀, 其中M把是直接给你的, N把是杀死对应的巨龙才能获得的, 每把 ...

  5. 题解报告:hdu 2072 单词数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 题目: Problem Description lily的好朋友xiaoou333最近很空,他想 ...

  6. Retinex系列之McCann99 Retinex 分类: 图像处理 Matlab 2014-12-03 11:27 585人阅读 评论(0) 收藏

    一.McCann99 Retinex McCann99利用金字塔模型建立对图像的多分辨率描述,自顶向下逐层迭代,提高增强效率.对输入图像的长宽有 严格的限制,要求可表示成 ,且 ,. 上述限制来源于金 ...

  7. 加密解密(4)SSL协议及HTTPS握手过程

    SSL协议 简介 SSL (Secure Sockets Layer 安全套接层)是一个安全协议,它提供使用 TCP/IP 的通信应用程序间的隐私与完整性.因特网的 超文本传输协议 (HTTP)使用 ...

  8. 关于c头文件的使用的小记录

    在用visual studio实现数据结构上的一些结构与算法的时候,想在一个工程中建立几个文件,然后主文件可以使用其他文件的函数与变量(比如定义的结构体还有数据结构接口函数).  我们可以利用头文件来 ...

  9. SpringIOC学习_属性注入(依赖注入)

    一.应用场景:Spring会帮创建实现类的实例,但是有时候我们还需要在类中设置一些属性用于传入设置值,这些跟类紧密关联的属性就叫依赖,通过spring帮忙设置的过程叫依赖注入. 二.依赖注入的实现 A ...

  10. rabbitmq在storm中使用

    storm中只能进行任务计算,不能保存中间结果,最后结果. 这就有一个需求,保存计算结果,最好还是分布式的,因为storm也是分布式,大数据计算. 流行的分布式计算中使用队列保存数据居多. kafka ...