`
huntfor
  • 浏览: 195373 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[leetcode]Jump Game

 
阅读更多

新博文地址:[leetcode]Jump Game

http://oj.leetcode.com/problems/jump-game/

 

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

题目思路是容易理解的,数组中每个数字表示最大跳数,问能不能调到最后一个元素,拿第一个数组A来讲:[2,3,1,1,4]第一个元素是2,即最大跳数是2,好吧,我们就跳两步,跳到了1,good,走一步,还是1,好的继续跳,4,最后一个元素,完成。当然,如果第一步我们不跳两步,跳一步的话,是3,还是可以跳到4的。综合来看,只要可以跳过0,就总是可以跳到最后一个元素的。

 

但是第二个数组A就不行,因为3,2,1,0这是个。。。(坑爹呢!)你是躲不过0的,因此,我的想法就是看看那数组中有没有这样的坑,如果有,再判断这样的坑能不能跳出去,如果存在跳不过去的这样的坑,返回false,其他情况总是true。

 

下面来看这种坑的特点,首先坑的必要不充分条件是0.。我们假设0的下标是index,则下标为[index - i]的值肯定是 <= index -i,比如,拿第二组测试用例来看,0的下标是3,则下标为2的数字必须 <= 1,下标为1的数字必须<=2,这样才能满足让你只能往坑里跳。。。

 

再看这样的案例:1,0,2,0

我们从后往前遍历,如果发现存在一个坑,那么就不需要再往前遍历了,因为就算前面顺利通过了,也得搁在这儿,如上例2 ,0其实并不是坑,是可以跳过去的,那继续往前遍历,查看前面是否存在“坑”。好了,发现第二个0,继续往前遍历,发现这个坑你是躲不过的,ok,返回false。

如1,0,1,0这个例子的话,只需要判断两个数字就可以了。

    public boolean canJump(int[] A) {
		int zeroIndex = 0;
		boolean canJump = true;
		for(int i =  A.length - 1; i >= 0; --i){
			if(A[i] == 0){
				zeroIndex = i;
				canJump = false;
				break;
			}
		}
		if(canJump == false){
			for(int i = zeroIndex; i >= 0; i--){
				if(A[i] > zeroIndex - i || (A[i] == zeroIndex - i && zeroIndex == A.length - 1)){
					canJump = true;
				}
				if (A[i] == 0 && i != zeroIndex && canJump) {//只有当后面的坑是可以跳过去的时候,才有必要检查前面的元素
					zeroIndex = i;
					canJump = false;
					continue;
				}
			}
		}
		return canJump;
    }

 

 

分享到:
评论

相关推荐

    fuxuemingzhu#Leetcode-Solution-All#55. Jump Game 跳跃游戏1

    1.贪心算法中,作出的每步贪心决策都无法改变,因为贪心策略是由上一步的最优解推导下一步的最优解,而上一步之前的最优解则不作保留 2.由(1)中的介绍,可以知道贪

    leetcode卡-Jump-Game-IV:跳跃游戏-IV

    leetcode卡跳跃游戏-IV 在这里找到了 Jump Game IV 的解决方案: 该解决方案适用于小型测试用例,但不适用于非常大的测试用例——仍在进行中。

    45jumpgame2.cpp

    leetcode45题leetcode45题leetcode45题leetcode45题leetcode45题leetcode45题leetcode45题leetcode45题leetcode45题leetcode45题

    leetcode71python-leetcode:leetcode

    leetcode 71 Python用 Python 编写 Leetcode (数据科学家的解决方案) - 易于理解的最佳解决方案,可以通过所有 Leetcode 测试用例,对于非 ...Game II (HARD) Leetcode 51. N-Queens (HARD) Leetcode 52. N-

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 $number 题号代表经典 LeetCode ...LeetCode ...Jump Game 56 Merge Intervals 64 Minimum Path Sum 73

    gasstationleetcode-LeetCode_Practice:我的LeetCode练习从2020年开始

    加油站 leetcode 力扣_实践 标签: leetcode 我的 LeetCode 练习从 2020 年开始 ...Leetcode ...55_Jump_Game 45_Jump_Game_II 121_Best_Time_to_Buy_and_Sell_Stock 122_Best_Time_to_Buy_and_Sell_Stock_

    leetcode跳跃-leetcode:leetcode一天一次

    Jump Game II - 二叉树 前序遍历判断二叉树:98. Validate Binary Search Tree - 二分查找 二分查找 + 数据缓存:1095. Find in Mountain Array 链表 有序链表合并:21. Merge Two Sorted Lists 回文 双指针判断回文...

    gasstationleetcode-leetcode:LeetcodeOJ解决方案

    leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167.二和二 2107/03/06 15.3 总和,16.3 总和最近,18.4 总和,11.最多水的容器 2017/03/09 62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/...

    leetcode和oj-algorithm:Javascript常用算法练习,对想找Javascript相关工作的人有帮助

    leetcode 和 oj 使用Javascript的算法练习 随着 Javascript 越来越流行,有很多 ...我把它们放在/LeetCode/目录下,例如如果你想运行LeetCode的JumpGame拼图,你只需要在shell中输入: node LeetCode/JumpGam

    leetcode跳跃-leetcode:leetcode解题之路

    II](./Array/jump-game-ii.md) [0053 最大子序和](./Array/maximum-subarray.md) [0041 缺失的第一个整数](./Array/first-missing-positive.md) [0042 接雨水](./Array/trapping-rain-water.md) [0048 旋转图像](./...

    LeetCode 55. 跳跃游戏

    题目来源:https://leetcode-cn.com/problems/jump-game 题目 给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个位置。 示例 1...

    lrucacheleetcode-leetcode-in-go:leetcode-in-go

    lru缓存leetcode Go 中解决的一些 Leetcode 问题 大批 ...jump-game-0055 最长公共子序列1143 最长公共子串 最长递增子序列0300 最大积子阵列0152 最大子阵列-0053 唯一路径-0062 word-break-0139 图形

    lrucacheleetcode-leetcode-rs:Leetcode算法问题的Rust解决方案

    lru缓存leetcode 已解决问题列表 (224) 1两和容易 5最长回文子串中 7反转整数简单 8字符串到整数 (atoi)中 15 3Sum中 20个有效括号简单 21轻松合并两个排序列表 第33章在旋转排序数组中搜索 35搜索插入位置容易 36个...

    lrucacheleetcode-RandomTasks:我为自学解决的任务

    lru缓存leetcode 随机任务 任务来自: 中等的: - 313 项测试中 311 项的时间限制 :( 第 1 周: 第 2 周: - 中等难度! 第 3 周: [Leftmost Column with at Least a One]() - 尚未上传 第 4 周: [Subarray Sum ...

    cpp-算法精粹

    Jump Game II Best Time to Buy and Sell Stock Best Time to Buy and Sell Stock II Longest Substring Without Repeating Characters Container With Most Water Patching Array 动态规划 Triangle Maximum ...

Global site tag (gtag.js) - Google Analytics