Jump Game
来源:互联网 发布:ttl数据 编辑:程序博客网 时间:2024/05/17 03:12
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
.
一开始给出了个错误的递归答案,大数据通不过
class Solution {public:bool judge(int* A, int start, vector<int>& dp){for (int step = A[start]; step > 0 ; step--){if (start + step >= dp.size() - 1) return true;if (dp[start + step] && judge(A, start + step, dp))return true;elsedp[start + step] = 0;}return false;}bool canJump(int A[], int n){vector<int> dp(n , 1);return judge(A, 0, dp);}};
仔细考虑后,简单暴力通过
class Solution {public: bool canJump(int A[], int n) { if (1 == n) return true; int max = 0; for (int i = 0; i < n - 1 && i <= max; i++) { max = (i + A[i] > max ? i + A[i] : max); if (max >= n - 1) return true; } return false; }};
0 0
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- 利用redis缓存热门数据,分页的一种思路
- 什么是动态规划
- tomcat集群
- Eclipse黑色主题设置
- git常用命令总结
- Jump Game
- grep及正则表达式
- UVa 12301 An Angular Puzzle 平面角度计算
- 黑马程序员 Java基础 面向对象:static关键字 main函数 单例设计模式
- java编程_socket_套接字_网络编程
- 记事本实用技巧集锦
- 一些好的关于算法的文章或网站
- 快速召唤CMD的几个方法
- win7的隐藏功能:上帝模式