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, ...
随机推荐
- Git查看、删除、重命名远程分支和tag
这篇文章记录我在使用git的过程中碰到远程分支和tag的相关内容,提纲: 查看远程分支 删除远程分支和tag 删除不存在对应远程分支的本地分支 重命名远程分支 把本地tag推送到远程 获取远程tag ...
- swift 截取字符串
- iOS10新增Api详解
1.SiriKit SiriKit的功能非常强大,支持音频.视频.消息发送接收.搜索照片.预订行程.管理锻炼等等.在用到此服务时,siri会发送Intent对象,里面包括用户的请求和各种数据,可以对这 ...
- wireshark添加用户执行
默认情况下,访问网络端口需要root权限,而wireshark的只是/usr/share/dumpcap的一个UI,/usr/share/dumpcap需要root权限,所以没法non-root用户无 ...
- 月薪10K必备--C#下拉框联动
下拉框联动 很多网站上都用到下拉框联动,就是第一个下拉框没有选择任何项,第二个下拉框就没有选项.这样的做法更加谨慎,更加紧密. 下面我就教大家怎么做下拉框联动: 首先在窗 ...
- java_设计模式_组合模式_Composite Pattern(2016-08-12)
概念: 组合模式(Composite Pattern)将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性. 有时候又叫做部分-整体模式,它使我们树 ...
- COM简单应用示例
使用com技术开发模式进行的示例. com技术关键部分源码:主要将所有接口都写入到这个文件中 testinterface.h #ifndef TESTINTERFACE_H #define TESTI ...
- 如何删除github上的项目
如果大伙用过github,那么就会知道它上面没有明显的选项可以让你直接删除掉一个Repositories或者项目.因此对很多github的新手来说一开始也常觉得这个事情比较犯难.这里我给大家简单介绍一 ...
- windows下使用cxfreeze打包python3程序
1:下载适合版本的cxfreeze http://sourceforge.net/projects/cx-freeze/files/4.3.2/ 2:安装,注意python版本是否正确 3:安装完成后 ...
- linux建立信任关系
(1).切换到需要建立信任关系的用户(2).执行命令:ssh-keygen -d,然后一直回车.该命令会在用户home目录下生成一个隐藏的.ssh目录.目录里面有两个文件:id_dsa.id_dsa ...