zoukankan      html  css  js  c++  java
  • 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: JuntaranMail@gmail.com
     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
    ", 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
    ", ret);
    55     
    56     return 0;
    57 }
  • 相关阅读:
    万维网
    MySQL客户端输出窗口显示中文乱码问题解决办法
    mysql数据库delete数据时不支持表别名
    Java 实现在固定区间内随机生成整数
    【面试】MySQL 中NULL和空值的区别?
    一个因MySQL大小写敏感导致的问题
    windows查看服务的状态
    不就是Select Count语句吗,竟然能被面试官虐的体无完肤!
    Java 代码的精优化
    java服务宕机原因查询
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5513526.html
Copyright © 2011-2022 走看看