LeetCode 45

Jump Game II

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.)

 1 /*************************************************************************
 2     > File Name: LeetCode045.c
 3     > Author: Juntaran
 4     > Mail: [email protected]
 5     > Created Time: Tue 13 May 2016 14:47:33 PM CST
 6  ************************************************************************/
 7
 8 /*************************************************************************
 9
10     Jump Game II
11
12     Given an array of non-negative integers,
13     you are initially positioned at the first index of the array.
14
15     Each element in the array represents your maximum jump length at that position.
16
17     Your goal is to reach the last index in the minimum number of jumps.
18
19     For example:
20     Given array A = [2,3,1,1,4]
21
22     The minimum number of jumps to reach the last index is 2.
23     (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
24
25  ************************************************************************/
26
27 #include <stdio.h>
28
29 int jump(int* nums, int numsSize)
30 {
31     if( numsSize <= 1 )
32     {
33         return 0;
34     }
35
36     int left = nums[0];
37     int right = nums[0];
38     int count = 1;
39     int max = 0;
40
41     int i;
42     for( i=1; i<numsSize; i++ )
43     {
44         if( i > left )
45         {
46             left = right;
47             count ++;
48         }
49         right = (i+nums[i])>right ? (i+nums[i]) : right;
50         if( left >= numsSize-1 )
51         {
52             return count;
53         }
54     }
55     return 0;
56 }
57
58 int main()
59 {
60     int nums[] = {2,3,1,1,4};
61     int numsSize = 5;
62
63     int ret = jump( nums, numsSize );
64     printf("%d\n", ret);
65
66     return 0;
67 }
时间: 2024-08-03 17:49:16

LeetCode 45的相关文章

LeetCode 45 Jump Game II(按照数组进行移动)

题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. 求解出从下标为0开始到下标到数组最后一个所需要的最少跳动次数! 1.当数组为空或者数组长度等于1时,不需要跳动,因此返回0 否则初始化step=1 2.初始化left=0 right = nums[0] 当left<=right时进入循环体中. 3.从第i=0开始,如果当前跳动距离大于数组长度则返回st

leetcode 45 Jump Game II ---- java

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

LeetCode 45. 跳跃游戏2

给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [2,3,1,1,4]       输出: 2       解释: 跳到最后一个位置的最小跳跃数是 2.       从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置.       说明: 假设你总是可以到达数组的最后一个位置. 来源:力扣(LeetCode)      链接:https

[LeetCode] #45 Jump Game II

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

leetCode(45):Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e

LeetCode 45 Permutations

Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 思路:使用字典序法,思路见http://blog.csdn.net/mlweixiao/article/details/38893507,这样不管

leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

Jump Game II 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 nu

leetcode || 45、 Jump Game II

problem: 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

19.2.7 [LeetCode 45] Jump Game II

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