LeetCode045 Jump Game II
来源:互联网 发布:网络流行用语英文 编辑:程序博客网 时间:2024/05/12 23:33
详细见:leetcode.com/problems/jump-game-ii
Java Solution: github
package leetcode;/*大致意思是:从0开始跳,每次跳的步数 <= 当前的值结果就是要跳到最后一个求跳最少次数 */public class P045_JumpGameII {/* * 4 ms * 13.87% */static class Solution2 {int jump(int[] nums) {if (nums == null || nums.length < 2)return 0;int ans = 0, curMax = 0, curRch = 0;for (int i = 0; i != nums.length; i++) {if (curRch < i) {ans ++;curRch = curMax;}curMax = Math.max(curMax, nums[i] + i);}return ans;}}}
C Solution: github
/* url: leetcode.com/problems/jump-game-ii/ jump: 519ms 14.29% jump2: 6ms 52.38%*/#include <stdio.h>#include <stdlib.h>int _min(int a, int b) { return a < b ? a : b;}int _max(int a, int b) { return a < b ? b : a;}int jump(int* n, int s) { int* m = (int*) malloc(sizeof(int) * (s)); int i = 0, j = 0, mi = 0; m[s-1] = 0; for (i = s-2; i > -1; i --) { mi = s; for (j = i + 1; j < s && j - i <= n[i]; j ++) { mi = _min(mi, m[j] + 1); } m[i] = mi; } //save ans to mi mi = m[0]; free(m); return mi;}int jump2(int* n, int s) { int ans = 0, mti = 0, cri = 0, i = 0; for (i = 0; i < s; i ++) { if (cri < i) { ans ++; cri = mti; } mti = _max(mti, n[i] + i); } return ans;}int main() { int n[] = {2, 3 ,1 ,1, 4}; int s = 5; printf("answer is %d\r\n", jump2(n, s));}
Python Solution: github
#coding=utf-8''' url: leetcode.com/problems/jump-game-ii @author: zxwtry @email: zxwtry@qq.com @date: 2017年4月7日 @details: Solution: 65ms 68.96%'''class Solution(object): def jump(self, n): """ :type n: List[int] :rtype: int """ nn = 0 if n == None else len(n) ans, mti, cur = 0, 0, 0 for i in range(nn): if cur < i: ans += 1 cur = mti if cur>=nn-1:break mti=max(mti,i+n[i]) return ansif __name__ == "__main__": n=[2,3,1,1,4] print(Solution().jump(n))
0 0
- LeetCode045 Jump Game II
- Jump Game &Jump Game II
- Jump Game/Jump Game II
- LeetCode: Jump Game II
- LeetCode Jump Game II
- LeetCode: Jump Game II
- [Leetcode] Jump Game II
- [LeetCode] Jump Game II
- [Leetcode] Jump Game ii
- leetcode Jump Game II
- Jump Game II
- Jump Game II
- Jump Game II
- leetcode Jump Game II
- [LeetCode]Jump Game II
- Jump Game II
- Jump Game II
- Jump Game II
- MyBatis 源码分析——动态代理
- C#133课的主要内容
- LeetCode43 Multiply Strings
- Linux 命令 练习 —02
- Linux下基于Platform的led驱动(二)——测试
- LeetCode045 Jump Game II
- 堆的应用
- LeetCode044 Wildcard Matching
- 20170407工作日记
- Useful Links for Statement of Purpose for CS Graduate Application,CS研究生申请SOP有用链接
- Windows中查看PowerShell版本
- Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled
- [LeetCode]234. Palindrome Linked List
- 集成剪枝分类算法的Bagging集成学习算法示例