[Leetcode Week7]Jump Game
Jump Game 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/jump-game/description/
Description
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.
Solution
class Solution {
private:
int max(int a, int b) {
return a > b ? a : b;
}
public:
bool canJump(vector<int>& nums) {
int size = nums.size();
if (size == 0)
return false;
if (size == 1)
return true;
if (nums[0] >= size - 1)
return true;
int* farthestPoint = new int[size];
farthestPoint[0] = nums[0];
for (int i = 1; i < size; i++) {
if (i <= farthestPoint[i - 1]) {
farthestPoint[i] = max(farthestPoint[i - 1], i + nums[i]);
} else {
delete []farthestPoint;
return false;
}
}
bool result = farthestPoint[size - 2] >= size - 1;
delete [] farthestPoint;
return result;
}
};
解题描述
这道题我使用的是贪心算法,通过遍历nums来更新每一个点上能够达到的最远点,最终判断是否能够达到终点。
[Leetcode Week7]Jump Game的更多相关文章
- Java for LeetCode 055 Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Frog Jump 青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
- Leetcode: Frog Jump
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 55. Jump Game (跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- 虚拟现实-VR-UE4-认识UE4
VR的火热,让每个人都想参与一下, 公司在展会上面搞了一个VR的Demo,关注度超出预期,使得公司高层决定来个VR项目 所以 关于UE4 百度百科地址:http://baike.baidu.com/l ...
- 序列化---fastjson使用
该文章主要介绍com.alibaba.fastjson的使用. 首先创建maven工程,导入fastjson.挑个热度高的版本就好了. 首先考虑下,我们通常什么时候会使用序列化和反序列化: 1.将ja ...
- beanshell引用参数化数据
步骤: 1.添加参数化组件CSV Data Set Config: 2.添加beanshell preprocessor,引用变量: 验证: 2个线程,迭代2次,分别取了4个不同的值.
- 1072 Gas Station (30 分)(最短路径)
#include<bits/stdc++.h> using namespace std; ; int n,m,k,Ds; int mp[N][N]; int dis[N]; int vis ...
- poj3026(bfs+prim)最小生成树
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...
- systemtap没找到函数变量
为啥systemtap没找到函数 hon@station6:~/codebox/stap/net$ sudo stap -L 'kernel.function("sock_recvmsg_n ...
- 算法(10)Subarray Sum Equals K
题目:在数组中找到一个子数组,让子数组的和是k. 思路:先发发牢骚,这两天做题是卡到不行哇,前一个题折腾了三天,这个题上午又被卡住,一气之下,中午睡觉,下午去了趟公司,竟然把namespace和cgr ...
- sessionStorage & URL Origin
sessionStorage & URL Origin same origin https://developer.mozilla.org/en-US/docs/Web/API/Window/ ...
- setcookie函数
在任何请求的服务器响应都会有个头部,默认情况下,头部发送动作会在第一个输出发生时触发,如echo,<html>.(注:php有个header方法手动发送原生header) 由于setcoo ...
- 【bzoj3052】[wc2013]糖果公园 带修改树上莫队
题目描述 给出一棵n个点的树,每个点有一个点权,点权范围为1~m.支持两种操作:(1)修改一个点的点权 (2)对于一条路径,求$\sum\limits_{i=1}^m\sum\limits_{j=1} ...