Leetcode 题解 Jump Game
一,笨方法 o(n^2)。果然看完别人的解法就自惭形秽啊!!
我用的动态规划方法。
比如输入
i: 0 1 2 3 4
————————————————
a[i]: 2 3 1 0 4
直接利用原来的数组计算。
a[i]>0 表示从a[i]出发可以达到终点,否则a[i] = 0。
首先,a[4]无论是多少,都能到4,因为到了4就不用走了。a[4] = 1;
现在看a[3] = 0,a[3] 只能在原地,所以a[3] = 0.
看a[2] = 1,那么看看 a[2 + 1]是否能到终点,如果可以a[2] =1,否则a[2] = 0.
看a[1] = 3,那么看看a[1+1],a[1+2],a[1+3]这三个数,是否有一个为1就可以了。
最终结果:
如果a[0] > 0,OK。否则false。
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size();
vector<int> &a = nums;
int i,j;
a[n-] = ;
for(i = n-; i>=; i--)
{
for( j = ; j <= a[i]; j ++)
{
if(a[i+j] > )
{
break;
}
}
if(j > a[i])
{
a[i] = ;
}
}
return a[] > ;
}
};
二、高手的解法。厉害啊。
用max来记录能到达的最右侧的位置。初始max = 0。因为只能在第0个位置。
遍历每一个能到达的位置,然后不断更新max。
最后检测,max是否到达了n-1.
bool canJump(vector<int>& nums) {
int n = nums.size();
vector<int> &a = nums;
int i;
int max_reach = ; //表示能达到的最大值
for(i = ; i<= max_reach; i++)
{
max_reach = max(max_reach, a[i] + i); //i 可以偏移 a[i]个单位。最大就是i + a[i]
if(max_reach >= n-) return true;
}
return false;
}
Leetcode 题解 Jump Game的更多相关文章
- Java for LeetCode 055 Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- leetcode题解-122买卖股票的最佳时期
题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...
- [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)
目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
- 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)
目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...
- 【LeetCode题解】844_比较含退格的字符串(Backspace-String-Compare)
目录 描述 解法一:字符串比较 思路 Java 实现 Python 实现 复杂度分析 解法二:双指针(推荐) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以 ...
随机推荐
- mysql查询优化之四:优化特定类型的查询
本文将介绍如何优化特定类型的查询. 1.优化count()查询count()聚合函数,以及如何优化使用了该函数的查询,很可能是mysql中最容易被误解的前10个话题之一 count() 是一个特殊的函 ...
- HTTPConnectionPool(host:XX)Max retries exceeded with url 解决方法
爬虫多次访问同一个网站一段时间后会出现错误 HTTPConnectionPool(host:XX)Max retries exceeded with url '<requests.package ...
- HDFS的工作流程
HDFS的工作机制 概述 HDFS集群分为两大角色:NameNode.DataNode NameNode负责管理整个文件系统的元数据 DataNode 负责管理用户的文件数据块 文件会按照固定的大小( ...
- Jmeter(二十七)Jmeter Question 之“集成Ant+Jenkins自动化”
首先介绍一下Ant.Apache Ant,是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发.由Apache软件基金会所提供. 是的.还是Apache家 ...
- 不同版本Eclipse对JDK版本要求
原文:https://blog.csdn.net/kevin_pso/article/details/54971739 1.Eclipse 4.6 (Neon)---需要JDK1.8版本,官网解释如下 ...
- CountDownLatch的简单讲解
正如每个Java文档所描述的那样,CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行.在Java并发中,countdownlatch的概念是一 ...
- Android给TextView设置多个字体颜色
效果如下:
- OpenJudge Cartesian Tree
[代码] #include <cstdio> #include <cstdlib> #include <cstring> #include <algorith ...
- xcode pod install 安装失败,提示缺少文件
I had the same problem in Xcode 6.1.1. I did the following to solve it: Set the configuration file s ...
- (转)C# WebApi 接口参数不再困惑:传参详解
原文地址:https://www.cnblogs.com/landeanfen/p/5337072.html 本篇打算通过get.post.put.delete四种请求方式分别谈谈基础类型(包括int ...