题目

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. Robot Framework问题汇总...不断更新中

    在实际使用Robot Framework工具过程中,难免会遇到一些问题, 我们将会一一记录下来,以便后来者碰到类似的问题能够快速解决! 安装类问题: ========================= ...

  2. http缓存之lastModified和etag

    1.cache-control 访问资源 首次访问页面时间:2018.2.1  9:56  (当前时间=GMT时间+8h) 缓存时长max-age:1 day Expire缓存失效时间:2018.2. ...

  3. 设置UITableViewCell 选中时的背景颜色

    自定义Cell如图 一个View上面放了四个Label 分别连线到.m文件中 @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @pro ...

  4. 转-MAC 下安装PHONEGAP开发环境

    来自:http://jinzhe.net/post/8.html 什么是Phonegap呢?Phonegap是一个利用HTML5去开发App的框架.可以为安卓.iOS.WP.黑莓.火狐等移动操作系统. ...

  5. D. Dasha and Very Difficult Problem 二分

    http://codeforces.com/contest/761/problem/D c[i] = b[i] - a[i],而且b[]和a[]都属于[L, R] 现在给出a[i]原数组和c[i]的相 ...

  6. AJPFX总结final、finally、finallize的区别

    final.finally.finallize有何区别?    final表示一个修饰符,如果用它来修饰一个类,则该类是不能继承的:如果用它来修饰一个变量,则该变量一旦赋值之后就不能再修改:如果用它来 ...

  7. 后缀数组 (Suffix Array) 学习笔记

    \(\\\) 定义 介绍一些写法和数组的含义,首先要知道 字典序 . \(len\):字符串长度 \(s\):字符串数组,我们的字符串存储在 \(s[0]...s[len-1]\) 中. \(suff ...

  8. 6 Specialzed layers 特殊层 第一部分 读书笔记

    6 Specialzed layers 特殊层  第一部分  读书笔记   Specialization is a feature of every complex organization. 专注是 ...

  9. linux命令规范

    Linux文件后缀: 系统文件:*.conf    *.rpm 系统与脚本:*.c  *.php 存档文件和压缩文件:*.tar   *.gz ……… Linux文件命名规则: 1.大小写敏感 2.除 ...

  10. 乐视max2 刷入第三方recovery 然后刷入root 包 root

    乐视max2 刷入第三方recovery 然后刷入root 包 root 第三方recovery:为奇兔 刷入root 包 https://share.weiyun.com/ddcdd5ea83956 ...