比较典型的贪心。维护一个区间,区间表示第i步所能到达的索引范围。递推的方法为:每次都遍历一遍当前区间内的所有元素,从一个元素出发的最远可达距离是index+array[index],那么下一个区间的左端点就是当前区间的右端点+1,下一个区间的右端点就是当前区间的max(index+array[index]),以此类推,直到区间包含了终点,统计当前步数即可。
实现代码:
1 | public class Solution { |
比较典型的贪心。维护一个区间,区间表示第i步所能到达的索引范围。递推的方法为:每次都遍历一遍当前区间内的所有元素,从一个元素出发的最远可达距离是index+array[index],那么下一个区间的左端点就是当前区间的右端点+1,下一个区间的右端点就是当前区间的max(index+array[index]),以此类推,直到区间包含了终点,统计当前步数即可。
实现代码:
1 | public class Solution { |
原文作者: findingsea
原文链接: http://findingsea.github.io/2015/04/06/jump-game-ii/
发表日期: April 6th 2015, 11:05:00 am
版权声明: 本文采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可
jsonContent:
meta: false
pages: false
posts:
title: true
date: true
path: true
text: false
raw: false
content: false
slug: false
updated: false
comments: false
link: false
permalink: false
excerpt: false
categories: true
tags: true