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. Keepalived高可用集群介绍

    1.Keepalived服务介绍 Keepalived起初是专为LVS设计的,专门用来监控LVS集群系统中各个服务节点的状态,后来又加入了VRRP的功能,因此除了配合LVS服务外,也可以为其他服务(n ...

  2. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  3. iOS开发——高级篇——传感器(加速计、摇一摇、计步器)

    一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...

  4. Linux新手应掌握的10个基本命令

    导读 Linux对我们的生活有着很大的影响.然而在Linux上,你通常应该使用终端命令,而不是只要点击启动器图像(就像你在Windows上操作那样).这10个基本的Linux命令和重要命令会帮助你尽快 ...

  5. JavaScript break跳出多重循环

    多重循环在编程中会经常遇到,那么在JavaScript中如何指定跳出那层的循环呢.其实这也是break的一个用法,下面是一个不错的例子,来自<JavaScript权威指南>,可以参考下: ...

  6. ThinkPHP函数详解:F方法(快速缓存方法)

    在Think中S方法的用法,F方法其实是S方法的一个子集功能, 仅用于简直数据缓存,并且只能支持文件形式,不支持缓存有效期,因为采用 的是PHP返回方式,所以其效率较S方法较高,因此我们也称之为快速缓 ...

  7. 18 TaskScheduler任务调度器抽象基类——Live555源码阅读(一)任务调度相关类

    这是Live555源码阅读的第二部分,包括了任务调度相关的三个类.任务调度是Live555源码中很重要的部分. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/ol ...

  8. xp远程桌面登陆需要身份验证问题解决

    今天在客户这边部署了一个专门用于远程访问的计算机,但是系统是xp,windows服务器最新的已经到2012版本了,windows服务器从2008开始就要求远程桌面登陆支持nla,因此在使用xp登陆wi ...

  9. python程序性能分析

    中文:http://www.cnblogs.com/zhouej/archive/2012/03/25/2379646.html 英文:https://www.huyng.com/posts/pyth ...

  10. c++写入txt文件

    简单方式: #include "stdafx.h" #include <iostream> #include <iomanip> #include < ...