55. Jump Game leetcode
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的更多相关文章
- [Leetcode][Python]55: Jump Game
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
- 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 ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- Jump Game - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Jump Game - LeetCode 注意点 解法 解法一:贪心算法,只关注能到达最远距离,如果能到达的最远距离大于结尾说明能到达,否则不能.并且如果 ...
- 刷题55. Jump Game
一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
- LeetCode 55. Jump Game (跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【一天一道LeetCode】#55. Jump Game
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...
随机推荐
- JQuery data方法的使用-遁地龙卷风
(-1)说明 我用的是chrome49,这个方法涉及到JQuery版本问题,我手里有3.0的,有1.9.1,后面将1.9.1及其以前的称为低版本,3.0称为高版本 测试例子用到的showMessage ...
- sass跨文件重写变量
利用变量默认值: !default 你可以在变量尚未赋值前,通过在值的末尾处添加 !default 标记来为其指定. 也就是说,如果该变量已经被赋值, 就不会再次赋值, 但是,如果还没有被赋值,就会被 ...
- 技术博客(初用markdown)。
技术博客 菜鸟教程在这个网站我学到许多有趣的东西,并且弥补了我之前的一些不足之处. 以下为我学习到的内容 输出不同的三位数 以下为代码和输出结果 *** #include<stdio.h> ...
- WebStorm设置左侧菜单栏背景色和样式
WebStrom一直以来都是默认的白色主题,今天想修改了下主题皮肤,结果导致左侧项目资源栏和顶部菜单栏也变成了黑色,结果无法改变回来,网上查了各种帖子,居然也没找到解决方法,自己研究了半天,终于搞定了 ...
- ZJOI2014 2048
Description 提交答案题,写个2048 AI 告诉你随机数生成方式. Sol xjblg+A*. 首先我写了个模拟,2048. 然后自己YY就可以啦...各种乱搞... 因为随机数,一个最好 ...
- CentOS 7更换 安装源
centos 安转源更换 一:切换到目录 cd /etc/yum.repos.d二: [备份] mv CentOS-Base.repo CentOS-Base.repo.bak三 : [下载] wg ...
- VB中 ByRef与ByVal区别
函数调用的参数传递有"值传递"和"引用传递"两种传递方式.如果采用"值传递",在函数内部改变了参数的值,主调程序的对应变量的值不会改变:如果 ...
- centos 6.5 apache配置web应用&防火墙设置(入门级)
硬件:centos 6.5 服务器 , Dell R420 , 两个网口,一个给公网,一个给内网. 软件:apache 2.2 配置了virtualhost以后,用curl在本地可以访问.但是其他机器 ...
- phpcms更换域名||外网访问本地网站
在网站在发展的过程中,很可能多次的修改域名:然后就发现我的搜索页还是之前的域名的链接地址,那么在PHPCMS V9中我们要怎么进行设置呢? 请进行以下步骤的修改: 修改/caches/config ...
- 使用GitHub
1. 进入 1.1 建立账号 打开网站https://github.com/主页上就可以注册.注册之后会有一个简单的帮助文档,在帮组文档上可以下载一个PC客户端(如果是WINDOWS平台,需要.NET ...