【LeetCode】C# 55、Jump Game
2017-10-13 15:33
417 查看
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.
有点像飞行棋,给定数组,每个数字代表你能往前走的最大格数,判断能否到达终点。
思路:定义整数 max 来存放到达第i位时能跳到的最远的距离。当i比max大则显示失败。
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.
有点像飞行棋,给定数组,每个数字代表你能往前走的最大格数,判断能否到达终点。
思路:定义整数 max 来存放到达第i位时能跳到的最远的距离。当i比max大则显示失败。
public class Solution { public bool CanJump(int[] nums) { int max = 0; for(int i=0;i<nums.Length;i++){ if(i>max) return false; max = Math.Max(nums[i]+i,max); } return true; } }
相关文章推荐
- leetcode 55|45. Jump Game 1|2
- leetcode[55]Jump Game
- LeetCode 55 Jump Game
- Leetcode 55 - Jump Game
- LeetCode(55)Jump Game
- LeetCode 55 Jump Game
- LeetCode55——Jump Game
- [LeetCode] 55. Jump Game
- LeetCode-55-Jump Game(C语言实现)
- LeetCode 55 - Jump Game
- 【Leetcode】45,55 Jump game
- LeetCode(55) Jump Game
- LeetCode(55) Jump Game
- LeetCode - 55 - Jump Game
- LeetCode 55: Jump Game
- 【LeetCode】55、jump game
- 第六周:(LeetCode 45 55) Jump Game I II(c++)
- leetcode(55). Jump Game
- LeetCode55 Jump Game
- LeetCode-55-Jump Game