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, ...
随机推荐
- css - a:hover变色问题
今天在帮我们学校做网站的时候,由于在css这里不是很擅长,过程中发现一个问题,a:hover的时候,字体的颜色不变.后来才发现将a和div的嵌套的问题, 我的css代码为: .left_box .lb ...
- [Twisted] deferred
Twisted提供一个优雅的实现(Deferred)来管理回调函数. Deferred Object 的结构 Deferred Object包含两个回调函数列表.一个用来保存成功的回调函数,另一个用来 ...
- Python:字典
#!/usr/bin/python3 #dict 字典 #字典是可变的 dict1 = {"name":"张三","age":22} pri ...
- maven项目显示红叉的解决方法
我们在做maven项目时,有时项目会显示红叉,但是项目本身并没有错误,如何去掉呢? 下面是我的解决方法 1.点击项目再右键,在搜索框中输入facets 2.把Dynamic Web Module的版本 ...
- Bootstrap 3 支持 IE8
Bootstrap 3 支持 IE8 <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries ...
- yum命令学习
yum配置文件 /etc/yum.conf yum check-update检查一下有无更新 每天都要(设置定时任务todo) 1.列出所有可更新的软件清单---yum check-update 2. ...
- Python 基础-python-列表-元组-字典-集合
列表格式:name = []name = [name1, name2, name3, name4, name5] #针对列表的操作 name.index("name1")#查询指定 ...
- 4537: [Hnoi2016]最小公倍数
Description 给定一张N个顶点M条边的无向图(顶点编号为1,2,…,n),每条边上带有权值.所有权值都可以分解成2^a*3^b的形式.现在有q个询问,每次询问给定四个参数u.v.a和b,请你 ...
- 自己挖坑自己跳 之JsonMappingException: (was java.lang.NullPointerException) (through reference chain:)
在Web项目中,我们经常会设计一些与界面相对应的JavaBean作为Entity,而为了兼容前台传入的空值,有些字段我们会用包装类型而不是基本类型.可是往往我的Entity已经设计完成,很多时候我们会 ...
- 关于setCharacterEncoding报错
有时候,代码已搬家,就会报这个错,导致这个错误的原因是: HttpServletResponse存在于servlet-api.jar中, 2.3版本的servlet-api.jar中HttpServl ...