Leetcode 贪心 Jump Game
来源:互联网 发布:什么软件外卖货到付款 编辑:程序博客网 时间:2024/05/26 02:55
Jump Game
Total Accepted: 18745 Total Submissions: 68916My SubmissionsGiven 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
.
题意:给一个包含非负整数的数组,每个数表示最大能跳跃的距离,最初的位置在
下标 0处,问能否跳到数组的最后一个位置
思路0:贪心
问能否跳最后一个位置,可以将问题转换为跳到最后一个位置后剩余的最大步数(如果不能跳到,提早结束程序)。
通过求到每个位置剩余的最大步数可求到最后一个位置的剩余的最大步数。
设 step = A[0],到下一个位置时,step--,并且step = max(step, A[1]);
复杂度:时间O(n),空间O(1)
bool canJump(int A[], int n){if(n == 0) return false;int step = A[0];for(int i = 1; i < n; ++i){if(step <= 0) return false;--step;step = max(step, A[i]);}return true;}
0 0
- Leetcode 贪心 Jump Game
- [leetcode][贪心] Jump Game
- leetcode---Jump Game---贪心
- leetcode---jump-game---贪心
- Leetcode 贪心 Jump Game II
- [leetcode][贪心] Jump Game II
- leetCode #55 Jump Game 贪心
- LeetCode Jump Game(贪心算法)
- LeetCode Jump Game II(贪心)
- [leetcode]55. Jump Game 贪心
- leetcode 55. Jump Game [贪心]
- Jump Game (能否到达终点,贪心) 【leetcode】
- [LeetCode]Jump Game II(贪心&&DP!!!!!)
- LeetCode 之 Jump Game(贪心算法)
- leetCode #45 Jump Game II 贪心
- leetcode -- Jump Game II -- 贪心,要看
- LeetCode-55-Jump Game(贪心)-Medium
- leetcode 55. Jump Game-贪心算法
- Hibernate内部缓存分析
- C#中使用DES和AES加密解密
- span 固定宽度,内容自适应容器自动换行
- 哈希查找
- Android 程序开发框架
- Leetcode 贪心 Jump Game
- NGUI 中物体坐标/旋转等归零的小功能
- Servlet 工作原理解析
- 一个c++版本的json实现思路
- Lua学习笔记
- eclipse快捷键
- [Android] 获取Android设备的唯一识别码|设备号|序号|UUID
- Bloom Filter概念
- jquery.ua.js docs之判断浏览器利器推荐