Leetcode55. Jump Game跳跃游戏
给定一个非负整数数组,你最初位于数组的第一个位置。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
判断你是否能够到达最后一个位置。
示例 1:
输入: [2,3,1,1,4] 输出: true 解释: 从位置 0 到 1 跳 1 步, 然后跳 3 步到达最后一个位置。
示例 2:
输入: [3,2,1,0,4] 输出: false 解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。
贪心
pos表示当前位置
class Solution {
public:
bool canJump(vector<int>& nums) {
int len = nums.size();
if(len == 1)
return true;
int pos = 0;
while(pos < len)
{
if(pos + nums[pos] >= len - 1)
return true;
int x = nums[pos];
int MAX = pos;
int MAXpos = pos;
for(int i = 1; i <= x; i++)
{
if(pos + i + nums[pos + i] >= len - 1)
return true;
else if(pos + i + nums[pos + i] > MAX)
{
MAX = pos + i + nums[pos + i];
MAXpos = pos + i;
}
}
if(pos == MAXpos)
return false;
else
pos = MAXpos;
}
return false;
}
};
升级简化版:
去掉了重复的遍历
MAX表示到目前位置能到达的最远距离
class Solution {
public:
bool canJump(vector<int>& nums)
{
int MAX = 0;
int len = nums.size();
for(int i = 0; i < len; i++)
{
if(i <= MAX && i + nums[i] > MAX)
MAX = i + nums[i];
}
return MAX >= len - 1;
}
};
Leetcode55. Jump Game跳跃游戏的更多相关文章
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode每天一题】Jump Game(跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 055 Jump Game 跳跃游戏
给定一个非负整数数组,您最初位于数组的第一个索引处.数组中的每个元素表示您在该位置的最大跳跃长度.确定是否能够到达最后一个索引.示例:A = [2,3,1,1,4],返回 true.A = [3,2, ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode(45): 跳跃游戏 II
Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- 【LeetCode每天一题】Jump Game II(跳跃游戏II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 跳跃游戏 12 · Jump Game 12
跳跃游戏 1 [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: 由于要用itera ...
- lintcode: 跳跃游戏 II
跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A = ...
随机推荐
- echarts高级
常用,待续... ♣tooltip自动轮播 ♣ 实现数据自动轮播 原理:其实就是timeline,获取某几段(时间)的数据,然后隐藏timeline ♣ legend自动轮播 ♣ 左侧多字出省略号 f ...
- (转)第03节:在Canvas上插入图片并设置旋转属性
我们已经学会了在Canvas上画简单的图形,这节我们就在Canvas上加一张图片.用到fabric.Image对象把图片添加到Canvas上. HTML文件:为了效果更好看我在html里写了一些CSS ...
- Android之RelativeLayout相对布局
1.相关术语解释 1.基本属性 gravity :设置容器内组件的对齐方式 ignoreGravity : 设置该属性为true的组件,将不受gravity属性的影响 2.根据父容器定位 layout ...
- vue-cli 构建项目
1.安装vue-cli和webpack npm install webpack -g npm install vue-cli -g 2.vue-cli初始化项目 vue init webpack-si ...
- 分布式配置中心(Spring Cloud Config)
真有意思的一个问题,我先把我遇到的写一次 ,今天学习Spring Cloud Config 新建了三个module ,eureka-server,config-server,config-clien ...
- Jqgrid 序号列宽度调整
// 遍历jqgrid 使其序号列宽度为45 function setwidth() { $("table[role='grid']").each(function () {//j ...
- 几种远程调用接口协议简单比较和web service(SOAP)与HTTP接口的区别:
什么是web service? 答:soap请求是HTTP POST的一个专用版本,遵循一种特殊的xml消息格式Content-type设置为: text/xml任何数据都可以xml化. ...
- CAS企业级单点登录原理
https://blog.csdn.net/anumbrella/article/details/80821486 1. 单点登录概述 1.1. 什么是单点登录? 单点登录:Single Sign O ...
- 新浪新闻API
新浪新闻API ustcmio 关注 2017.01.15 20:44* 字数 536 阅读 2479评论 2喜欢 7 新浪新闻的API:1.访问手机新浪网https://sina.cn/?from= ...
- hdu1527
hdu1527两堆博弈模型,威佐夫博弈 #include<iostream> #include<cstdio> #include<queue> #include&l ...