博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode55 Jump Game
阅读量:4598 次
发布时间:2019-06-09

本文共 1023 字,大约阅读时间需要 3 分钟。

题目:

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. (Medium)

For example:

A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

分析:

不知道为什么jump game在 jump game II后面...

思路跟jump game II一样,两重循环动归是超时的。

维护一个end表示当前能到的最远点,每到一个节点更新end,一旦到达一个大于end的点,返回false;

一旦发现更新end > num.size() - 1,则返回true

代码:

1 class Solution { 2 public: 3     bool canJump(vector
& nums) { 4 if (nums.size() == 1) { 5 return true; 6 } 7 int end = 0; 8 for (int i = 0; i < nums.size(); ++i) { 9 if (i > end) {10 return false;11 }12 end = max(end, nums[i] + i);13 if (end >= nums.size() - 1) {14 return true;15 }16 }17 return false;18 }19 };

 

 

 

转载于:https://www.cnblogs.com/wangxiaobao/p/5866598.html

你可能感兴趣的文章
51nod1241(连续上升子序列)
查看>>
SqlSerch 查找不到数据
查看>>
集合相关概念
查看>>
Memcache 统计分析!
查看>>
(Python第四天)字符串
查看>>
个人介绍
查看>>
使用python动态特性时,让pycharm自动补全
查看>>
MySQL数据库免安装版配置
查看>>
你必知必会的SQL面试题
查看>>
html5 Canvas绘制时钟以及绘制运动的圆
查看>>
Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween
查看>>
JavaScript空判断
查看>>
洛谷 P1439 【模板】最长公共子序列(DP,LIS?)
查看>>
python timeit
查看>>
Wireless Network 并查集
查看>>
51nod 1019 逆序数
查看>>
20145202马超《JAVA》预备作业1
查看>>
云推送注意(MSDN链接)
查看>>
IDEA 生成 jar 包
查看>>
加减乘除混合版
查看>>