leetcode-55. Jump Game · Array
题面
这个题面挺简单的,不难理解。给定非负数组,每一个元素都可以看作是一个格子。其中每一个元素值都代表当前可跳跃的格子数,判断是否可以到达最后的格子。
样例
Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.Input: [,,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.
算法
只要存在一条路径可以到达最后就说明可以。我们可以从后往前看,只要前面存在元素索引加上其元素值大于目标元素索引值,就代表从前面格子可以跳到目标格子。只要我们从后往前判断满足该条件就向前滑动,即目标格子更新成为前面的格子,直到数组头则代表都能走通;否则,走不通。
倒叙遍历数组,定义target为数组尾,判断索引值+元素值 > target ?target = 该索引 :不做处理;
便利结束,判断 target = 0 ? true : false
评价
时间复杂度:O(n) 只需要遍历一次数组。
空间复杂度:O(1)
源码
class Solution {
public:
bool canJump(vector<int>& nums) {
int len = nums.size();
if(len <= )
return true;
int target = len -;
for(int i = target-; i >= ; i--)
{
if(nums[i] + i >= target)
target = i;
}
return target == ;
}
};
leetcode-55. Jump Game · Array的更多相关文章
- 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 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
- LeetCode 55. Jump Game (跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 55. Jump Game
我一开始认为这是一道DP的题目.其实,可以维护一个maxReach,并对每个元素更新这个maxReach = max(maxReach, i + nums[i]).注意如果 i>maxReach ...
- [LeetCode] 55. Jump Game 解题思路
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 ...
- 55. Jump Game (Array; Greedy)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- 17flutter中的路由/命名路由/命名路由传值/无状态组件传值/有状态组件传值。
main.dart import 'package:flutter/material.dart'; import 'package:flutter_demo/pages/Search.dart'; i ...
- SortedMap和TreeMap有什么区别?
SortedMap和TreeMap有什么区别 答: TreeMap的类的源码: public class TreeMap<K,V> extends AbstractMap<K,V ...
- java实现https免证书认证
java实现https免证书认证 解决方法: 1.下载两个包,httpclient-4.2.jar和httpcore-4.2.jar,复制以下代码就可使用. 2.调用类代码: String htt ...
- Linux——定时任务crontab
linux内置的cron进程能帮我们实现这些需求,cron搭配shell脚本,非常复杂的指令也没有问题. cron介绍 我们经常使用的是crontab命令是cron table的简写,它是cron的配 ...
- iOS-UIScreen,UIFont,UIColor,UIView,UIButton
6.1 UIScreen // 屏幕的宽度 CGFloat screenW = [UIScreen mainScreen].bounds.size.width; 6.2 UIFont + (UIFon ...
- socket 异步接收连接和接收数据
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Java中String做为synchronized同步锁
synchronized (("" + userId).intern()) { // TODO:something } JVM内存区域里面有一块常量池,关于常量池的分配: JDK6 ...
- ARTS 第八周打卡
Algorithm : 做一个 leetcode 的算法题 13. 罗马数字转整数 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I ...
- PHP和Memcached - Memcached的安装
1.现有扩展对比 memcache memcached 实现方式 原生 局域libmemcached的类库,性能高 编程方式 面向过程.对象 面向对象 CAS命令 NO YES php7 NO Y ...
- MySQL之锁、事务、优化、OLAP、OLTP
本节目录 一 锁的分类及特性 二 表级锁定(MyISAM举例) 三 行级锁定 四 查看死锁.解除锁 五 事务 六 慢日志.执行计划.sql优化 七 OLTP与OLAP的介绍和对比 八 关于autoco ...