您的位置:首页 > 编程语言 > C#

【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大则显示失败。

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 c#