题解 | #青蛙跳跃#

青蛙跳跃

https://www.nowcoder.com/practice/290a76e0d54c4fa6951098c38781c50b?tpId=363&tqId=10605486&ru=/exam/oj&qru=/ta/super-company23Year/question-ranking&sourceUrl=%2Fexam%2Foj

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pond int整型一维数组 
     * @return int整型
     */
  public int minJump(int[] pond) {
        // 跳跃的次数
        int jumpNumber = 0;
        // 当前能够到的距离
        int tempPos = 0;
        // 下一步能够到的距离
        int pos = 0;
        for (int i = 0; i <= tempPos&&tempPos<pond.length-1; i++) {
            // 每次更新最大能够到达的距离
            pos = Math.max(pos,i+pond[i]);
            // 如果i==tempPos 表明已经进入边界,需要更新当前能够到达的距离
            if(i==tempPos){
                // 更新距离
                tempPos = pos;
                // 跳跃次数++
                jumpNumber++;
            }
        }
        return jumpNumber;
    }
}

本题知识点分析:

1.动态规划

2.数学模拟

3.API函数

4.数组遍历

本题解题思路分析:

1.记录当前能够到达的距离tempPos,Pos是最大能够达到的距离,jumpNumber是跳跃次数

2.每次更新最大能够到达的距离

3.如果i==tempPos 表明已经进入边界,需要更新当前能够到达的距离

4.更新距离,跳跃次数++

关键点:循环条件:i<=tempPos&&tempPos<nums.length-1,如果tempPos能够达到末尾,那么自然结束循环

本题使用编程语言: Java

如果你觉得本篇文章对你有帮助的话,可以点个赞支持一下,感谢~

全部评论

相关推荐

2 收藏 评论
分享
牛客网
牛客企业服务