[LeetCode] 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.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
- 创建一个标记为last,表示A数组下标为last 前的位置都可以达到,初始化为 1.
- 从左遍历数组,当前位置加上可跳跃的长度A[i] 更新last。
- 如果last >= n ,即可以达到数组末尾,否则失败。
我写的如下:
#include <iostream>
using namespace std; class Solution {
public:
bool canJump(int A[], int n) {
if(n<=) return true;
int last = ;
for(int i =;i<last&&i<n;i++){
last = last>i+A[i]+?last:i+A[i]+;
if(last>=n) return true;
}
return false;
}
}; int main()
{
int A[] = {,,,,};
Solution sol;
cout<<sol.canJump(A,sizeof(A)/sizeof(int))<<endl;
return ;
}
#include <iostream>
using namespace std; /**class Solution {
public:
bool canJump(int A[], int n) {
if(n<=1) return true;
int last = 1;
for(int i =0;i<last&&i<n;i++){
last = last>i+A[i]+1?last:i+A[i]+1;
if(last>=n) return true;
}
return false;
}
};
*/
class Solution {
public:
bool canJump(int A[], int n) {
for(int i = n-; i >= ; i--){
if(A[i] == ){
int j;
for(j = i - ; j >=; j--){
if(A[j] > i - j) break;
}
if(j == -) return false;
}
}
return true;
}
}; int main()
{
int A[] = {,,,,};
Solution sol;
cout<<sol.canJump(A,sizeof(A)/sizeof(int))<<endl;
return ;
}
[LeetCode] Jump Game 数组控制的更多相关文章
- LeetCode:寻找数组的中心索引【668】
LeetCode:寻找数组的中心索引[668] 题目描述 给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和 ...
- LeetCode:删除排序数组中的重复项||【80】
LeetCode:删除排序数组中的重复项||[80] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...
- LeetCode初级算法--数组01:只出现一次的数字
LeetCode初级算法--数组01:只出现一次的数字 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...
- LeetCode初级算法--数组02:旋转数组
LeetCode初级算法--数组02:旋转数组 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...
- 前端与算法 leetcode 189. 旋转数组
目录 # 前端与算法 leetcode 189. 旋转数组 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 189. 旋转数组 题目描述 189. 旋转数组 概要 把他当做一到简单 ...
- 每日一道 LeetCode (14):数组加一
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- LeetCode二维数组中的查找
LeetCode 二维数组中的查找 题目描述 在一个 n*m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增.请完成一个搞笑的函数,输入这样的一个二维数组和一个整数,判断数 ...
- [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 ...
随机推荐
- 【数论分块】bzoj2956: 模积和
数论分块并不精通……第一次调了一个多小时才搞到60pts:因为不会处理i==j的情况,只能枚举了…… Description $\sum_{i=1}^{n}\sum_{j=1 \land i \not ...
- 【php】函数重载问题
PHP 不支持函数重载,也不可能取消定义或者重定义已声明的函数.
- 多线程之volatile关键字(五)
开始全文之前,先铺垫一下jvm基础知识以及线程栈: JVM栈是线程私有的,每个线程创建的同时都会创建JVM栈,JVM栈中存放的为当前线程中局部基本类型的变量(java中定义的八种基本类型:boolea ...
- python中字符串的一些用法
一.字符串的拼接: a=‘123’ b=‘abc’ d=‘hello world’ 1.print(a+b) 2.print(a,b) 3. c=‘ ’.join((a ...
- python基础-面向对象的三大特征
继承 单继承 父类 基类 子类 派生类 继承:是面向对象软件技术当中的一个概念,如果一个类别A“继承自”另一个类别B,就把这个A称为“B的子类别”,而把B称为“A的父类别”也可以称“B是A的超类”. ...
- LeetCode(292) Nim Game
题目 You are playing the following Nim Game with your friend: There is a heap of stones on the table, ...
- PAT Basic 1075
1075 链表元素分类 给定一个单链表,请编写程序将链表元素进行分类排列,使得所有负值元素都排在非负值元素的前面,而 [0, K] 区间内的元素都排在大于 K 的元素前面.但每一类内部元素的顺序是不能 ...
- HTTP认证之基本认证——Basic(二)
导航 HTTP认证之基本认证--Basic(一) HTTP认证之基本认证--Basic(二) HTTP认证之摘要认证--Digest(一) HTTP认证之摘要认证--Digest(二) 在HTTP认证 ...
- THUSC2019游记
Day 0 完全没有明明是最后一次机会的紧张感.大概是滚粗的预兆. 住在西郊.房间好小. 和thupc前一样又有一场cometoj,好像又有小裙子了.upd:改成星空棒棒糖! Day 1 早餐有点棒. ...
- Makefile基础(一)
在大型的C语言项目中,一般都是由多个源文件编译链接形成的可执行程序,而这些源文件的处理步骤,通常交给Makefile来管理,Makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后 ...