题目

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. SpringBoot入门-15(springboot配置freemarker使用YML)

    https://blog.csdn.net/fengsi2009/article/details/78879924 application.yml spring: http: encoding: fo ...

  2. UCOSII学习 - 创建任务

    本人刚刚学习UCOSII,平台为正点原子的STM32F103战舰开发板,写这篇博客主要是为了学习UCOSII,也方便自己能够一点一点的进步,话不多说直入正题吧. 第一步:在STM32上移植好UCOSI ...

  3. java数组实现买彩票(阿基老师的打乱排序思想)

    package com.wh.array; public class Lottery { public static void main(String[] args) { int[] num=new ...

  4. windows下Python的安装,以及IDLE的使用

    一.Python的下载安装 (1)python的windows安装包可以从https://www.python.org 网址中下载,可以下载3.4版本的或者2.7版本的.(2)下载后直接运行即可.然后 ...

  5. Rocketmq Broker启动网卡顺序问题

    方法一.修改网卡名称,因为网卡顺序是通过名称排列的 方法二.指定broker使用IP echo "brokerIP1=192.168.1.220" > conf/broker ...

  6. 【转】Java中,&&与&,||与|的区别

    转自:http://blog.csdn.net/lishiyuzuji/article/details/8116516 在Java的逻辑运算符中,有这么四类:&&(短路与),& ...

  7. PHP使用Session遇到的一个Permission denied Notice解决办法

    搜索 session.save_path 在这里你有两个选择,一个是像我一样用; 把这一行注释掉,另一个选择就是修改一个 nobody 用户可以操作的目录,也就是说有读写权限的目录,我也查了下这个默认 ...

  8. SSM学习

    一.https://www.cnblogs.com/zyw-205520/p/4771253.html 二.https://blog.csdn.net/dwhdome/article/details/ ...

  9. APP崩溃处理

    以前经常遇到APP内部异常情况下的Exception,最初是通过try catch这样的方式处理:但是APP上线后,用户在特地的情况下触发 了某些Exception,当然这些Exception从理论和 ...

  10. vue1.0生命周期

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...