[Leetcode 55]跳格子JumpGame
【题目】
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.
【举例】
Example 1:
Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
【思路】
贪心、取最大
【代码】
倒序考虑。设flag在尾部,一直考虑前面一步i,nums[i]+i>=flag,那i点可以到达flag点,则flag回跳到i点。
当循环结束,flag跳到起点0,则返回true。
class Solution {
public boolean canJump(int[] nums) {
int flag=nums.length-1;
for(int i=nums.length-1;i>=0;i--){
if(nums[i]+i>=flag)
flag=i;
}
return flag==0;
}
}
【其他】
public boolean canJump(int[] nums) {
int tmp = 0;
for (int i=0; i<nums.length; ++i) {
if (i > tmp) return false;
tmp = Math.max(tmp, i + nums[i]);
}
return true;
}
[Leetcode 55]跳格子JumpGame的更多相关文章
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode: 55. Jump Game(Medium)
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
- [leetcode]55.JumpGame动态规划题目:跳数游戏
/** * Given an array of non-negative integers, you are initially positioned at the first index of th ...
- [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode]55. 跳跃游戏(贪心)
题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: tr ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
- LeetCode 55. Jump Game (跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
随机推荐
- 日常英语---十、MapleStory/Monsters/Level 191-201(Sad Erda:向你发射炮弹进行攻击)
日常英语---十.MapleStory/Monsters/Level 191-201(Sad Erda:向你发射炮弹进行攻击) 一.总结 一句话总结:一种悲伤的精神,形成于奥术之河,一个黑法师能够融合 ...
- 图像识别 | AI在医学上的应用 | 深度学习 | 迁移学习
参考:登上<Cell>封面的AI医疗影像诊断系统:机器之心专访UCSD张康教授 Identifying Medical Diagnoses and Treatable Diseases b ...
- linux重启后进入了救援模式,无法远程登录
卸载删除了某个文件系统分区的逻辑卷,但是没有修改fstab文件,导致系统重启异常,此时已经无法远程连接了,只能本地登录到救援模式修改
- No address associated with hostname
java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associat ...
- p1218 Superprime Rib
深搜,添加数字后如果仍为质数,继续递归. #include <iostream> #include <cstdio> #include <cmath> #inclu ...
- php字符串转成数组
/* 4.$m = “woxihuanphp”,编程实现:将字符串分割为单个字符存放到一个数组中,并打印数组? */ $m='woxihuanphp'; echo $res=trim(chunk_sp ...
- Oracle 基本操作--数据类型、修改和删除表、增删改查和复制表
一.Oracle基础数据类型:数据类型: 创建数据表时,设计数据表的结构问题,也就是设计及确定数据表中各个列的数据类型,是数值.字符.日期还是图像等其他类型. 因为只有设计好数据表结构,系统才会在磁盘 ...
- blog项目知识点梳理
1.获取图片验证码: def get_validCode_img(request): # 方式1: # import os # path= os.path.join(settings.BASE_DIR ...
- python-django rest framework框架
1.API 接口 是什么,干什么用的? API简单的来说就是一个url - http://www.oldboyedu.com/get_user/ - http://www.oldboyedu.com ...
- 【转】你必须知道的EF知识和经验
注意:以下内容如果没有特别申明,默认使用的EF6.0版本,code first模式. 推荐MiniProfiler插件 工欲善其事,必先利其器. 我们使用EF和在很大程度提高了开发速度,不过随之带来的 ...