leetcode https://oj.leetcode.com/problems/jump-game-ii/
1.超时的,效率太低
public class Solution {
public int jump(int[] A) {
int len=A.length;
int d[]=new int[len];
d[0]=0; for(int i=1;i<len;i++)
{
d[i]=1;
int j;
for(j=1;j+i<len&&j<=A[j];j++)
{
d[i+j]=Math.max(d[i]+1,d[j+i]);
}
if(j+i==len) return d[len-1]; } return d[len-1]; }
}
其实根本不用判断的的,只要求出覆盖区域一定是当前最长+1;
public class Solution {
public int jump(int[] A) {
int len=A.length; int step=0;
int last=0;
int maxpost=0; for(int i=0;i<len;i++)
{
if(i>last)
{
step++;
last=maxpost; } maxpost=Math.max(i+A[i],maxpost); } return step; }
}
AC代码
public class Solution {
public int jump(int[] A) {
int len=A.length; int step=0;//记录当前的步骤
int last=0; //当前步骤达到的最大值
int maxpost=0;//当前能扩张的最大范围 ,很像bfs啊,一层一层的,bfs就是求最小距离的啊 for(int i=0;i<len;i++)
{
if(i>last)
{
step++;
last=maxpost; } maxpost=Math.max(i+A[i],maxpost); } return step; }
}
leetcode https://oj.leetcode.com/problems/jump-game-ii/的更多相关文章
- https://oj.leetcode.com/problems/majority-element/
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode shttps://oj.leetcode.com/problems/surrounded-regions/
1.从外围搜索O,深度搜索出现了 Line 35: java.lang.StackOverflowError Last executed input: ["OOOOOOOOOOOOOOOOO ...
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode——Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [leetcode]Best Time to Buy and Sell Stock II @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...
- [leetcode]Populating Next Right Pointers in Each Node II @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...
- LeetCode OJ-- Populating Next Right Pointers in Each Node II **@
https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 接上一题目,输入的树不是perfect ...
- 【原创】leetCodeOj --- Jump Game II 解题报告
原题地址: https://oj.leetcode.com/problems/jump-game-ii/ 题目内容: Given an array of non-negative integers, ...
随机推荐
- 如何在OpenWRT环境下做开发
1.搭建开发环境 首先,在执行make menuconfig后,会出现下图: 其中,图中红框部分是我定制路由器的系统版本,大家可以根据不同的路由器进行不同的选择:绿框部分表示我们需要编译一个SDK开发 ...
- OC基础-day03
1#pragma mark - Day03_01_对象作为方法的返回值 1) 对象是可以作为方法的返回值的,返回值应该写这个类指针类型. - (MkDog *)buyDog; 代表方法执行完毕之后,有 ...
- 【清橙A1094】【牛顿迭代法】牛顿迭代法求方程的根
问题描述 给定三次函数f(x)=ax3+bx2+cx+d的4个系数a,b,c,d,以及一个数z,请用牛顿迭代法求出函数f(x)=0在z附近的根,并给出迭代所需要次数. 牛顿迭代法的原理如下(参考下图) ...
- 图解数据结构树之AVL树
AVL树(平衡二叉树): AVL树本质上是一颗二叉查找树,但是它又具有以下特点:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.在AVL树中任何节点的两个子 ...
- Eclipse中文乱码解决汇总(应该比较全):
Eclipse中文乱码解决汇总(应该比较全,欢迎补充): 方法一: 把GBK改成utf-8. 方法二: Window->preference->general->content ty ...
- Javascript 中 call 的两种用法
用法一(常见用法): 表现形式为:一个对象.方法.call(另一个对象),意义是用另一个对象代替当前对象,执行当前对象的方法.先看示例: function Class1(){ this.name = ...
- .Net4.0 ashx页面报错:检测到有潜在危险的Request.Form值(转)
原地址:http://zzhi191.blog.163.com/blog/static/1350849520111116518067/ web开发中难免要多到ajax技术. asp.net中我们处理a ...
- php 函数 array_slice
array_slice array_slice -- 从数组中取出一段 <?php$input = array("a", ); // returns "c ...
- iPhone分辨率
分辨率和像素 1.iPhone5 4" 分辨率320x568,像素640x1136,@2x 2.iPhone6 4.7" 分辨率3 ...
- 新年之际,盘点一些APP开发技巧
(原文:Reader Submissions - New Year's 2015 作者:Mattt Thompson 译者:培子 校对:蓝魂) 回顾过去一年发生在我们身边的事情时,有一点不得不提:对苹 ...