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.)
/*************************************************************************
> File Name: LeetCode045.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Tue 13 May 2016 14:47:33 PM CST
************************************************************************/ /************************************************************************* 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.) ************************************************************************/ #include <stdio.h> int jump(int* nums, int numsSize)
{
if( numsSize <= )
{
return ;
} int left = nums[];
int right = nums[];
int count = ;
int max = ; int i;
for( i=; i<numsSize; i++ )
{
if( i > left )
{
left = right;
count ++;
}
right = (i+nums[i])>right ? (i+nums[i]) : right;
if( left >= numsSize- )
{
return count;
}
}
return ;
} int main()
{
int nums[] = {,,,,};
int numsSize = ; int ret = jump( nums, numsSize );
printf("%d\n", ret); return ;
}
LeetCode 45的更多相关文章
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 45 Jump Game II(按照数组进行移动)
题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 45. 跳跃游戏 II | Python
45. 跳跃游戏 II 题目来源:https://leetcode-cn.com/problems/jump-game-ii 题目 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素 ...
- Java实现 LeetCode 45 跳跃游戏 II(二)
45. 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...
- 力扣Leetcode 45. 跳跃游戏 II - 贪心思想
这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...
随机推荐
- iOS事件机制(二)
从上一篇的内容我们知道,在iOS中一个事件用一个UIEvent对象表示,UITouch用来表示一次对屏幕的操作动作,由多个UITouch对象构成了一个UIEvent对象.另外,UIResponder是 ...
- HDU 5660 jrMz and angles (暴力枚举)
jrMz and angles 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/E Description jrMz has tw ...
- DATASNAP为支持FIREDAC而增加的远程方法的数据类型TFDJSONDataSets
前面的博客提到用FIREDAC全面替代COM那一套东西:DATAPROVIDER,OLEVARIANT,CLIENTDATASET,DBEXPRESS... 显然,DATASNAP的远程方法必须增加对 ...
- Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)
题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...
- HDU 4432 Sum of divisors (水题,进制转换)
题意:给定 n,m,把 n 的所有因数转 m 进制,再把各都平方,求和. 析:按它的要求做就好,注意的是,是因数,不可能有重复的...比如4的因数只有一个2,还有就是输出10进制以上的,要用AB.. ...
- map的正确删除方式
遍历删除map元素的正确方式是 for(itor = maptemplate.begin; itor != maptemplate.end(); ) { if(neederase) ...
- 解决安装SQL Server2008失败的问题
安装SQL Server2008时遇到"2008安装错误 必须重新启动计算机才能安装 SQL Server". 解决办法:HKEY_LOCAL_MACHINE\SYSTEM\Cu ...
- Autofac实例生命周期
1.默认,每次请求都会返回一个实例 builder.RegisterType<X>().InstancePerDependency(); 2.Per Lifetime Scope:这个作用 ...
- SqlServer获取表结构语句
--sql server 2005-- 1. 表结构信息查询 -- ================================================================== ...
- 判断sqlserver临时表等临时资源是否存在
if exists(select * from tempdb..sysobjects where id=object_id('tempdb..#TEMP')) drop table #TEMP