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 ...
随机推荐
- 9 patch png 的上下左右
9 patch png 的上下左右 前言: 9 patch png 图片,扩展名为.9.png,是一个标准的PNG图像,它包括额外的1个像素的边界,通过对这个边界的描述来达到我们预期的拉伸效果.a ...
- iOS开发——网络篇——JSON和XML,NSJSONSerialization ,NSXMLParser(XML解析器),NSXMLParserDelegate,MJExtension (字典转模型),GDataXML(三方框架解析XML)
一.JSON 1.JSON简介什么是JSONJSON是一种轻量级的数据格式,一般用于数据交互服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) JSON的格式很像OC中的字典 ...
- sql执行顺序
SQL 不同于与其他编程语言的最明显特征是处理代码的顺序.在大数编程语言中,代码按编码顺序被处理,但是在SQL语言中,第一个被处理的子句是FROM子句,尽管SELECT语句第一个出现,但是几乎总是最后 ...
- windows2008安装IIS
一个朋友说不会使用2008搭建IIS.刚好我之后也要学习如何绕过waf需要安装一些环境.就帮个忙了,写这篇文章吧. 进入服务器管理. 添加角色 选择:WEB服务器(IIS)后会提示添加角色向导.那么久 ...
- OpenCV图像轮廓检测
轮廓检测: 轮廓检测的原理通俗的说就是掏空内部点,比如原图中有3*3的矩形点.那么就可以将中间的那一点去掉. 一.关键函数1.1 cvFindContours函数功能:对图像进行轮廓检测,这个函数将 ...
- 一、 Java的值传递和引用传递
如果参数类型是原始类型(基本类型),那么传过来的就是这个参数的一个副本,也就是这个原始参数的值,这个跟之前所谈的传值是一样的.如果在函数中改变了副本的值不会改变原始的值. 如果参数类型是引用类型,那么 ...
- CCF 模拟D 动态规划
http://115.28.138.223:81/view.page?opid=4 这道题写的我醉醉的,想建一棵指定深度的树最后统计满足条件的个数 居然没去考虑这样必然超时!!!代码写的也是醉了,把没 ...
- 算法题解之math类题
Bulb Switcher 灯泡开关 思路:除了平方数以外,其他所有位置的灯泡最终都被开关了偶数次,因此最终都为0.问题等价于求1~n中平方数的个数. public class Solution { ...
- Spring+SpringMVC+MyBatis+Maven 服务端XML配置
项目目录结构 spring-mybatis.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
- SDL播放声音
extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> ...