LeetCode 55
2016-05-20 21:49
218 查看
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.
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.
/************************************************************************* > File Name: LeetCode055.c > Author: Juntaran > Mail: JuntaranMail@gmail.com > Created Time: Wed 11 May 2016 20:30:25 PM CST ************************************************************************/ /************************************************************************* 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. ************************************************************************/ #include <stdio.h> #define MAX(a,b) ((a)>(b) ? (a) : (b)) int canJump(int* nums, int numsSize) { int sum = 0; int i; for( i=0; i<numsSize && sum<=numsSize; i++ ) { if (i > sum) { return 0; } sum = MAX( nums[i]+i, sum ); printf("sum is %d\n", sum); } return 1; } int main() { int nums[] = { 2,3,1,1,4 }; int numsSize = 5; int ret = canJump( nums, numsSize ); printf("%d\n", ret); return 0; }
相关文章推荐
- 括号配对问题
- spark yarn分布式安装
- 牛客网的题,重建二叉树。(用树的先序,中序,还原二叉树)
- POJ 2253 Frogger(翻译/最短路/floyd/思维)
- Java并发教程(Oracle官方资料)
- OOM原因和解决方案
- dex文件入门(二)
- jsp 四大对象的作用域
- Java内存与垃圾收集知识总结
- ubuntu系统没有声音解决方法
- SQL SERVER 对权限的授予GRANT、拒绝DENY、收回REVOKE
- 【算法总结系列-1】快速排序-c语言实现
- THE TOWER OF HANOI
- 在一个数组中找差值最大数对,输出最大差值
- Swift-构造过程(Initialization)(十二)
- nyoj 715相邻的计数(dp)
- char *a和 char a[]
- MySQL入门——修改数据表2:往指定的表中添加多列数据(字段)
- linux awk命令详解
- poj 2356 Find a multiple(鸽巢原理)