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 = ...
随机推荐
- VS2010-MFC(对话框:创建对话框类和添加控件变量)
转自:http://www.jizhuomi.com/software/153.html 前两讲中讲解了如何创建对话框资源.创建好对话框资源后要做的就是生成对话框类了.生成对话框类主要包括新建对话框类 ...
- 从xmlns的作用说起
查了资料和自己实践后,得出了一些关于xml和xmlns的结论 看一个最常见的javaweb 中xml配置文件的开头: <?xml version="1.0" encoding ...
- css 渐变背景
background: linear-gradient(left,#fa7f6d, #fc5e7f); left: 从左边开始
- 解决Navicat 报错:1130-host is not allowed MySQL不允许从远程访问的方法
ERROR 1130: Host '192.168.1.3' is not allowed to connect to thisMySQL server 解决方法: 1. 改表法.可能是你的帐号不允许 ...
- fwt优化+树形DP HDU 5909
//fwt优化+树形DP HDU 5909 //见官方题解 // BestCoder Round #88 http://bestcoder.hdu.edu.cn/ #include <bits/ ...
- [Ceoi2011]Traffic
#2387. [Ceoi2011]Traffic Online Judge:Bzoj-2387,Luogu-4700 Label:Yy,Tarjan缩点,dfs 题目描述 格丁尼亚的中心位于Kacza ...
- leetcode 75 Sorted Colors
两种解法 1)记录0和1的个数 然后按照记录的个数将0和1重新放入原数组,剩下的补2 2)双指针left,right left表示0~left-1都为0,即i之前都为0 right表示right+1~ ...
- Mysql8+mybatisGenerator (mysql 8的逆向工程)
最近试了一下mysql8的逆向工程工具 1.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOC ...
- sql join 的一次小使用
表为: 列名:站号,模式名,偏差,日期,要素 试图查询每个站中最小的那个偏差的模式名 create table B as SELECT stationid,min(abserror) as minab ...
- 我认为最节省时间的CSS命名规范
CSS命名规范一 js中对变量的命名最好使用camel case驼峰式命名法,但CSS中更适用于red-box命名规范. CSS命名规范二 BEM命名规范 B=>block E=>elem ...