题目

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. python数据库连接例子

    import sqlite3 conn = sqlite3.connect('food.db') curs = conn.cursor() curs.execute(''' CREATE TABLE ...

  2. pycharm快捷键及一些常用设置(转载)

    转载于:http://blog.csdn.net/wangtong95/article/details/51100872 在PyCharm /opt/pycharm-3.4.1/help目录下可以找到 ...

  3. A - I'm bored with life

    Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university ...

  4. [POI2007]石头花园SKA

    Description Blue Mary是一个有名的石头收藏家.迄今为止,他把他的藏品全部放在他的宫殿的地窖中.现在,他想将他的藏品陈列在他的花园中.皇家花园是一个边长为1000000000单位的平 ...

  5. 暴力 BestCoder Round #46 1001 YJC tricks time

    题目传送门 /* 暴力:模拟枚举每一个时间的度数 详细解释:http://blog.csdn.net/enjoying_science/article/details/46759085 期末考结束第一 ...

  6. 题解报告:hdu1205吃糖果(插空法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1205 Problem Description HOHO,终于从Speakless手上赢走了所有的糖果, ...

  7. HttpURLConnection与HTTP Client的区别,及多用前者

    转自: http://android-developers.blogspot.jp/2011/09/androids-http-clients.html Level 9以前用client,以后用url ...

  8. Css 基本的规则写法

    样式表的写法: css的语法由一些标志构成,就是一个基本的样式表由选择器,属性和属性值构成.Css有标准的写法规则标准的css写法: h1 { Font-family:黑体;} h1:表示选择符Fon ...

  9. VS2010 好用的javascript扩展工具

    工具1) JScript Editor Extensions 折叠代码 下载地址: JScript Editor Extensions 工具2) Javascript parser 以树形方式查的代码 ...

  10. VUE 入坑系列 一 基础语法

    html代码 <div id="app"> {{message}} </div> JavaScript代码 var vm = new Vue({ el: & ...