55 Jump Game
来源:互联网 发布:抗风柱设计软件 编辑:程序博客网 时间:2024/06/04 19: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
.
#include<stdio.h>#include <iostream>#include <vector>#include<algorithm>#include<functional>#include<tr1/unordered_map>using namespace std;class Solution {public: bool canJump(vector<int>& nums) { int i = 0; int n = nums.size(); for (int reach = 0; i < n && i <= reach; ++i) reach = max(i + nums[i], reach); return i == n; }};int main(){ Solution s; vector<int> vec_A; int c,n; cin>>n; for(int i=0;i<n;i++){ cin>>c; vec_A.push_back(c); } cout<<s.canJump(vec_A)<<endl; return 0;}
采用贪心算法,每次迭代算出能到达的最大位置,若指针未到达列表末位且已经超过目前能到达的最大位置时说明无法到达列表末位,无法跳出。
阅读全文
0 0
- LeetCode 55: Jump Game
- [leetcode 55] Jump Game
- LeetCode(55) Jump Game
- 55、Jump Game
- [LeetCode 55]Jump Game
- leetcode || 55、Jump Game
- LeetCode-55-Jump Game
- leetcode-55 Jump Game
- #55 Jump Game
- Jump Game - LeetCode 55
- 【Leetcode】Jump Game #55
- Leetcode #55 Jump Game
- 【LeetCode】55:Jump Game
- LeetCode(55) Jump Game
- Leetcode#55||Jump Game
- 55Jump Game
- (leetcode #55)Jump Game
- Leetcode 55 - Jump Game
- ssh 用法
- Laravel 5.4 官方文档摘记:请求实例
- 机器学习与深度学习(一) 决策树算法 (Decision Tree)
- 动态链接库导出模板类以及一些问题
- 具有P2P及防盗链功能的OTT/IPTV互联网解决方案
- 55 Jump Game
- 将github上的项目整合到jitpack
- 解决Xcode编译错误:does not contain bitcode
- 爬虫新的方法----中级
- iOS runtime实用篇
- 数组
- String常用方法介绍
- 计算机网络中TCP、IP协议的对比
- oracle同一列字符串拼接