55. Jump Game

  • Total Accepted: 95819
  • Total Submissions: 330538
  • Difficulty: Medium

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.

贪心算法:正向

 class Solution {
 public:
     bool canJump(const vector<int>& nums) {
         ; //reach the rightest position
         ; i < reach && reach < nums.size(); ++i)
             reach = max(reach, i +  + nums[i]);
         return reach >= nums.size();
     }
 };

//贪心算法,逆向,比正向稍费时

 class Solution {
 public:
     bool canJump (const vector<int>& nums) {
         if (nums.empty()) return true;
         //
         ;
         ; i >= ; --i)
             if (i + nums[i] >= left_most)
                 left_most = i;
         ;
     }
 };
 bool canJump(int* nums, int numsSize) {
     ;
     int flag = true;
     ; i < numsSize ; ++i) {
         int j = i;
         for (; j <= idx + nums[idx]; ++j) {
             if(idx + nums[idx] <= j + nums[j]){
                 idx = j;
                 flag = true;
                 break;
             }else {
                 flag = false;
             }
         }

     }
     )
     return true;
     return false;
 }

9月份人人net来某校宣讲,其中一个编程题即为此题.

 #include <iostream>
 #include <string.h>
 #include <mcheck.h>
 using namespace std;
 bool test(int *arr, int size){
     ;
     ) {
         //cout << idx << endl;
         ) return false;
         idx += arr[idx];
          //cout << idx << endl;
     }
     ) return true;
     else
     return false;
 }

 int main(int argc, char* argv[])
 {
     ,,,,,};
     ]);
     bool flag = test(arr, len);
     cout << flag << endl;
     ,,,,,};
 ]);
     cout << test(a, lena) << endl;
     ;
 }

55. Jump Game leetcode的更多相关文章

  1. [Leetcode][Python]55: Jump Game

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...

  2. Leetcode 55. Jump Game & 45. Jump Game II

    55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...

  3. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  4. Jump Game - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Jump Game - LeetCode 注意点 解法 解法一:贪心算法,只关注能到达最远距离,如果能到达的最远距离大于结尾说明能到达,否则不能.并且如果 ...

  5. 刷题55. Jump Game

    一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...

  6. [LeetCode] 55. Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. Jump Game 的三种思路 - leetcode 55. Jump Game

    Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...

  8. LeetCode 55. Jump Game (跳跃游戏)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. 【一天一道LeetCode】#55. Jump Game

    一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...

随机推荐

  1. mysql基础面试

    php面试题之五--MySQL数据库(基础部分) 五.MySQL数据库 mysql_num_rows() mysql_affected_rows() 这两个函数都作用于 mysql_query($qu ...

  2. apache 访问出现403 Forbidden

    在linux虚拟机的apache上新增一个虚拟目录/var/wordpress,想把理论网挂上去. 在配置文件httpd.conf中,把”Include conf/extra/httpd-vhosts ...

  3. POJ3461——Oulipo

    1.题目大意:单字符串匹配问题 2.分析:经典KMP问题 存个模板QAQ #include <cstdio> #include <cstdlib> #include <c ...

  4. am335x sd卡启动开启识别emmc kernel 上的改动

    sbc 7109-454 sd 卡启动qt系统后一直识别不了  emmc 也就是mmc1口, 一开始以为是硬件初始化的问题,后面又以为是io口复用,最后才知道是根本没有注册mmc1设备. 更改下面的代 ...

  5. Linux ffmpeg命令的介绍与使用

    ffmpeg使用语法 ffmpeg [[options][`-i' input_file]]... {[options] output_file}... 如果没有输入文件,那么视音频捕捉(只在Linu ...

  6. SNMP报文抓取与分析(一)

    SNMP报文抓取与分析(一) 1.抓取SNMP报文 SNMP报文的形式大致如下图所示 我们这里使用netcat这个工具来抓取snmp的PDU(协议数据单元).(因为我们并不需要前面的IP和UDP首部) ...

  7. Exception异常

    JAVA异常指的是运行期出现的错误. 观察错误的名字和行号最重要. 运用关键字try将可能出错的语句catch出来并添加友好的话 \ 在这句话中的ae代表一个自己命名的对象. 1.要捕获首先需要知道错 ...

  8. java.lang.ClassNotFoundException: net.sf.json.JSONArray,java.lang.NoClassDefFoundError: net/sf/json/JSONArray jetty跑项目遇到的问题

    2016-05-18 15:44:25 ERROR Dispatcher.error[user:|url:]:L38 - Dispatcher initialization failed Unable ...

  9. Droid4x快照还原

    一.问题描述 1. Droid4x还原快照可以通过VirtualBox 先还原快照 2. virtualbox 还原快照之后 如果没有用virtualbox启动 并关闭     而是直接启动Droid ...

  10. php 通过API接口连接12306余票查询

    <?php header("content-type:text/html;charset='utf-8'"); //设置编码 echo "<meta cont ...