55. Jump Game(贪心)
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
jump length is 0, which makes it impossible to reach the last index. 贪心思想
如果当前存的步数now 小于于当前位置可以跳的步数,则更新now now 小于0则死掉了。
class Solution {
public boolean canJump(int[] nums) {
int now = nums[0];
for(int i =1;i<nums.length;i++){
now--;
if(now<0) return false;
if(nums[i]>now){
now = nums[i];
}
}
return true;
}
}
55. Jump Game(贪心)的更多相关文章
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- 55. Jump Game leetcode
55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...
- [Leetcode][Python]55: Jump Game
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
- 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 ...
- 刷题55. Jump Game
一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...
- leetcode 55 Jump Game 三种方法,回溯、动态规划、贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 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 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- shell脚本技巧记录
2014/4/9 shell脚本变量处理: ${varible##*string} //从左向右截取最后一个string后的字符串 ${varible#*string} //从左向右截取第一个stri ...
- C#取得Web程序和非Web程序的根目录的N种取法
取得控制台应用程序的根目录方法方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径方法2.AppDomain.CurrentDomain.BaseDi ...
- Android ScrollView嵌套ScrollView滚动的问题解决办法
引用:http://mengsina.iteye.com/blog/1707464 http://fenglog.com/article.asp?id=449 Android ScrollView嵌套 ...
- MUI---上传头像功能实现
这里使用MUI上传头像的功能是结合VUE来做的,所以: changeFace:function(){ var IMAGE_UNSPECIFIED = "image/*"; //相册 ...
- pandas常用
#python中的pandas库主要有DataFrame和Series类(面向对象的的语言更愿意叫类) DataFrame也就是#数据框(主要是借鉴R里面的data.frame),Series也就是序 ...
- Centos 添加永久路由
今天在用虚拟机测试的时候,突然发现外网不通了,记得之前加过路由的,重启网络服务后就没了,仔细一想,应该是添加的路由是临时的,一重启就没了,于是乎就有了想把它永久写入的冲动,在看了一些文档之后,只要在/ ...
- eclipse快速向下复制行
Ctrl+Alt+↓ 复制当前行到下一行(复制增加)
- HDU 4352 - XHXJ's LIS - [数位DP][LIS问题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- NEFU 118 - n!后面有多少个0 & NEFU 119 - 组合素数 - [n!的素因子分解]
首先给出一个性质: n!的素因子分解中的素数p的幂为:[ n / p ] + [ n / p² ] + [ n / p³ ] + …… 举例证明: 例如我们有10!,我们要求它的素因子分解中2的幂: ...
- POJ - 1054 The Troublesome Frog 模拟 枚举优化。
题意:有个R*C的格网.上面有若干个点,这些点可以连成一些直线,满足:这些点在直线上均匀排布(也就是间隔相等),直线的两段穿过网格(也就是第一个,最后一个在网格的边界附近) 求某条直线上最多的点数 题 ...