LeetCode 55

Jump Game

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.

Determine if you are able to reach the last index.

For example:

A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

 1 /*************************************************************************
 2     > File Name: LeetCode055.c
 3     > Author: Juntaran
 4     > Mail: [email protected]
 5     > Created Time: Wed 11 May 2016 20:30:25 PM CST
 6  ************************************************************************/
 7
 8 /*************************************************************************
 9
10     Jump Game
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     Determine if you are able to reach the last index.
18
19     For example:
20
21     A = [2,3,1,1,4], return true.
22
23     A = [3,2,1,0,4], return false.
24
25  ************************************************************************/
26
27 #include <stdio.h>
28
29 #define MAX(a,b) ((a)>(b) ? (a) : (b))
30
31 int canJump(int* nums, int numsSize) {
32
33     int sum = 0;
34
35     int i;
36     for( i=0; i<numsSize && sum<=numsSize; i++ )
37     {
38         if (i > sum)
39         {
40             return 0;
41         }
42         sum = MAX( nums[i]+i, sum );
43         printf("sum is %d\n", sum);
44     }
45     return 1;
46 }
47
48 int main()
49 {
50     int nums[] = { 2,3,1,1,4 };
51     int numsSize = 5;
52
53     int ret = canJump( nums, numsSize );
54     printf("%d\n", ret);
55
56     return 0;
57 }
时间: 2024-11-05 19:20:34

LeetCode 55的相关文章

BFS问题-LeetCode 55、45、5297、127、433、434(BFS)

[LeetCode #55]跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true 解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 跳 3 步到达最后一个位置. class Solution { public: bool canJump(vector& nums) { int maxReach = nums[0];

[LeetCode]55.Jump Game

题目 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. Determine if you are able to reach the last index. For example:

LeetCode 55/45 Jump Game I/II-----Greedy**

一:Jump Game 题目: 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. Determine if you are able to reach the last index.

LeetCode 55. Jump Game (跳跃游戏)

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. Determine if you are able to reach the last index. For example:A = 

LeetCode: 55. Jump Game(Medium)

1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的值代表每一次你能从当前位置跳跃的步数.问能否跳到该数组的最后一个元素位置 注意:可以跳的步数超出数组长度依旧视为可以达到最后位置 3. 解题思路 从第一个元素开始遍历,记录下你所能到达的最远位置,例如{2, 2, 0, 1, 2},遍历第一个元素时,你所能到达的最远位置是"i+nums[i]&quo

[leetcode] 55. 跳跃游戏

55. 跳跃游戏 分明就是45. 跳跃游戏 II的缩水版本嘛..??,难度高的版本居然放在了前面,把像我这种有强迫症的必须按照题号刷题的人不放在眼里么... class Solution { public boolean canJump(int[] nums) { return jump(nums) != Integer.MAX_VALUE; } public int jump(int[] nums) { int f[] = new int[nums.length]; if (nums.leng

LeetCode 55 _ Jump Game 跳跃游戏

Description: 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. Determine if you are able to reach the last index. Exa

leetcode 55 Jump Game ----- 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. Determine if you are able to reach the last index. For example:A = 

leetCode(55):Minimum Window Substring(limits.h头文件)

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such windo