刷题——蛙跳
来源:互联网 发布:万能五笔for mac版 编辑:程序博客网 时间:2024/04/30 21:42
public class 跳跃游戏2 {//方法一:暴力法超时/*public static int jump(int[] A,int index) {int minStep = Integer.MAX_VALUE;for (int i = A[index]; i >0 ; i--) {if (index+i>=A.length-1) {return 1;}if (A[i+index]==0) {continue;}int step = 1+jump(A,index+i);minStep = step<minStep?step:minStep;}return minStep;}public static int jump(int[] A) { if (A==null || A.length<=1)return 0; return jump(A,0); }*///方法二public static int jump(int[] A) {int lastFarst = 0;//上一轮最远int curFarest = 0;//本轮可跳的最远位置int jump = 0;//i记录当前位置 for (int i = 0; i < A.length; i++) { //超出了上一步的有效范围if (i>lastFarst) {jump++;lastFarst = curFarest;}if (i>curFarest) {//证明有0跳不过去return -1;}curFarest = A[i]+i>curFarest?A[i]+i:curFarest;//当前可跳至最远处} if (lastFarst>=A.length-1) {return jump;} return -1; }public static void main(String[] args) {// TODO Auto-generated method stub//int[] A = {2,3,1,1,4};int[] A = {3,2,1,0,4,7,5,7};System.out.println(jump(A));}}
阅读全文
0 0
- 刷题——蛙跳
- 蛙跳
- 递归算法学习之蛙跳问题——01
- 蛙跳算法
- POJ2253 蛙跳 图论(Dijkstra变形)
- java 蛙跳台阶问题解决算法
- C++ 遗传学SFLA混合蛙跳算法
- 蛙跳到第N个台阶的概率,一次以p的概率跳1个台阶,(1-p)的概率跳2个台阶
- 刷题——SortList
- 刷题——ReorderList
- 刷题——WordLadder
- leetcode刷题——
- 刷题——5.5
- 刷题——5.7
- 刷题——5.8
- 刷题—Palindrome Number
- C++刷题——乱七八糟
- 刷题——质数因子
- const 修饰函数参数,返回值,函数体
- 【多校训练】 hdu 6092 Rikka with Subset
- lodash学习笔记之Array方法
- 公务员面试七大忌讳(2)
- HBase总结之原理架构篇
- 刷题——蛙跳
- 如何把.mat数据存为.txt
- 百练OJ:2712:细菌繁殖
- Centos7删除自带JDK1.8
- Python3 注释
- SD卡中FAT32文件格式快速入门(图文详细介绍)
- slam需要的第三方庫的安裝
- 习题 5.12 猴子吃桃问题。
- The What Why and Wow Behind the CoreOS Container Linux