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 ...
随机推荐
- http之错误码
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes 响应码由三位十进制数字组成,它们出现在由HTTP服务器发送的响应的第一行.响应码分五种类型 ...
- 吉他笔记 solo 和弦 推弦 音程
十二平均律: 如下图所示: 第一行为唱名:do re mi fa so.... 第二行为音名:C #C D #D E F #F G #G A #A B C 第三行为D调对应的音名,即1 = D 第四行 ...
- asp.net的sql防注入和去除html标记的方法
一. // <summary> /// 过滤标记 /// </summary> /// <param name="NoHTML">包括HTML, ...
- TCP(传输控制协议)和三次握手和四次断开
TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 793定义.在简化的计算机网络OSI模型中, ...
- Android学习笔记(十八)——再谈升级数据库
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 之前我们为了保证数据库中的表是最新的,只是简单地在 onUpgrade()方法中删除掉了当前所有的表,然后强制 ...
- 160809228_符瑞艺_C语言程序设计实验3 循环结构程序设计
#include <stdio.h> int main(){ //使用for循环完成1+2+......+100 ; ;i<=;i++) sum +=i; //sum = sum ...
- (转) C#如何使用异步编程
怎么使用异步,就是用委托进行处理,如果委托对象在调用列表中只有一个方法,它就可以异步执行这个方法.委托类有两个方法,叫做BeginInvoke和EndInvoke,它们是用来异步执行使用. 异步有三种 ...
- Design Elevator
From: https://discuss.leetcode.com/topic/89/write-elevator-program-using-event-driven-programming/9 ...
- 【leetcode】Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- 【leetcode】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...