LeetCode(55)Jump Game
题目
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;
}
};
LeetCode(55)Jump Game的更多相关文章
- LeetCode (45) Jump Game II
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- LeetCode(55): 跳跃游戏
Medium! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...
- LeetCode(一) jump game
一. 1. #include<iostream> #include<cmath> using namespace std; bool CanJump(int n[],int n ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- 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 ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 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 ...
随机推荐
- 上帝造题的七分钟2/花神游历各国/GSS4 线段树维护区间开方 By cellur925
题目传送门 或者 另一个传送门 询问区间和都好说.但是开方?? 其实是这样的,一个数(1e9)以内连续开方6次就会变成1,于是我们就可在开方操作上进行暴力修改.暴力修改的意思其实也就是找到叶子节点进行 ...
- 初学Linux应该注意的事项
相比于windows linux严格区分大小写 linux所有内容都是以文件形式保存 linux不靠扩展名区分文件类型(靠权限),linux下文件扩展名主要是方便管理员分类 linux所有的存储设备都 ...
- python系列1_travel
Python__copy copy模块用于对象的拷贝操作.该模块只提供了两个主要的方法:copy.copy与copy.deepcopy,分别表示浅复制与深复制. 浅拷贝(copy):拷贝父对象,不会拷 ...
- logstsh | logstash-input-jdbc 启动错误收集
1: Failed to execute action {:action=>LogStash::PipelineAction::Create/pipeline_id:main, :excepti ...
- 数据结构 - 静态顺序线性表的实行(C语言)
数据结构 - 静态顺序线性表的实行(C语言) 1 获取元素操作 对于线性表的顺序存储结构来说,如果我们要实现GetElem操作,即将线性表L中的第i个位置元素值返回,其实是非常简单的. 只要i的数值在 ...
- LoadRunner_11破解教程完整版
2017.12.17更正 qtm的LR11,如果是win10版本的电脑而且ie浏览器是11以上的请到loadrunner官网下载社区免费版,支持google,firefox,edge,ie11四大浏览 ...
- 博弈 HDOJ 4371 Alice and Bob
题目传送门 题意:Alice和 Bob轮流写数字,假设第 i 次的数字是S[i] ,那么第 i+1 次的数字 S[i+1] = S[i] + d[k] 或 S[i] - d[k],条件是 S[i+1] ...
- JMeter(十一)内存溢出解决方法
使用jmeter进行压力测试时遇到一段时间后报内存溢出outfmenmory错误,导致jmeter卡死了,先尝试在jmeter.bat中增加了JVM_ARGS="-Xmx2048m -Xms ...
- .Net应用自定义鼠标样式
(调用系统API的方法) 1.引用命名空间 using System.Runtime.InteropServices; 命名空间提供各种各样支持 COM 互操作 及平台调用服务的成员.using Sy ...
- 使用一个CSS Class去给标签定义Style
使用一个CSS Class去给标签定义Style 类是可重用的样式,可以添加到HTML元素. 下面是一个CSS类声明的例子: <style> .blue-text { colo ...