leetcode55—Jump Game
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.
Example 1:
Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
想法:动态规划方式,按照如下方式:
1、最后一步:假设在索引i的地方,能够到达索引n-1,那么需要满足条件(1)青蛙能够落在索引i(2)i+array[i]>n-1
2、状态转移表达式:f[j] = (0=<i<j)(f[i]&&i+f[i]>j)
3、边界条件:f[0]=true
4、计算顺序
class Solution {
public:
bool canJump(vector<int>& nums) {
== nums.size())
return false;
bool result[nums.size()] ;
result[] = true;
//从第一块石头开始
; i < nums.size() ; i++){
result[i] = false;
//遍历石头i之前的所有石头,判断那一块能够落在石头i上
; j < i ; j++){
if(result[j] && j + nums[j] >= i){
result[i] = true;
}
}
}
];
}
};
leetcode55—Jump Game的更多相关文章
- [array] leetcode-55. Jump Game - Medium
leetcode-55. Jump Game - Medium descrition Given an array of non-negative integers, you are initiall ...
- [LeetCode55]Jump Game
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- leetcode-55. Jump Game · Array
题面 这个题面挺简单的,不难理解.给定非负数组,每一个元素都可以看作是一个格子.其中每一个元素值都代表当前可跳跃的格子数,判断是否可以到达最后的格子. 样例 Input: [2,3,1,1,4] Ou ...
- Leetcode55. Jump Game跳跃游戏
给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true ...
- [Swift]LeetCode55. 跳跃游戏 | Jump Game
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 ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 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
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- 测试单元测试完毕关闭jvm
今天一天都在纠结Netty中的服务器端究竟是如何实现自动关闭的, 吃完晚饭才发现原来不是netty关闭,是测试单元关闭的...
- Java static和final
java提高篇(七)-----关键字static static 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个“伪全局”的概念,在Java中static表示“全局”或者“静 ...
- shell_语法
1.运算符: 1.基本语法:$((运算式))或$[运算式] 2.expr + n // 注意运算符中间有空格 再用expr时要加 ` ` 号,* 号加转义字符\ \* ,表示乘 2.判断语句 [ c ...
- HDU4725(KB4-P SPFA+LLL+SLF优化)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- POJ3186(KB12-O DP)
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5801 Accepted: 30 ...
- HDU1257(dp)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- jQuery获取子元素的个数
一.获取div下的子元素的个数 $("div").children().length; 二.获取div下的span子元素的个数 $("div").childre ...
- js弹窗 js弹出DIV,并使整个页面背景变暗
1.首先写一个遮罩层div,然后再写一个弹窗的div <!-- 遮罩层 --> <div id="cover" style="background: # ...
- 设计模式原则(5)--Law of Demeter(LoD)--迪米特法则
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.定义: 一个软件实体应当尽可能少地与其他实体发生相互作用.也就是说:一个类对自己依赖的类知道的越少越好.也就是 ...
- 【代码笔记】iOS-NSFileManager
一,代码. #import "ViewController.h" @interface ViewController () @end @implementation ViewCon ...