Jump Game II
来源:互联网 发布:php购物车提交订单 编辑:程序博客网 时间:2024/05/01 06:49
看完题第一感觉,单源最短路,于是敲完代码,提交,超时。
之后仔细想了想, 广搜可以解题。
广搜:
public class Solution { public int jump(int[] A) { // Start typing your Java solution below // DO NOT write main() function int len = A.length; if(len <= 1) return 0; int []dist = new int[len]; dist[0] = 0; int maxIndex = 0; for(int i=0;i<=maxIndex;i++) { int pos = i + A[i]; if(pos >= len-1) return dist[i] + 1; if(pos > maxIndex) { for(int j=maxIndex+1; j<=pos; j++) { dist[j] = dist[i] + 1; } maxIndex = pos; } } return dist[len-1]; }}
单源最短路径:
import java.util.Arrays;public class Solution {public static final int MAX = (int) Math.pow(2, 30);public int jump(int[] A) { // Start typing your Java solution below // DO NOT write main() function int len = A.length; if(len == 0) return 0; boolean []isVisited = new boolean[len+1]; Arrays.fill(isVisited, false); int []dist = new int[len+1]; dist[0] = 0; for(int i=1;i<len;i++) dist[i] = A[0] >= i ? 1 : MAX; isVisited[0] = true; int CASE = len - 1; while(CASE-- > 0) { int min = MAX, index = 0; for(int i=0;i<len;i++) { if(isVisited[i]==false && dist[i]<min) { min = dist[i]; index = i; } } isVisited[index] = true; if(index == len-1) return min; for(int i=index+1;i<len;i++) { int len_index_i = A[index] >= i - index ? 1 : MAX; if(isVisited[i]==false && dist[index]+len_index_i<dist[i]) dist[i] = dist[index] + len_index_i; } } return dist[len-1]; }//public static void main(String[] args) {//Solution s = new Solution();//int []a = new int[30000];//for(int i=0;i<30000;i++) a[i] = 3000 - i;//System.out.println(s.jump(a));//}}
- Jump Game &Jump Game II
- Jump Game/Jump Game II
- LeetCode: Jump Game II
- LeetCode Jump Game II
- LeetCode: Jump Game II
- [Leetcode] Jump Game II
- [LeetCode] Jump Game II
- [Leetcode] Jump Game ii
- leetcode Jump Game II
- Jump Game II
- Jump Game II
- Jump Game II
- leetcode Jump Game II
- [LeetCode]Jump Game II
- Jump Game II
- Jump Game II
- Jump Game II
- Jump Game II
- Codeforces Round #186 (Div. 2)A、B、C、D、E
- PE
- C++ 虚函数
- hadoop 大数据开发3---HBase 安装步骤
- linux中搭建MongoDB Replset
- Jump Game II
- rqnoj 496 [IOI1999]花店橱窗布置 (简单dp)
- WinDBG 的首选资源
- jquery实现帮侧悬浮可收缩层
- 20个Android很有用的代码片段
- RHEL6 修改口令报错passwd: Authentication token manipulation error
- php调试与优化
- 海量数据处理 ——bloom filter
- HDU/HDOJ 1241 Oil Deposits (DFS)深度优先搜索