[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 ...
随机推荐
- java,求1-100之和。
package study01; public class TestWhile { public static void main(String[] args) { int sum = 0; int ...
- 使用lua做序列化和反序列化
-- lua对象序列化 function serialize(obj) local lua = "" local t = type(obj) if t == "numbe ...
- iOS开发之蓝牙业务封装
因为公司做智能家居开发,有很多蓝牙的智能硬件.因此项目中经常需要和蓝牙打交道.为此为了提高开发效率,就把蓝牙的公共业务进行了封装. 本文将对封装的思路做一个简单的阐述. 首先我们需要一个头文件.在这个 ...
- Angular-constructor和ngOnInit区别
参考文档:https://blog.csdn.net/u010730126/article/details/64486997 总结:constructor做依赖注入,避免业务操作: ngOninit做 ...
- 【转】vxworks的default boot line说明
boot程序的主要功能是引导vxworks 内核,所以boot程序需要知道vxworks的内核存放在何处,通过什么手段去获取.在vxworks缺省的boot程序里有一条内建的default boot ...
- 关键字final
final数据 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改:如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象.再次赋值将引起编译报错. 当f ...
- Golang ioutil读写文件测试
运用 ioutil.ReadFile .ioutil.WriteFile package main import ( "io/ioutil" "log" &qu ...
- python入门:if和else的基本用法
#!/usr/bin/env python # -*- coding:utf-8 -*- #2.X用raw_input,3.X用input #if和else的基本用法 name = input(&qu ...
- ubuntu 设置定时任务
crontab -l #查看详情crontab -e #设置定时任务 * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时 ...
- 《linux设备驱动开发详解》笔记——7并发控制
linux中并发无处不在,底层驱动需要考虑. 7.1 并发与竞争 7.1.1 概念 并发:Concurrency,多个执行单元同时.并行执行 竞争:Race Condistions,并发的执行单元对共 ...