leetcode 55. Jump Game [贪心]
来源:互联网 发布:中国超级高铁计划知乎 编辑:程序博客网 时间:2024/06/05 12:29
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
.
完全自己做出来的题,吼吼吼,好开心,一会去做matlab的实验
根据题意,很容易想到是要越过0的点,举几个例子:
3,2,1,0 false
3,2,2,0 true
4,2,1,0 true
3,3,1,0 true
说明什么?0左边的只要有一个能越过这个0的就可以。
那么很容易想到,这个算法是将数组以0分割的,分段判断是否能够成立的
判断是否能越过这个点就是pos-i<=A[i] pos表示最右边的0
循环的时候从右往左,不用考虑n-1这个点,这个点是几都无所谓,只要能到就行
class Solution {public: bool canJump(int A[], int n) { int pos=n-1; if(n<=1)return true; bool flag=0; bool exist=0; for(int i=n-2;i>=0;i--) { if(A[i]==0&&flag==1) { if(exist==0)return false; pos=i+1; flag=0; exist=0; continue; } if(A[i]!=0)flag=1; if(pos-i<=A[i])exist=1; } if(exist==0)return false; else return true; }};
0 0
- [leetcode]55. Jump Game 贪心
- leetcode 55. Jump Game [贪心]
- Leetcode 贪心 Jump Game
- [leetcode][贪心] Jump Game
- leetcode---Jump Game---贪心
- leetcode---jump-game---贪心
- leetcode 55. Jump Game-贪心算法
- LeetCode 55. Jump Game|贪心算法
- leetcode.55.Jump Game(medium)[贪心算法]
- leetcode 55. 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
- LeetCode --- 55. Jump Game
- [Java]serialVersionUID是干什么用的?(一)
- matlab下WindowMediaPlayer测试使用
- Ubuntu下安装JDK及配置环境变量
- BOM对象模型应用分析
- android(十五) 录制影片
- leetcode 55. Jump Game [贪心]
- Geode 创建并使用集群配置
- 如何优雅的格式化接口
- MFC连接地址
- AES对称加密Util
- [Java String] StringBuilder类解读
- bzoj 4810: [Ynoi2017]由乃的玉米田 莫队算法+bitset
- C++ 迭代器 iterator
- cefsharp.Wpf 入门