麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

LeetCode Jump Game II

2019-11-11 06:12:49
字體:
來源:轉載
供稿:網友

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.

Your goal is to reach the last index in the minimum number of jumps.

For example:Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

Note:You can assume that you can always reach the last index.

思路一:將所有的情況都考慮進去,基于遞歸進行暴力搜索

代碼如下:

class Solution {public:    void oneJump(vector<int>& nums,int& minJump,int& count,int& pos)    {        if(pos == nums.size()-1)        {            if(minJump > count)            {                minJump = count;                return;            }        }                    for(int i=pos+1;i<=pos+nums[pos];i++)        {            if(i>nums.size()-1)                return;            count++;            oneJump(nums,minJump,count,i);            count--;        }    }        int jump(vector<int>& nums) {        int minJump = 0x7fffffff;        int count = 0;        int pos = 0;        oneJump(nums,minJump,count,pos);        return minJump;        }};結果是用時超時,需要進行剪枝或尋找更好的方法

思路二:仔細分析題目發現,題目關心的是最少通過幾步達到,對于具體的到達選擇并不關心,并且必然會達到。所以可以將其轉換為廣度優先算法模型,然后采用貪心原則進行求解,

代碼如下:

class Solution {public:    int jump(vector<int>& nums) {        if(nums.size() < 2)            return 0;                    int level=0,currentMax=0,nextMax=0;        int pos=0;        while(currentMax-pos+1>0)        {            level++;            for(;pos<=currentMax;pos++)            {                if(pos+nums[pos] > nextMax)                    nextMax = pos+nums[pos];                if(nextMax >= nums.size()-1)                    return level;            }            currentMax = nextMax;        }        return 0;    }};


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久免费视频一区 | 一级黄色片武则天 | 中文字幕国产一区 | 免费在线观看亚洲 | 欧美日韩在线中文字幕 | 美国av免费看 | 国产成人精品免高潮在线观看 | 国产一级aa大片毛片 | 亚洲网站在线观看视频 | 欧美三级欧美成人高清www | chinese18 xxxx videos| 国产免费大片视频 | 久久亚洲第一 | 鲁丝一区二区二区四区 | 日韩av影片在线观看 | v11av在线播放 | 免费看一级毛片欧美 | 色婷婷久久久久久 | 中文字幕综合 | 日本综合久久 | 国产一区二区三区色淫影院 | 国产成人在线观看免费网站 | 亚洲欧美一区二区三区在线观看 | 久草在线视频网 | 欧美日本一 | 国产乱淫a∨片免费观看 | 欧美成年性h版影视中文字幕 | 色97色| 日韩一级精品 | 久久久aa | 欧美亚洲免费 | 成人黄色小视频网站 | 精品人伦一区二区三区蜜桃网站 | 手机av免费电影 | 亚洲视频在线网 | 久久久久久久久久久久久久久久久久 | 亚洲精品午夜电影 | 毛片免费视频播放 | av在线免费电影 | 一级毛片免费高清视频 | 小视频在线看 |