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, ...
随机推荐
- java的集合框架之一
java是一套很成熟的东西,很多商用的东西都喜欢用它,用的人多,稳定.不过一般也不怎么说起它,因为太常见了,私下里说,写java应用层得就像农民工,每一处都是搭积木,根据设计师的东西如何优雅地搭好积木 ...
- 监听EditText的变化
http://liangruijun.blog.51cto.com/3061169/729505 之前博客上的有关EditText的文章,只是介绍EditText的一些最基本的用法,这次来深入学习一下 ...
- 如何:确定调用 ASP.NET 网页的方式
如何:确定调用 ASP.NET 网页的方式 通常有必要了解调用 ASP.NET 网页的方式:是由原始请求 (HTTP GET).回发 (HTTP POST).来自其他页的跨页面发送 (HTTP POS ...
- 一个jquery的图片下拉列表 ddSlick
[ddSlick]http://designwithpc.com/Plugins/ddSlick How to use with JSON data Include the plugin javasc ...
- Ajax结合Js操作灵活操作表格
Table页面: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...
- C#字符串颠倒输出
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- Codevs 1081 线段树练习 2
1081 线段树练习 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 传送门 题目描述 Description 给你N个数,有两种操作 1:给区间[a,b]的 ...
- Jquery插件之信息弹出框showInfoDialog(成功、错误、警告、通知)
一.信息种类说明: 1.1.操作成功信息 1.2.错误信息 1.3.警告信息 1.4.通知信息 二.使用说明 <!DOCTYPE html PUBLIC "-//W3C//DTD HT ...
- 分享一个自己写的基于TP的关系模型(2)
1.增加多对多关系的处理 /** * 定义关系 * @return array */ public function test4(){ //参数说明 //关联的模型 //主表关联字段 //关联中间表 ...
- PHP创建桌面快捷方式实例
要利用php创建桌面快捷方式我们需要借助于header,InternetShortcut及一些我看不懂的代码. 方法:新建一个php文件,然后把下面的代码扔进去,保存为比如shortcut.php,放 ...