- 浏览: 174053 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
我们可以用贪心算法来解决。维护一个指针,指针不断的靠近终点,每前进一步指针的值都减1,指针的值根据经过的元素而更新。代码如下:
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
我们可以用贪心算法来解决。维护一个指针,指针不断的靠近终点,每前进一步指针的值都减1,指针的值根据经过的元素而更新。代码如下:
public class Solution { public boolean canJump(int[] nums) { if(nums == null || nums.length == 0) return false; int cur = nums[0]; for(int i = 1; i < nums.length; i++) { if(cur >= nums.length - 1) return true; //每前进一步,cur的值就减1 cur --; if(cur < 0) return false; //cur移动到了nums[i]的位置,查看是否更新cur的值 if(nums[i] > cur) { cur = nums[i]; } if(cur == 0 && i < nums.length - 1) return false; } return true; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 228Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 231You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 344Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 335Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 455Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 518Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 433Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 623Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 431The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 389Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 521Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 537Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 374All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 866Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 884Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 558Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 604Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 766Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 738You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 632For a undirected graph with tre ...
相关推荐
1.贪心算法中,作出的每步贪心决策都无法改变,因为贪心策略是由上一步的最优解推导下一步的最优解,而上一步之前的最优解则不作保留 2.由(1)中的介绍,可以知道贪
jumpgame untiy项目源码
通过python脚本实现微信跳一跳小游戏自动跳跃
leetcode45题leetcode45题leetcode45题leetcode45题leetcode45题leetcode45题leetcode45题leetcode45题leetcode45题leetcode45题
JumpGame
本简易版 跳一跳使用Cocos来完成编写。 (使用js进行逻辑编写) 对应csdn博客链接:https://blog.csdn.net/weixin_43388844/article/details/96730842
jumpGame 给我孩子们的跳棋游戏。
leetcode卡跳跃游戏-IV 在这里找到了 Jump Game IV 的解决方案: 该解决方案适用于小型测试用例,但不适用于非常大的测试用例——仍在进行中。
Yaaa,您喜欢Jumping Game,所以现在就玩在线Doodle Jump游戏[更新]嬉皮乐使您跃入Doodle Jump榜首。 嬉皮在Doodle Jump中跳到顶部。 这款激动人心的平台游戏可让您向上跳跃到无限远,抓住助力,避免途中遇到野兽。 ...
Doodle Jump game for calc
语言:Bahasa Melayu,Deutsch,English,English (UK),English (United States),Français,Nederlands,Norsk,Tiếng Việt,Türkçe,català,dansk,eesti,español,español (Latinoamérica),hrvatski,italiano,latvie...
Helix Jump game. Full game helix jump.
跳一跳辅助,适用于IOS及安卓系统,安卓模拟器可用
跳一跳源代码包,实现手机自动玩跳一跳,可以看一看学习一下,转自别人
Jump Game II Best Time to Buy and Sell Stock Best Time to Buy and Sell Stock II Longest Substring Without Repeating Characters Container With Most Water Patching Array 动态规划 Triangle Maximum ...
一个小小的跳跃游戏,比较简单。希望大家喜欢
The Tiny.js game project build by tinyjs-cli 目录: 聚气效果 重构 UI 命令 npm install: 安装依赖 npm start: 本地服务,默认端口:8017 npm build: 执行编译 关于 Tiny.js 官网: 指南: API: 关于标准版 此项目...
本文件WECHAT-Jump-JumpSharp-master.rar,微信跳一跳的源码。
可以说是它的加强版,谢谢!操作方法:按 ← → 键控制弹板,接住小球。 等级平均数值: 1级:平民:78.5% 高手:100% 2级:平民:52.1% 高手:83.6% ... 3级:平民:12.2% 高手:36.7% ... 4级:平民:5.4% 高手:...
and want to jump head first into the world of video game development. It’s also great follow-up book for readers of Foundation Game Design with HTML5 and JavaScript (by the same author) who want to ...