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. javax.swing.JList 设置分割线

    public class TestJList extends JFrame { public TestJList() { JList list = new JList(new String[] { & ...

  2. net-snmp配置文件详解

    net-snmp配置文件详解 net-snmp的配置文件是有一定的层次结构的,配置起来也很方便.网上找了很多资料,大概把这个配置文件的各个信息搞懂了一点.其实在net-snmp的EXAMPLE.con ...

  3. @ModelAttribute运用详解

      @ModelAttribute使用详解 1.@ModelAttribute注释方法     例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个 ...

  4. midi格式

    http://www.ccarh.org/courses/253/handout/smf/

  5. 一个简单的任务执行时间监视器 StopWatch

    有时我们在做开发的时候需要记录每个任务执行时间,或者记录一段代码执行时间,最简单的方法就是打印当前时间与执行完时间的差值,然后这样如果执行大量测试的话就很麻烦,并且不直观, 如果想对执行的时间做进一步 ...

  6. CSS3 text-overflow 属性

    1. <!DOCTYPE html> <html> <head> <style> div.test { white-space:nowrap; widt ...

  7. c3p0 config

    c3p0-config.xml<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> ...

  8. springMVC 访问404

    问题:404 但是其他的controller可以访问!!!

  9. Light OJ 1140

    数位dp,需要记录前导0. 数位dp中需要注意统计0,00,000……这些数字. 数位dp的写法可以分为两类.由于我们通常采用记忆化搜索的方式进行dp,所以我们有一个记忆化数组. 一种是记忆化数组的意 ...

  10. ios 使用block中使用self可能产生的循环引用

    在block中调用 self,那么就会引起循环引用问题,那么这是为什么呢? 为什么self会对block进行强引用呢???? 这里推荐一篇关于block的专业文章,http://blog.csdn.n ...