leetcode——45. 跳跃游戏 II

我真的是超开心了,又做对了!!!!!!而且没走啥弯路!!!!!!!

class Solution(object):
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)<2:
            return 0
            
        pact=0
        i=0
        while i<len(nums):
            if nums[i]>=len(nums)-i-1:
                pact+=1
                return pact
            else:
                pact+=1
                m=0
                k=[0]
                for j in range(i,i+nums[i]+1):
                    if nums[j]>=len(nums)-j-1:
                        pact+=1
                        return pact
                    else:
                        if j+nums[j]>m:
                            m=j+nums[j]
                            k[0]=j
                i=k[0]
执行用时 :88 ms, 在所有 python 提交中击败了90.09%的用户
内存消耗 :13.3 MB, 在所有 python 提交中击败了32.95%的用户
 
执行用时为 68 ms 的范例
class Solution(object):
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        cur = pre = step = 0
        for i in range(len(nums)-1):
            cur = max(cur,nums[i]+i)
            if i == pre:
                step += 1
                pre = cur
        return step

                                                                                                ——2019.10.14


JAVA 动态规划:

public int jump(int[] nums) {
        int n = nums.length;
        if(n<=1) return 0;
        int[] dp = new int[n];  //跳跃到位置i所需的最小步数
        dp[1] = 1;
        for(int i = 2;i<n;i++){
            for(int j = 0;j<i;j++){
                if(nums[j] >= i-j){
                    dp[i] = dp[j] + 1;
                    break;
                }
            }
        }
        return dp[n-1];
    }

方法二:从后往前进行循环

public int jump(int[] nums) {
        int n = nums.length;
        if(n<=1) return 0;
        int count = 1;
        int i = n-1;
        int index = n-1;
        while(i>=0){
            for(int j = i;j>=0;j--){
                if(nums[j] + j >= i){
                    index = j;
                }
            }
            if(index == 0){
                return count;
            }else{
                i = index;
                count++;
            }
        }
        return count;
    }

方法三:贪心算法

public int jump(int[] nums) {
        int n = nums.length;
        int step = 0;
        int left = 0;
        int right = 0;
        if(n == 1) return 0;
        while(left<=right){
            step++;
            int old_right = right;
            for(int i = left;i<=old_right;i++){
                int new_right = i + nums[i];
                if(new_right >= n-1){
                    return step;
                }
                if(new_right > right){
                    right = new_right;
                }
            }
            left = old_right + 1;
        }
        return step;
    }

方法四:

public int jump(int[] nums) {
        int n = nums.length;
        int result = 0;
        int last = 0;
        int cur = 0;
        for(int i = 0;i<n;i++){
            if(i>last){
                last = cur;
                result ++ ;
            }
            cur = Math.max(cur,i+nums[i]);
        }
        return result;
    }

 稍做修改:

public int jump(int[] nums) {
        int n = nums.length;
        if(n<=1) return 0;
        int result = 0;
        int last = 0;
        int cur = 0;
        for(int i = 0;i<n;i++){
            if(i>last){
                last = cur;
                result ++ ;
            }
            cur = Math.max(cur,i+nums[i]);
            if(cur>=n-1){
                return result+1;
            }
        }
        return result;
    }

——2020.8.5

我的前方是万里征途,星辰大海!!
原文地址:https://www.cnblogs.com/taoyuxin/p/11671059.html