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. maven之respository(仓库)本地路径修改

    maven默认的仓库路径会在c盘(我的C:\Users\Administrator\.m2\repository),这样如果系统出问题或其他原因会造成仓库损坏,因此会自己配置仓库路径. 1.首先找到你 ...

  2. 简单易懂的crontab设置工具集

    导读 在上一篇文章中,我介绍了crontab的配置文件以及如何设置定时任务,对于偶尔用一次crontab的同学而言,可能遗忘配置语法会很快,这里我向大家推荐几个在线设置crontab任务的网站. ge ...

  3. XSS Filter绕过

    之前挖到某金融网站的xss 但是困于xss filter无奈不好下手.只能在火狐下弹窗. 以下该图是我的测试. 后来发给一个Invoker哥们儿.成功给我发来payload成功绕过了XSS Filte ...

  4. java代码和spring框架读取xml和properties文件

    1.java文件读取properties文件 Properties props = new Properties(); try { //资源文件存放在类文件的根目录下.即是放在src下面.则不需要写路 ...

  5. Python自动化之sqlalchemy关联查询

    外键关联 from sqlalchemy import ForeignKey from sqlalchemy.orm import relationship class Address(Base): ...

  6. NDK学习二: NDK目录结构

    NDK目录结构   NDK下载好之后目录结构如下:         目录名 描述 build   存放和编译相关的脚本文件,最外面的ndk-build就是调用该目录下的makefile文件,其中mak ...

  7. Post方法调用公司发Mail的接口

    调用公司发Mail的接口. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  8. 一个Try多个Catch需要注意的事项

    一个程序包含一个try块和两个catch块,两个catch子句都有能力捕捉一个try块发出的异常,若两个catch子句次序不同时程序结果会发生变化吗? 一个try块后有两个catch块,这很正常,因为 ...

  9. js 的 深拷贝 和 浅拷贝

    http://www.cnblogs.com/yichengbo/archive/2014/07/10/3835882.html 类似于拷贝指针和 值?  ...占坑先.

  10. delphi 快捷键

    1. 编辑器     按键时候请注意输入法: 英文状态 Code Folding (Fold ---Methods)   [Ctrl + Shift] + K + M