12.1—贪心法—Jump Game
来源:互联网 发布:社交网络剧情 编辑:程序博客网 时间:2024/05/19 19:58
描述
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.
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.
#include<iostream>using namespace std;bool JumpGame(int a[], int n){if (a == NULL || n <= 0)return false;int reachindex = a[0];if (reachindex >= n - 1)return true;for (int i = 0; i < n-1; i++){if (a[i] == 0 && reachindex == i)return false;if (a[i] + i > reachindex)reachindex = a[i] + i;if (reachindex >= n - 1)return true;}}int main(){const int n = 5;int a[n] = { 3,2,1,0,4};bool flag = JumpGame(a, n);if (flag)cout << "you are able reach int last index!" << endl;elsecout << "No!" << endl;}
阅读全文
0 0
- 12.1—贪心法—Jump Game
- 贪心法、动态规划——jump-game,jump-gameII
- 12.2—贪心法—Jump Game II
- 贪心算法——jump-game
- Leetcode 贪心 Jump Game
- Jump Game 贪心
- 贪心 Jump Game II
- [leetcode][贪心] Jump Game
- leetcode---Jump Game---贪心
- leetcode55. Jump Game (贪心)
- jump game 贪心算法
- leetcode---jump-game---贪心
- Leetcode 贪心 Jump Game II
- Jump Game I,II 贪心
- [leetcode][贪心] Jump Game II
- leetCode #55 Jump Game 贪心
- Jump Game II-贪心算法
- LeetCode Jump Game(贪心算法)
- 11.1—分治法—Pow(x,n)
- Ubuntu16.04如何将普通用户提升至root权限
- caffe随记(四) --- mnist示例超详细讲解
- Mac 使用 80 端口
- 11.2—分治法—Sqrt(x)
- 12.1—贪心法—Jump Game
- 信息系统设计师的出路有哪些?
- css 小技巧
- 在linux中安装jdk,以及linux下java环境变量的配置问题
- 12.2—贪心法—Jump Game II
- iOS中的@property的属性
- 游戏room8 第六关的答案
- 12.3—贪心法—Best Time to Buy and Sell Sto
- 系统开发团队需要什么样的专业人才?