[LeetCode]题解(python):045-Jump game II
题目来源
https://leetcode.com/problems/jump-game-ii/
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.
Your goal is to reach the last index in the minimum number of jumps.
题意分析
Input: a list named nums
Output:minimum steps to the last index
Conditions:最小的步数到达最后
题目思路
网上大牛的代码好厉害= =想了老半天才想懂,主要思想是每一次跳跃时记录一个能到达的最大位置,然后在这个位置前记录所能到达的位置的最大位置(不断更新),当到达之前步数记录最大位置时,step+1,然后赋值新的最大位置,然后继续遍历下去(感觉这样说我都不明白……直接看代码)
AC代码(Python)
class Solution(object):
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
last = 0 #the number steps
step = 0 #current max
curr = 0 for i in range(len(nums)):
if i > last:
step += 1
last = curr
curr = max(curr, i + nums[i]) return step
[LeetCode]题解(python):045-Jump game II的更多相关文章
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- LeetCode 045 Jump Game II
题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...
- Java for LeetCode 045 Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 045 Jump Game II 跳跃游戏 II
给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...
- LeetCode (45) Jump Game II
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- LeetCode 题解 Search a 2D Matrix II。巧妙!
[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30 ...
- LeetCode题解之Pascal's Triangle II
1.题目描述 2.题目分析 题目要求返回杨辉三角的某一行,需要将杨辉三角的某行的全部计算出来. 3.代码实现 vector<int> getRow(int rowIndex) { ) ,) ...
- LeetCode 题解之Linked List Cycle II
1.题目描述 2.问题分析 使用快慢指针方法判断链表是否有环,然后寻找环开始的节点. 3.代码 ListNode *detectCycle(ListNode *head) { if( head == ...
- LeetCode 题解之Add Two Numbers II
1.题目描述 2.分析 首先将链表翻转,然后做加法. 最后将结果链表翻转. 3.代码 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { Lis ...
随机推荐
- .NET中的视图和过滤器 (DefaultView和RowFilter)
NET中的视图和过滤器 (DefaultView和RowFilter) ADO.NET中有一层对象,用来创建任意数据源的抽象模型.其中包括DataSet,DataTable,DataRow,DataV ...
- 【BZOJ】1601: [Usaco2008 Oct]灌水(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=1601 很水的题,但是一开始我看成最短路了T_T 果断错. 我们想,要求连通,对,连通!连通的价值最小 ...
- Dijkstra堆优化与SPFA模板
Dijkstra+优先队列 #include<cstdio> #include<cctype> #include<queue> #include<cstrin ...
- JBPM表达业务流程(流程定义语言)
业务流程包括三部分: 活动 Activity / 节点 Node (有很多种,不同的类型有不同的功能,必须要有一个Start Activity) 连线 Transition / 转移 (从一个Acti ...
- FLTK 1.3.3 VS 2010 Configuration 配置
Download FLTK 1.3.3 Download VS2010 I assume you've already installed VS2010 correctly. Compile the ...
- shenyi 语录
[讲师]沈逸(65480539) 2016-06-08 14:58:42 会centos 转redhat是分分钟的事 [讲师]沈逸(65480539) 2016-06-08 14:58:54 查看 ...
- OS | 哲学家问题
哲学家进餐问题: (1) 在什么情况下5 个哲学家全部吃不上饭?考虑两种实现的方式,如下:A.算法描述: void philosopher(int i) {/*i:哲学家编号,从0 到4*/ whil ...
- Spring3.1 Cache注解
依赖jar包: <!-- redis --> <dependency> <groupId>org.springframework.data</groupId& ...
- POJ 1182 食物链(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ...
- Bootstrap页面布局7 - Bootstrap响应式布局的实用类
在bootstrap-responsive.css这个CSS样式表中已经为我们设定好了几个实用的类: .visible-phone: 在智能手机设备上显示这个元素,在其他设备上隐藏该元素 .visib ...