leetcode105: jump-game-ii
题目描述
给出数组 A =[2,3,1,1,4]
最少需要两次才能跳跃到数组最后一个元素的位置。(从数组下标为0的位置跳长度1到达下标1的位置,然后跳长度3到数组最后一个元素的位置)
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =[2,3,1,1,4]
The minimum number of jumps to reach the last index is2. (Jump1step from index 0 to 1, then 3 steps to the last index.)
输出
- 2
class Solution {
public:
/**
*
* @param A int整型一维数组
* @param n int A数组长度
* @return int整型
*/
int jump(int* A, int n) {
// write code here
int curReach=0,maxReach=0,steps=0;
for (int i=0;i<n && i<=maxReach;i++)
{
if (i>curReach)
{
++steps;
curReach=maxReach;
}
maxReach=max(maxReach,i+A[i]);
}
if (maxReach<n-1){
return -1;
}else {
return steps;
}
}
};
leetcode105: jump-game-ii的更多相关文章
- 57. Jump Game && Jump Game II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- 55 Jump Game i && 45 Jump Game ii
Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- 【LeetCode】45. Jump Game II
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- 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 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [leetcode解题记录]Jump Game和Jump Game II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
随机推荐
- linux 内核参数设置 - sysctl
sysctl 命令用于查看和修改内核参数 查看指定参数: sysctl kernel.threads-max 查看所有参数: sysctl -a 修改指定参数: sysctl -w kernel.th ...
- C# 生成chart图表的三种方式
.net中,微软给我们提供了画图类(system.drawing.imaging),在该类中画图的基本功能都有.比如:直线.折线.矩形.多边形.椭圆形.扇形.曲线等等,因此一般的图形都可以直接通过代码 ...
- c++ 西安交通大学 mooc 第十三周基础练习&第十三周编程作业
做题记录 风影影,景色明明,淡淡云雾中,小鸟轻灵. c++的文件操作已经好玩起来了,不过掌握好控制结构显得更为重要了. 我这也不做啥题目分析了,直接就题干-代码. 总结--留着自己看 1. 流是指从一 ...
- kubernetes-介绍与特性
1. kubernetes概述 1) kubernetes是什么 2) kubernetes能做什么 3) kubernetes特性 4) kubernetes集群架构与组件 5) kubernete ...
- 协同开发功能——Github团队协作
最近需要写一个HoloLens开发的简明介绍,其中要测试几个demo.用到github以团队协作,像下面是简单的事件记录. 一.创建项目 1. 2.项目设置 名称 描述description Init ...
- MySQL:SELECT COUNT 小结
MySQL:SELECT COUNT 小结 背景 今天团队在做线下代码评审的时候,发现同学们在代码中出现了select count(1) . select count(*),和具体的select co ...
- Windows下使用GitStack搭建Git服务器
Win10下使用GitStack搭建Git服务器 Git是目前世界上最先进的分布式版本控制系统(没有之一). 许多人习惯用复制整个项目目录的方式来保存不同的版本,或许还会改名加上备份时间以示区别. ...
- centos8上redis5在生产环境的配置
一,创建redis的数据和日志目录: [root@yjweb data]# mkdir /data/redis6379 [root@yjweb data]# mkdir /data/redis6379 ...
- 后羿:我射箭了快上—用MotionLayout实现王者荣耀团战
前言 昨晚跟往常一样,饭后开了一局王者荣耀,前中期基本焦灼,到了后期一波决定胜负的时候,我果断射箭,射中对面,配合队友直接秒杀,打赢团战一波推完基地.那叫一个精彩,队友都发出了666666的称赞,我酷 ...
- JavaSE学习笔记02运算符、帮助文档生成与Scanner输入
1. 运算符 1. 算术运算符:+,-,*,/,%,++,-- //二元运算符 int a = 10; int b = 20; int c = 25; int d = 25; System.out.p ...