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 = ...
随机推荐
- gatekeeper学习概述
1.概述 该产品部署在网络隔离装置两端,以代理程序的身份,完成两侧设备连接维护,数据转发的功能.场景简化如图所示: 软件核心是一个基于Netty的网络应用程序,考虑到系统的可维可测性,集成了web化的 ...
- eclipse memory analyzer对系统内存溢出堆文件解析0(转)
前言 在平时工作过程中,有时会遇到OutOfMemoryError,我们知道遇到Error一般表明程序存在着严重问题,可能是灾难性的.所以找出是什么原因造成OutOfMemoryError非常重要.现 ...
- C# 读写 Photoshop PSD文件 操作类
分析了PSD的文件....才发现PSD的RGB色彩保存也是 先红 蓝 绿 这样保存的 ....麻烦的.. 另外8BIM好象没什么用..可以直接跳过..直接获取最后的图形信息就可以了.. 我只对一些PS ...
- kafka comsumer
kafka的顺序消费只保证在同一个partition中而已
- vue:element-ui输入框绑定回车事件
参考: https://segmentfault.com/q/1010000011347642 https://weiku.co/article/297/ vue监听input输入框的回车事件:key ...
- SVN 环境搭建
安装配置 安装环境 #查看系统版本环境 [root@svn ~]# cat /etc/redhat-release CentOS release 6.7 (Final) [root@svn ~]# u ...
- 安装postgresql11.5
root身份安装 创建用户 编译安装成功后,接下来要做的就是创建一个普通用户,因为默认超级用户(root)不能启动postgresql,所以需要创建一个普通用户来启动数据库,执行以下命令创建用户: [ ...
- ROS节点的初始化及退出详解(ros::init、SIGINT、ros::ok、ros::NodeHandle
https://haoqchen.site/2018/04/28/ROS-node-init/ #include "ros/ros.h" #include <signal.h ...
- Ionic JPush极光推送二
1.看图解决问题 2.解决出现统计代码提示问题 修改这个java 文件 导入命名空间 import cn.jpush.android.api.JPushInterface; 添加方法 @Overr ...
- 编写一个函数isMerge,判断一个字符串str是否可以由其他两个字符串part1和part2“组合”而成
编写一个函数isMerge,判断一个字符串str是否可以由其他两个字符串part1和part2“组合”而成.“组合 ”的规则如下: 1). str中的每个字母要么来自于part1,要么来自于part2 ...