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 = ...
随机推荐
- 笔试之const问题
1 . ; int *j=(int *)&i; *j=; cout<<i<<*j<<endl; 答案i为0,*j为1. 2. char * const p= ...
- SPSS输出结果统计表与统计图的专业性编辑及三线表定制格式
SPSS输出结果统计表与统计图的专业性编辑及三线表定制格式 世界前三统计软件,SPSS最容易学习,但SPSS默认输出的统计表与统计图美观度与专业度不够好,离发表水平尚有距离,本期咱们就谈谈SPSS图表 ...
- SpringBoot学习笔记(三):SpringBoot集成Mybatis、SpringBoot事务管理、SpringBoot多数据源
SpringBoot集成Mybatis 第一步我们需要在pom.xml里面引入mybatis相关的jar包 <dependency> <groupId>org.mybatis. ...
- jquery 判断当前设备是PC端还是移动端
$(function(){ var system = { win: false, mac: false, xll: false, ipad:false }; //检测平台 var p = naviga ...
- 表单下拉框select
<!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...
- <jsp:forward page=""></jsp:forward>标签失效异常
解决方案:在web.xml <filter-mapping> <filter-name>struts2</filter-name> ...
- Activiti流程图部署及流程图部分操作
流程图部署有两种方式,一种是通过classpath,另一种是通过zip文件 通过classpath方式如下 public void deploymentProcessDefinition_classp ...
- JavaWEB过滤器和监听器技术
过滤器介绍 什么是过滤器 生活中的例子: 滤水器,口罩,杯子上滤网,渔网 生活中的过滤器:留下我们想要的,排除,我们不想要的. 高考: 只有分数够高的同学才能进入理想的大学.有一部分同学被拦截在大学之 ...
- SPRINGBOOT配置事物注解和@MAPPER注意
MAPPER接口要使用@Mapper注解,不能用@Compent @Repository,否则没有效果 一.开启事物 在启动类上加 @EnableTransactionManagement //如果m ...
- React 组件&Props
组件&Props 组件&Props 组件可以将UI切分成一些独立的.可复用的部件,这样你就只需要专注于构建每一个单独的组件. 组件从概念上看就像是函数,它可以接受任意的输入值(称之为& ...